让JFrame关闭的正确方法是什么,就像用户按下X关闭按钮,或按下Alt+F4(在Windows上)一样?
我的默认关闭操作设置为我想要的方式,通过:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
它完全符合我对上述控件的要求。这个问题与此无关。
我真正想做的是使GUI的行为与按下X关闭按钮所引起的行为相同。
假设我要扩展WindowAdaptor,然后通过addWindowListener()将适配器的一个实例添加为侦听器。我希望看到通过windowDeactivated()、windowClosing()和windowClosed()的调用序列与X close按钮相同。可以说,与其说是撕破窗户,不如说是让它自己撕毁。
将问题正文中的内容作为CW答案。
想分享的成果,主要来源于关注camickr的链接。基本上我需要抛出一个WindowEvent。应用程序事件队列上的WINDOW_CLOSING。下面是解决方案的概要
// closing down the window makes sense as a method, so here are
// the salient parts of what happens with the JFrame extending class ..
public class FooWindow extends JFrame {
public FooWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(5, 5, 400, 300); // yeah yeah, this is an example ;P
setVisible(true);
}
public void pullThePlug() {
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
}
// Here's how that would be employed from elsewhere -
// someplace the window gets created ..
FooWindow fooey = new FooWindow();
...
// and someplace else, you can close it thusly
fooey.pullThePlug();
本示例演示了如何实现确认窗口关闭操作。
窗口有一个窗口适配器,它将默认关闭操作切换为exit_on_close或DO_NOTHING_ON_CLOSE,这取决于您在OptionDialog中的答案。
ConfirmedCloseWindow的closeWindow方法触发关闭窗口事件,可以在任何地方使用,即作为菜单项的动作
public class WindowConfirmedCloseAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Object options[] = {"Yes", "No"};
int close = JOptionPane.showOptionDialog(e.getComponent(),
"Really want to close this application?\n", "Attention",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
options,
null);
if(close == JOptionPane.YES_OPTION) {
((JFrame)e.getSource()).setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
} else {
((JFrame)e.getSource()).setDefaultCloseOperation(
JFrame.DO_NOTHING_ON_CLOSE);
}
}
}
public class ConfirmedCloseWindow extends JFrame {
public ConfirmedCloseWindow() {
addWindowListener(new WindowConfirmedCloseAdapter());
}
private void closeWindow() {
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
}
如果你所说的Alt-F4或X是指“立即退出应用程序,而不考虑其他窗口或线程正在运行”,那么System.exit(…)将以一种非常突然的、暴力的、可能有问题的方式执行你想要的操作。
如果Alt-F4或X你的意思是隐藏窗口,那么frame.setVisible(false)是你“关闭”窗口的方式。该窗口将继续消耗资源/内存,但可以很快再次可见。
如果Alt-F4或X的意思是隐藏窗口并处理它所消耗的任何资源,那么frame.dispose()就是你“关闭”窗口的方式。如果该帧是最后一个可见窗口,并且没有其他非守护进程线程正在运行,则程序将退出。如果你再次显示窗口,它将不得不重新初始化所有的本机资源(图形缓冲区,窗口句柄等)。
Dispose()可能最接近于您真正想要的行为。如果你的应用程序有多个窗口打开,你想Alt-F4或X退出应用程序或只是关闭活动窗口?
关于窗口侦听器的Java Swing教程可能会帮助您澄清一些事情。
退出Java运行过程非常简单,基本上你只需要做两件简单的事情:
在应用程序的退出点调用java方法System.exit(…)
例如,如果你的应用程序是基于框架的,你可以添加监听器WindowAdapter,并在它的方法windowClosing(WindowEvent e)中调用System.exit(…)。
注意:你必须调用System.exit(…)否则你的程序会出错。
避免意外的java异常,以确保始终可以调用exit方法。
如果您在正确的位置添加System.exit(…),但这并不意味着该方法可以始终被调用,因为意外的java异常可能会阻止该方法被调用。
这与你的编程技能密切相关。
**下面是一个最简单的示例(基于JFrame),它向您展示了如何调用退出方法
import java.awt.event.*;
import javax.swing.*;
public class ExitApp extends JFrame
{
public ExitApp()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0); //calling the method is a must
}
});
}
public static void main(String[] args)
{
ExitApp app=new ExitApp();
app.setBounds(133,100,532,400);
app.setVisible(true);
}
}