java gui textarea没有正确更新

Cha*_*son -1 java concurrency user-interface swing textarea

我的refresh和refresh2方法导致出现一个新的窗口jpanel.我希望我的textareas在同一个窗口中更新.我不认为我正在调用正确的jpanel.我该如何解决?另外为什么要创建一个新窗口?

public static void main(String[] args) {
                MPUComp frame = new MPUComp();
                frame.setVisible(true);

}
public MPUComp() {
    setTitle("Mpu Finder");
    ImageIcon LoadIco = new ImageIcon(getClass().getResource("load.png"));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 799, 680);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null); 
    btnFind1.setBounds(250, 27, 68, 23);

    contentPane.add(btnFind1);  
    btnLoadMpu.setBounds(328, 27, 36, 22);
    btnLoadMpu.setIcon(LoadIco);
    contentPane.add(btnLoadMpu);
    btnFind2.setBounds(642, 27, 68, 23);
    contentPane.add(btnFind2);
    btnLoadMpu2.setBounds(720, 28, 36, 22);
    btnLoadMpu2.setIcon(LoadIco);
    contentPane.add(btnLoadMpu2);
    menu();

}
public void refresh(String pane1) {
    textArea_1.append(pane1 + "\n");
    contentPane.revalidate();
    contentPane.repaint();
    setVisible(true);
}
public void refresh2(String pane1) {
        textArea_2.append(pane1 + "\n");
        contentPane.revalidate();
        contentPane.repaint();
        setVisible(true);

}
Run Code Online (Sandbox Code Playgroud)

mre*_*mre 5

  1. 必须在事件派发线程中更新Swing组件
  2. 无需使容器无效或发出重绘请求
  3. 不要使用null布局管理器
  4. 当提出这样的问题时,最好包括一个sscce

像这样的问题每天被问到近50次.下次,请搜索已经得到充分回答的相关项目.

有关更多信息,请参阅Swing中的并发.