【问题标题】:Change JTextPane text on boolean variable change在布尔变量更改时更改 JTextPane 文本
【发布时间】:2015-12-14 19:19:54
【问题描述】:

我有一个包含两个子类的主类,一个用于 GUI 初始化,一个用于服务器创建和功能。我有一个布尔变量“已连接”,当用户连接到服务器时为真,未连接时为假。我如何能够从服务器子类更新 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 类(删除无关部分):

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

【问题讨论】:

  • if (connected) { label.setText("Connected"); } else { label.setText("Client not connected) } 也许(?)
  • 到底是什么问题?访问 JTextPane 实例?在正确的时间设置文本?附加到 JTextPane?请更具体,否则我们只能猜测。理想情况下,还请在minimal reproducible example 中显示您尝试过的内容。
  • 模型-视图-控制器
  • @Frakcool 用户可能无法访问组件实例?
  • @SatoshiKouno OP 询问 当“已连接”为真且“否”时,我如何能够从服务器子类更新 GUI 子类中的 JTextPane 的文本以说“客户端已连接”连接”当错误?这就是我“回答”...如果他无法访问它,那么也许他应该发布MCVE你不这么认为吗?

标签: java swing jtextpane


【解决方案1】:

您可以使用 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!");
        } 
    } 

} 

【讨论】:

  • 不要忘记 Swing 不是线程安全的,对 UI 的任何更新都必须在事件调度线程的上下文中进行
猜你喜欢
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
相关资源
最近更新 更多