为 setDefaultCloseOperation 创建自定义操作?

发布于 2024-11-08 17:44:15 字数 113 浏览 0 评论 0原文

我想让我的 Java 应用程序在按下“关闭”十字按钮时调用我自己的自定义函数。据我所知,可能没有办法,因为 setDefaultCloseOperation 根本没有重载。

知道如何实现这一点吗?

I want to make my Java Application to call my own custom made function when the "close" cross button is pressed. as far as i see there may be no way since setDefaultCloseOperation has no overloads at all.

Any idea how this can be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衬遮猫 2024-11-15 17:44:15

也许是这个,但在此之前请阅读 Howard 发布的教程 WindowListener ,有一些/其他选项

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?", 
             "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           System.exit(0);
        }
    }
};
frame.addWindowListener(exitListener);

maybe this one, but before that read tutorial WindowListener posted by Howard, there are some/another options

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?", 
             "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           System.exit(0);
        }
    }
};
frame.addWindowListener(exitListener);
从﹋此江山别 2024-11-15 17:44:15

上述所有建议都是正确的,因为您需要使用 WindowListener。

然而,所有答案也不完整,因为他们忘记提及您可能还想添加:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(...);

这将允许您的代码完全控制窗口关闭过程,因为窗口不会自动关闭,除非您告诉它(通常通过在框架上使用 dispose() 方法)。这允许您提示用户确认是否关闭窗口。

关闭应用程序有一个简单的 API,允许您创建关闭窗口时执行的简单操作。它为您管理关闭操作和窗口侦听器代码。

All the above suggestions are correct in that you need to use a WindowListener.

However all the answer are also incomplete in that they forget to mention that you may also want to add:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(...);

This will allow your code to take full control of the window closing process as the window will not automatically close unless you tell it to (generally by using the dispose() method on the frame). This allow you to promt the user for a confirmation to close the window or not.

Closing an Application has a simple API that allows you to create a simple Action that is executed when the window is closed. It manages the close operation and the window listener code for you.

黑凤梨 2024-11-15 17:44:15

您可以为框架添加窗口侦听器

frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
     onExit();
   }
  });

...
public void onExit() {
  System.err.println("Exit");
  System.exit(0);
}

You can add a window listener for the frame:

frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
     onExit();
   }
  });

...
public void onExit() {
  System.err.println("Exit");
  System.exit(0);
}
你不是我要的菜∠ 2024-11-15 17:44:15

您可以将 WindowListener 添加到框架中(请参阅例如 这里)。

You can add a WindowListener to your frame (see e.g. here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文