gpt4 book ai didi

java - JTextArea 未从另一个类更新

转载 作者:行者123 更新时间:2023-12-01 12:04:03 25 4
gpt4 key购买 nike

我有一个 JFrame 类:

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);
}
});

}

}

我在另一个类中有一个此类的实例:

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 控制台中立即记录该值,文本被 append 到 JTextArea,只是从未显示。

最佳答案

在您使用的 Console 类中:

 new Console().setVisible(true);

因此这将创建控制台并显示框架。

然后在您的服务器代码中执行以下操作:

console = new Console();

这会创建第二个控制台。因此,您的服务器类正在更新第二个不可见控制台的文本区域。

您应该只拥有一个控制台。如果您希望服务器类更新控制台,那么您需要将 Console 类的引用传递给服务器,而不是创建新的控制台。像这样的东西:

Server server = new Server(console);

现在您的服务器类已经引用了可见的控制台。

关于java - JTextArea 未从另一个类更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27782596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com