简体   繁体   English

java gui textarea无法正确更新

[英]java gui textarea not updating properly

My refresh and refresh2 methods cause a new window jpanel to appear. 我的refresh和refresh2方法导致出现一个新窗口jpanel。 I want my textareas to update in the same window. 我希望我的文本区域在同一窗口中更新。 I don't think I am calling the right jpanel. 我不认为我在调用正确的jpanel。 How do I fix this? 我该如何解决? Also why is it creating a new window? 为什么还要创建一个新窗口?

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

}
  1. Swing components must be updated in the event-dispatching thread 必须在事件调度线程中更新Swing组件
  2. There is no need to invalidate the container or issue a repaint request 无需使容器无效或发出重涂请求
  3. Do not use a null layout manager 不要使用空布局管理器
  4. When asking a question like this, it's best to include an sscce 当问这样的问题,这是最好的,包括一个SSCCE

Questions like these are asked almost 50 times a day. 这类问题每天几乎要问50次 Next time, please do a search for related items that have already been sufficiently answered. 下次,请搜索已经被充分回答的相关项目。

For more information, see Concurrency in Swing . 有关更多信息,请参见Swing中的并发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM