JTextArea 没有从另一个更新 class

JTextArea is not updating from another class

我有一个 JFrame class:

public class Console extends javax.swing.JFrame {


private StringBuilder b;
/**
 * Creates new form NewJFrame
 */
public Console() {
    initComponents();
    b = new StringBuilder();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("MainFrame"); // NOI18N
    setResizable(false);

    jScrollPane1.setHorizontalScrollBar(null);
    jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
    jScrollPane1.getViewport().setBorder(null);
    jScrollPane1.setViewportBorder(null);
    jScrollPane1.setBorder(null);


    jTextArea1.setBackground(new java.awt.Color(0, 0, 0));
    jTextArea1.setColumns(20);
    jTextArea1.setForeground(new java.awt.Color(204, 204, 204));
    jTextArea1.setRows(5);
    jTextArea1.setText("initial text\n");
    jTextArea1.setEditable(false);
    jScrollPane1.setViewportView(jTextArea1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public void start() {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Console.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Console().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration       

public void addText(String s){
    jTextArea1.append(s);
    System.out.println(jTextArea1.getText()+ " = text");
}
public void consoleText(final String consoleUpdate){
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          jTextArea1.append(consoleUpdate);
        }
     });

}

}

我在另一个 class:

中有这个 class 的实例
public SocketServer(int port){
    this.port = port;
    clientList = new ArrayList<Socket>();
    console = new Console();
    console.start();

这不起作用,我不知道为什么,我稍后在 SockerServer 中调用它。

SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          console.addText("Starting server at port: "+port);
        }
      });
}

简单

console.addText(...);

也不行。我的意思是它没有显示,但它确实添加到 JTextArea,因为我在 eclipse 控制台之后立即记录值,文本被附加到 JTextArea,只是从未显示。

在你的 Console class 你使用:

 new Console().setVisible(true);

这样就创建了控制台并显示了框架。

然后在您的服务器代码中执行:

console = new Console();

创建第二个控制台。所以你的服务器 class 正在更新第二个不可见控制台的文本区域。

您应该永远只有一个控制台。如果您希望您的服务器 class 更新控制台,那么您需要将控制台 class 的引用传递给您的服务器,而不是创建新的控制台。类似于:

Server server = new Server(console);

现在您的服务器 class 具有对可见控制台的引用。