简体   繁体   English

具有新线程的新JFrame

[英]New JFrame with new thread

I created a JFrame with combo boxes and a button which will create a new thread and continue to do an action. 我用组合框和一个按钮创建了一个JFrame,它将创建一个新线程并继续执行操作。 I want a new JFrame to start with every new thread to output logs to the new JFrame. 我希望新的JFrame从每个新线程开始,以将日志输出到新的JFrame。 But even if I put the code related to the JFrame in the new thread and close that JFrame it ends the whole program instead of that running thread. 但是,即使我将与JFrame相关的代码放在新线程中并关闭该JFrame,它也会结束整个程序,而不是该运行线程。 What the best approach to making what I want possible? 使我想要的成为可能的最佳方法是什么? I simply want a new JFrame to open with each new thread started and when I close that JFrame it would end that thread. 我只是想在每个新线程启动时打开一个新的JFrame,当我关闭该JFrame时,它将结束该线程。

Regards! 问候!

By default, closing a JFrame will simply hide it (see the documentation for setDefaultCloseOperation() ). 默认情况下,关闭JFrame只会将其隐藏(请参阅文档setDefaultCloseOperation() )。 If closing a window is exiting your application, this must be due to your own code. 如果关闭窗口正在退出您的应用程序,那一定是由于您自己的代码。 You're not, by chance, calling setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) , are you? 您不是偶然地打电话给setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE) ,对吗?

Here are some ideas: 这里有一些想法:

  • Don't block the event dispatch thread; 不要阻塞事件分发线程; use SwingWorker instead, as shown here . 使用SwingWorker代替,如图所示这里

  • Don't use multiple frames; 不要使用多个框架; use panels in a container having a suitable layout . 在具有适当布局的容器中使用面板。

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

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