在布尔变量更改时更改JTextPane文本

时间:2015-12-14 19:19:55

标签: java swing jtextpane

我有一个带有两个子类的主类,一个用于GUI初始化,另一个用于服务器创建和功能。我有一个布尔变量' connected',当用户连接到服务器时为true,否则为false。我如何能够从服务器子类更新GUI子类中的JTextPane文本以说明"客户端连接"什么时候连接'是真的,没有联系"什么时候假?

主要课程:

public class serverMain {

    public static boolean allowConnections = false;
    public static boolean connected = false;
    public static boolean launchOnStart = false;
    public static boolean loop = true;

    public static int serverPort = 1234;
    public static ServerSocket serverSocket;

    public static void main(String args[]){

        serverGUI.gui();

        while (loop){

            if (allowConnections == true){
                serverCommands.commands();
            }
            else {
                System.out.println("reject");
            }

        }

    }

}

gui class(删除了相关部分):

public class serverGUI extends serverMain {

    public static JTextPane txtConnectionMarker;

    public static void gui(){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    serverGUI window = new serverGUI();
                    window.frmRemoteServer.setVisible(true);
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public serverGUI() {
        initialise();
    }

    public void initialise() {

        frmRemoteServer = new JFrame();
        frmRemoteServer.setBounds(100, 100, 720, 420);
        frmRemoteServer.setResizable(false);
        frmRemoteServer.setTitle("Remote Server");
        frmRemoteServer.getContentPane().setBackground(Color.WHITE);
        frmRemoteServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmRemoteServer.getContentPane().setLayout(null);

        JTextPane txtConnectionMarker = new JTextPane();
        txtConnectionMarker.setEditable(false);
        txtConnectionMarker.setBounds(548, 21, 149, 33);
        txtConnectionMarker.setFont(new Font("Segoe UI Light", Font.BOLD, 16));
        txtConnectionMarker.setBackground(SystemColor.menu);
        frmRemoteServer.getContentPane().add(txtConnectionMarker);

        if (connected == true){
            txtConnectionMarker.setText("Connected");
            txtConnectionMarker.setForeground(Color.GREEN);
        }
        else {
            txtConnectionMarker.setText("No Connection");
            txtConnectionMarker.setForeground(Color.RED);
        }

        // obviously this will not update as connected changes

    }
}

服务器类(删除了不相关的部分):

public class serverCommands extends serverMain {

    public static void commands() {

        String input;

        try {

            serverSocket = new ServerSocket(serverPort);
            System.out.println("socket opened on port "+serverPort);

            Socket clientSocket = serverSocket.accept();
            connected = true;

            // there's more to all parts but the given code
            // should be enough to get what I'm trying to do
            // basically once the user disconnects connected
            // will change back to false

1 个答案:

答案 0 :(得分:0)

您可以使用java接口创建一种侦听器,其中侦听器方法的实现在您的子类中编写。

public interface ResultListener {
    public void setResult(boolean result);
} 

然后您唯一需要的是将此接口作为参数传递给初始化服务器并调用侦听器方法。

public class MyServer {

    private ResultListener listener;

    public MyServer(ResultListener listener) {
       this.listener = listener;
    } 

    public void doSomething() {

        // other stuff
        listener.setResult(true); // or false if connected or not

    } 

} 

最后:

public class MainClass implements ResultListener {

    public void init() {
        // your GUI and other logic

        MyServer server = new MyServer(this);
        server.doSomething();

    } 

    @Override
    public void setResult(boolean result) {

        if(result) {
            // do stuff
            label.setText("Connected!");
        } else {
            // do other stuff
            label.setText("Connection failed!");
        } 
    } 

}