java JFrame关闭事件

w3349516 2009-06-27 05:44:32
我重写了 windowClosing 事件

在点退出按钮的时候
我希望直接触发windowClosing事件
也就是JFrame 右上角的关闭按钮

setVisible 和 dispose() 失败

`请大家想想`办法`~
...全文
740 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
w3349516 2009-06-27
  • 打赏
  • 举报
回复
主界面将不可编辑
w3349516 2009-06-27
  • 打赏
  • 举报
回复
大家辛苦了`~~
我的意思没有表达明确 ̄

现在有主界面 jf 和 子界面 jf1 都是 JFrame
子界面显示的时候 主界面不可编辑 就是仿的 JDialog模式

jf1.addWindowListener(new JFrameEnable());
jf1.setVisible(true);
JFrame jf= (JFrame)SwingUtilities.getAncestorOfClass(JFrame.class, ButtonMenu.this);
jf.setEnabled(false);


//界面切换
class JFrameEnable extends WindowAdapter{
public void windowClosing(WindowEvent e) {
//主界面显示
SwingUtilities.getAncestorOfClass(JFrame.class, ButtonMenu.this).setEnabled(true);
}

}



在jf1里有个退出按钮
如果直接 setVisible 和 dispose()
就没有触发 windowClosing 事件
也就无法将主界面显示



彷徨的蜗牛 2009-06-27
  • 打赏
  • 举报
回复
同上
wanglingzhong 2009-06-27
  • 打赏
  • 举报
回复

this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent we){
this.setVisible(flase);
System.exit(0);
}
}
);

当然,既然楼主用了javax.swing了,用setDefaultCloseOperation(EXIT_ON_CLOSE); 就可以!
蜡笔和小新 2009-06-27
  • 打赏
  • 举报
回复
this.setDefaultCloseOperation(EXIT_ON_CLOSE)这是关闭按钮的事件,可以添加它来实现
baetg 2009-06-27
  • 打赏
  • 举报
回复
设置默认关闭事件



对JFrame,JDialog,JInternalFrame有效






setDefaultCloseOperation(EXIT_ON_CLOSE);






可用参数在javax.swing.WindowConstants内(JFrame,JDialog,JInternalFrame都实现了)






DO_NOTHING_ON_CLOSE = 0;



HIDE_ON_CLOSE = 1;



DISPOSE_ON_CLOSE = 2;



EXIT_ON_CLOSE = 3;




2、使用內部類實現窗體關閉事件的捕獲

protected static class CloseHandler

extends WindowAdapter

{

public void windowClosing(final WindowEvent event)

{

//要做的動作

System.exit(0);

}

}

javamode 2009-06-27
  • 打赏
  • 举报
回复
up

62,615

社区成员

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

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