gpt4 book ai didi

java - mac 中的 processWindowEvent

转载 作者:可可西里 更新时间:2023-11-01 10:03:27 28 4
gpt4 key购买 nike

我有一个 java swing 应用程序,它有一个 processWindowEvent 方法。下面是片段

@Override
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//exit application with proper error message
}
}

现在,当我的 swing 应用程序在 Windows 中启动时。

  1. 在 Swing UI 中用十字关闭。 ==>显示正确的错误信息
  2. 从任务栏关闭应用程序 ==>显示正确的错误消息

但现在如果同样的步骤在 mac 中完成。

  1. 在 Swing UI 中用十字关闭。 ==>显示正确的错误信息
  2. 从任务栏关闭应用程序 ==>不在上面的方法中。所以没有合适的消息。

我想知道当从任务栏(停靠栏)关闭 java swing 应用程序时将在 mac 中调用的默认方法是什么

最佳答案

一个没有 com.apple.eawt.*

的世界

你需要看java.awt.Desktop相反。

例如……

Desktop.getDesktop().setQuitHandler(new QuitHandler() {
@Override
public void handleQuitRequestWith(QuitEvent e, QuitResponse response) {
// Do some stuff
//response.cancelQuit();
//response.performQuit();
}
});
Desktop.getDesktop().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);

原始答案

欢迎来到“Apple doing everything differently”的精彩世界

基本上发生的事情是,当您“退出”该程序时,Apple 正在调用 System.exit(0),这与您使用 CMD+Q

现在,Apple 提供了一个 API,它提供了一些功能,您可以使用它来“配置”您的应用程序与 MacOS 并执行一些 Apple 独有的功能,问题是,查找代码非常痛苦关于和使用的有用信息。

您要找的是com.apple.eawt.ApplictionsetQuitStrategy .这默认调用 System.exit(0),但您可以将其更改为“关闭所有窗口”。

在这种情况下,它将允许您捕获 WindowEvent 并执行您想执行的任何操作

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("Closing");
System.exit(0);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

try {
Class quitStrategies = Class.forName("com.apple.eawt.QuitStrategy");
Object quitStrategy = null;
for (Object o : quitStrategies.getEnumConstants()) {
if ("CLOSE_ALL_WINDOWS".equals(o.toString())) {
quitStrategy = o;
}
}
if (quitStrategy != null) {
Class appClass = Class.forName("com.apple.eawt.Application");
Class params[] = new Class[]{};

Method getApplication = appClass.getMethod("getApplication", params);
Object application = getApplication.invoke(appClass);
Method setQuitStrategy = application.getClass().getMethod("setQuitStrategy", quitStrategies);
setQuitStrategy.invoke(application, quitStrategy);
}

} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {
}
}

我的一般建议是,构建一个漂亮的“Mac”实用程序类,它封装了您想要使用的功能并调用它。

另请注意,此功能可能会在未来的版本中突然消失。

需要注意的是,如果你打算有一个“one for all”的应用程序,你将需要使用反射,因为所需的API在标准API中是不可用的,但是如果你想做一个“Apple”只发布,你应该看看this有关如何在 MacOS 上编译代码的更多信息,因为使用...

Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);

编写和理解要容易得多

关于java - mac 中的 processWindowEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com