关于退出JFrame窗口是询问是否要退出的问题

nainai007 2011-06-03 08:14:39
我的代码是这样的
this.setTitle("图书管理系统--" + adminmsg.getAdminName());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
int option = JOptionPane.showConfirmDialog(null,"是否退出","提示",JOptionPane.YES_NO_OPTION);
if(option == 0)
System.exit(0);
else
return;
}
});

不管我点击“是”还是“否”程序都退出

求解为什么
...全文
147 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2011-06-04
  • 打赏
  • 举报
回复
modify your action event function
public void windowClosing(WindowEvent e)
{
int option = JOptionPane.showConfirmDialog(null,"是否退出","提示",JOptionPane.YES_NO_OPTION);
if(option == 0)
//System.exit(0);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//return;
}
});

TwilightSun 2011-06-04
  • 打赏
  • 举报
回复
汗,果然是HIDE,没仔细看,实在是抱歉,误人了……
WuBill 2011-06-04
  • 打赏
  • 举报
回复
标记,学习
riyuezhizhi 2011-06-04
  • 打赏
  • 举报
回复
同意用this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
茫茫大海 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mankeyaq 的回复:]
默认情况下是DisposeOnClose
[/Quote]
这位仁兄,这个不对吧!别误导了lz,JFrame源码中是:
private int defaultCloseOperation = HIDE_ON_CLOSE;
茫茫大海 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mankeyaq 的回复:]
如楼上
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
默认情况下是DisposeOnClose,也就是当关闭时就释放所有窗口所占用的资源,如果所有的窗口资源被释放,程序退出

按照楼上的改法,你就可以自己处理关闭动作了,Java不会自主做任何事
[/Quote]
默认是DisposeOnClose?那JFrame源码中怎么是private int defaultCloseOperation = HIDE_ON_CLOSE;
TwilightSun 2011-06-03
  • 打赏
  • 举报
回复
如楼上
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
默认情况下是DisposeOnClose,也就是当关闭时就释放所有窗口所占用的资源,如果所有的窗口资源被释放,程序退出

按照楼上的改法,你就可以自己处理关闭动作了,Java不会自主做任何事
茫茫大海 2011-06-03
  • 打赏
  • 举报
回复
JFrame的源码中有下面一行:
private int defaultCloseOperation = HIDE_ON_CLOSE;
通过这句代码可以看出,JFrame的默认关闭时执行的操作为隐藏窗口。
下面为JFrame处理窗口事件的代码:

protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
case HIDE_ON_CLOSE:
setVisible(false);
break;
case DISPOSE_ON_CLOSE:
dispose();
break;
case DO_NOTHING_ON_CLOSE:
default:
break;
case EXIT_ON_CLOSE:
// This needs to match the checkExit call in
// setDefaultCloseOperation
System.exit(0);
break;
}
}
}

通过上面的代码可以看出JFrame就是根据defaultCloseOperation来决定对窗口的操作的。
因此可以在开始的时候设置defaultCloseOperation为DO_NOTHING_ON_CLOSE。这样当关闭窗口的时候就不执行处理了,然后可以在你加的事件中关闭窗口。
下面的代码,可以实现这个需求:

package T;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test extends JFrame {
public Test() {
this.setTitle("图书管理系统");
this.setSize(800, 600);
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(null, "是否退出", "提示",
JOptionPane.YES_NO_OPTION);
if (option == 0) {
System.exit(0);
}
}
});
}

public static void main(String[] args) {
new Test();
}
}
huntor 2011-06-03
  • 打赏
  • 举报
回复
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
nainai007 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhouyuqwert 的回复:]

...你return又不是阻止close事件
[/Quote]

那怎么做才能阻止close时间呢
nainai007 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gentleboy2009 的回复:]

Java code

this.setTitle("图书管理系统--" + adminmsg.getAdminName());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent……
[/Quote]

还是一样,不行
jinancf 2011-06-03
  • 打赏
  • 举报
回复
楼上的,省掉else不行吗?
gentleboy2009 2011-06-03
  • 打赏
  • 举报
回复

this.setTitle("图书管理系统--" + adminmsg.getAdminName());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
int option = JOptionPane.showConfirmDialog(null,"是否退出","提示",JOptionPane.YES_NO_OPTION);
if(option == 0)
System.exit(0);
else
;
}
});
阳明 to life 2011-06-03
  • 打赏
  • 举报
回复
...你return又不是阻止close事件

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧