我正在使用 WindowBuilder 在 Java 中创建一些聊天功能。我的 GUI 类创建了一个线程,我希望能够在其中更新超类的 TextArea。我创建线程的原因是我希望能够中断子类内部的代码。
我的问题是我不能从子类的线程中附加到超类的 TextArea 。
我尝试将我的代码缩减到最基本的部分:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SSCCE {
private JFrame frmRoom;
private final static String newline = "\r\n";
private JTextField textField;
private JScrollPane scrollPane;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SSCCE window = new SSCCE();
window.frmRoom.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SSCCE() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmRoom = new JFrame();
frmRoom.setTitle("Test");
frmRoom.setBounds(100, 100, 450, 300);
frmRoom.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmRoom.setJMenuBar(menuBar);
JMenu mnButton1 = new JMenu("Button 1");
menuBar.add(mnButton1);
JMenuItem mntmButton2 = new JMenuItem("Button 2");
mntmButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Thread t = new Thread(new SSCCESub());
SwingUtilities.invokeLater(t);
//t.start();
//Neither of the above work.
}
});
mnButton1.add(mntmButton2);
textField = new JTextField();
scrollPane = new JScrollPane();
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
GroupLayout groupLayout = new GroupLayout(frmRoom.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 434, Short.MAX_VALUE)
.addGap(0))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
);
frmRoom.getContentPane().setLayout(groupLayout);
}
private void addLineToTextArea(String line) {
System.out.println("Tried calling a superclass method in order to append to the TextArea");
textArea.append(line + newline);
}
private static class SSCCESub extends SSCCE implements Runnable {
public void run() {
super.textArea.append("This won't be visible" + newline); //TODO This is what my question is about.
super.addLineToTextArea("This won't be visible");
System.out.println("This should be visible");
return;
}
}
}