我有一个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控制台之后立即记录该值,文本被附加到JTextArea,但从未显示过。
答案 0 :(得分:1)
在您使用的Console
课程中:
new Console().setVisible(true);
因此,这会创建控制台并显示框架。
然后在您的服务器代码中执行:
console = new Console();
创建第二个控制台。因此,您的服务器类正在更新第二个不可见控制台的文本区域。
您应该只有一个控制台。如果您希望服务器类更新控制台,则需要将Console类的引用传递给服务器,而不是创建新的控制台。类似的东西:
Server server = new Server(console);
现在,您的Server类具有对可见控制台的引用。