
package volume2.chapter3; import javax.swing.*; import java.awt.*; public class JFrameDispatchEventTest extends JFrame { public JFrameDispatchEventTest(){ JPanel panel = new JPanel(new BorderLayout()); JButton test = new JButton("Test"); JTextArea textArea = new JTextArea(); panel.add(test,BorderLayout.NORTH); panel.add(new JScrollPane(textArea),BorderLayout.SOUTH); System.out.println(Thread.currentThread()); test.addActionListener((e) -> { textArea.append(Thread.currentThread().toString()+"\n"); Thread t = new Thread(new Runnable() { @Override public void run() { textArea.append("Another thread runs in thread: "+Thread.currentThread()+"\n"); } }); t.start(); }); add(panel); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(JFrameDispatchEventTest::new); } } 这里我新开了一个线程更新textArea也没有什么问题啊。