【问题标题】:JFrame DO_NOTHING_ON_CLOSE is not working on Command + QJFrame DO_NOTHING_ON_CLOSE 不适用于 Command + Q
【发布时间】:2017-11-29 05:55:39
【问题描述】:

我正在创建一个窗口,我需要通过仅单击按钮来关闭它。
为此,我正在使用
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) 方法。

但问题是当我单击 command + q 时,我的 Mac 正在关闭该窗口。
这是一个代码:

package screen.saver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

public class ScreenSaver {

    public static void main(final String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        JFrame screenSaverFrame = new JFrame();
        screenSaverFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        screenSaverFrame.setUndecorated(true);
        screenSaverFrame.setResizable(false);

        JButton closeWindow = new JButton("Close window");
        closeWindow.addActionListener(e -> {
            screenSaverFrame.dispose();
        });

        screenSaverFrame.add(closeWindow, BorderLayout.CENTER);
        screenSaverFrame.validate();
        GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .setFullScreenWindow(screenSaverFrame);
    }
}

【问题讨论】:

  • “但问题是我的 Mac 在我单击 command + q 时正在关闭该窗口” - 是的,它就是这样工作的,CMD+Q 正在终止进程跨度>
  • 但是我不知道用command + q 来关闭它。我只想通过按钮关闭它
  • 为什么?你的用户对你做了什么?
  • 我只想每 30 分钟锁定一次屏幕以便休息一下。
  • 我建议查看 `Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);` 和 Application.getApplication().disableSuddenTermination(); - 更多详情请参阅 this example

标签: java swing jframe


【解决方案1】:

Apple 提供了几个有用的 API。首先查看Java for OS X v10.6 Update 3 and 10.5 Update 8 Release Notes 和this example 了解更多详情。

特别是你真正感兴趣的东西

突然终止

OS X v10.6 上的 Java 应用程序现在可以选择被操作系统突然终止以加速注销和关闭。 应用程序可以通过 enableSuddenTermination() 和 disableSuddenTermination() 方法 com.apple.eawt.Application 类。更多关于突然的信息 终止在 NSProcessInfo 类参考中可用。 (5756768)

默认退出操作

应用程序现在可以请求 eAWT 将 WindowClosing 事件发送到所有打开的窗口,而不是调用 System.exit(0) 当用户从应用程序菜单中选择退出时。经过 将 apple.eawt.quitStrategy 系统属性设置为 CLOSE_ALL_WINDOWS,eAWT 会发送一个关闭事件到每个窗口 从后到前的顺序 (3198576)。

这应该可以让您更好地控制应用程序的终止方式,例如...

import com.apple.eawt.Application;
import com.apple.eawt.FullScreenUtilities;
import com.apple.eawt.QuitStrategy;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {

    public static void main(String[] args) {
        Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
        Application.getApplication().disableSuddenTermination();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setUndecorated(true);
                FullScreenUtilities.setWindowCanFullScreen(frame, true);
                frame.setLayout(new GridBagLayout());
                JButton close = new JButton("Close me");
                close.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                    }
                });
                frame.add(close);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("Closed");
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("Closing");
                    }

                });
                frame.setLocationRelativeTo(null);
                //Application.getApplication().requestToggleFullScreen(frame);
                //frame.setVisible(true);
                GraphicsEnvironment.getLocalGraphicsEnvironment()
                                .getDefaultScreenDevice()
                                .setFullScreenWindow(frame);
            }
        });
    }
}

当按下 CMD+Q 时,windowClosing 事件被触发,这意味着终止应用程序的唯一方法是点击关闭按钮

Java 9

需要注意的是,对于那些有幸使用它的人来说,Java 9 通过java.awt.Desktop API 将之前在com.apple.eawt 中发现的大部分支持添加到了API 中

【讨论】:

  • 对了,其他操作系统怎么样
  • 如果你想在其他操作系统上运行它,你需要使用反射来使它工作(否则它会崩溃)。使用 DO_NOTHING“应该”在 Windows 上工作,但您必须对其进行测试
猜你喜欢
  • 2022-11-09
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2020-10-31
  • 1970-01-01
  • 2012-05-21
相关资源
最近更新 更多