是 否 确认和窗口事件
我有以下代码片段:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(Bookstore.this, "Are you sure to quit?", "Confirm", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// release connection resource
if (bookstoreConnection != null) {
bookstoreConnection.closeConnection();
}
// JFrame handles close request based on the property
// set by invoking the setDefaultCloseOperation(...)
Bookstore.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
});
每当我按下右上角的 [x] 按钮时,无论我选择什么选项,框架都会消失。在这种情况下,我想知道如果我选择“否”选项如何保留框架窗口。谢谢你!
I have following code snippet:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(Bookstore.this, "Are you sure to quit?", "Confirm", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// release connection resource
if (bookstoreConnection != null) {
bookstoreConnection.closeConnection();
}
// JFrame handles close request based on the property
// set by invoking the setDefaultCloseOperation(...)
Bookstore.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
});
and whenever i press [x] button at upper right corner the frame disappears regardless of whatever option i chose. In this case I want to know how to retain frame window if i choose NO option. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这个 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) 作为 JFrame 子类的默认关闭操作(如果您没有显式添加 setDefaultCloseOperation(.. .) 与另一个值)
http://download.oracle.com /javase/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)
Use this
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
as the default close operation on your JFrame subclass (I don't remember if this is the default if you don't explicitly add thesetDefaultCloseOperation(...)
with another value)http://download.oracle.com/javase/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)