JAVA中JFRAME關閉事件及捕獲

1[原创]Swing技巧.1:用最少的代码设置frame的默认关闭事件

设置默认关闭事件

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);

    }

  }

在類中用 this.addWindowListener(new CloseHandler()); 實現捕獲

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/skiwdhwhssh/p/10341768.html