java gui textarea 未正确更新

发布于 2025-01-03 08:13:36 字数 1327 浏览 2 评论 0原文

我的刷新和刷新2方法导致出现一个新窗口jpanel。我希望我的文本区域在同一窗口中更新。我认为我没有调用正确的 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);

}

My refresh and refresh2 methods cause a new window jpanel to appear. I want my textareas to update in the same window. I don't think I am calling the right 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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-01-10 08:13:36
  1. Swing 组件必须在事件分派线程中更新
  2. 无需使容器失效或发出重绘请求
  3. 不需要 使用空布局管理器
  4. 当提出这样的问题时,最好包含 sscce

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

有关详细信息,请参阅Swing 中的并发

  1. Swing components must be updated in the event-dispatching thread
  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

Questions like these are asked almost 50 times a day. Next time, please do a search for related items that have already been sufficiently answered.

For more information, see Concurrency in Swing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文