从 JButton 进程中关闭 JFrame 保持活动状态
我有一个用 windowbuilderpro 开发的类,我想从 JButton 关闭它,而不是使用窗口上的标准 X 按钮,所以这里是该类的示例:
public class MainWindow {
public JFrame frame;
public MainWindow() {
initialize();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Show the main Frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
当我从 X 按钮关闭窗口时,窗口正确关闭,并且进程终止。
当我从具有此侦听器的 JButton 关闭时:
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
框架窗口正确关闭,但进程保持活动状态......为什么?
如您所见,有一个连续启动和终止的 AWT-Shutdown 线程,我怎样才能实现相同的行为关闭应用程序进程的 X 按钮?
注释:
System.exit(0);不合适,因为如果有另一个后台运行线程并且我不希望这样,它也会终止应用程序。 MainWindow 类应该关闭并释放它的资源,与使用关闭 MainWindow 实例的 X 按钮关闭应用程序的行为相同,但如果有后台线程正在运行,它不会杀死它们,而是等到它们完成工作...
环境:
- JDK 7
- Eclipse 3.7.1
I have a class developed with windowbuilderpro that i want to close also from a JButton further than with the standard X button on the window, so here the example of the class :
public class MainWindow {
public JFrame frame;
public MainWindow() {
initialize();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Show the main Frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
When i close the window from the X button the window close correctly and the process terminate.
When i close instead from a JButton that have this listener :
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
the frame window close correctly but the process remain alive ... Why ?
As you can see there is an AWT-Shutdown thread that start and terminate continuously, How can i achieve the same behaviour of the X button that close also the application process ?
Notes :
System.exit(0); is not suitable because it terminate the application also if there are another background running thread and i don't want that . The MainWindow class should close and release it's resource, the same behaviour that have closing the application with the X button that close the MainWindow instance but if there are background thread running it doesn't kill they but wait until they finished their work...
Enviroment :
- JDK 7
- Eclipse 3.7.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定您真正需要什么,看起来您再次创建新的 JFrame,不要这样做,创建 JFrame 一次并重新使用此容器,
然后为了可见,您只能调用
frame.setVisible(true );
更舒适的是覆盖 WindowListener,那么就可以控制一些Events
not sure what you really needed, that looks like that you create new JFrame again an again, don't do that, create JFrame once and re-use this Container
then for visibily you can only to call
frame.setVisible(true);
for more Confortable is override WindowListener, then you can control some Events
当激活
x
按钮或Exit
按钮时,此代码中的所有线程都会停止。你有不同的行为吗?All threads in this code stop when either the
x
button or theExit
button are activated. Are you getting different behavior?只需添加一行:
Just add one line: