JFrame DO_NOTHING_ON_CLOSE无法在Command + Q上运行

Val*_*eme 4 java swing jframe

我正在创建一个窗口,只需单击一个按钮即可关闭它.
为此,我正在使用
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) .

但问题是当我点击command+ 时我的Mac正在关闭那个窗口q.
这是一个代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 6

Apple提供了一些有用的API.首先来看看Java for OS X v10.6 Update 3和10.5 Update 8发行说明以及此示例以获取更多详细信息.

特别是,你真正感兴趣的是什么

突然终止

OS X v10.6上的Java应用程序现在可以选择加入操作系统突然终止,以加快注销和关闭速度.应用程序可以使用com.apple.eawt.Application类上的enableSuddenTermination()和disableSuddenTermination()方法来提高和降低其突发终止计数.有关突发终止的更多信息,请参阅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);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Java 9

应该注意的是,对于那些幸运地使用它的人来说,Java 9增加了以前com.apple.eawt通过java.awt.DesktopAPI 在API中找到的大部分支持.