我重写了 windowClosing 事件在点退出按钮的时候 
我希望直接触发windowClosing事件
也就是JFrame 右上角的关闭按钮setVisible 和 dispose() 失败`请大家想想`办法`~ 

解决方案 »

  1.   

    设置默认关闭事件对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 CloseHandlerextends WindowAdapter{public void windowClosing(final WindowEvent event){//要做的動作 System.exit(0);}}
      

  2.   

    this.setDefaultCloseOperation(EXIT_ON_CLOSE)这是关闭按钮的事件,可以添加它来实现
      

  3.   


    this.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    this.setVisible(flase);
    System.exit(0);
    }
    }
    );
    当然,既然楼主用了javax.swing了,用setDefaultCloseOperation(EXIT_ON_CLOSE); 就可以!
      

  4.   

    大家辛苦了`~~
    我的意思没有表达明确 ̄现在有主界面 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 事件 
    也就无法将主界面显示