gpt4 book ai didi

java - 如何以编程方式在 Mac 中隐藏 Java 应用程序?

转载 作者:行者123 更新时间:2023-11-29 08:53:00 28 4
gpt4 key购买 nike

我正在为 mac 制作一个 java 应用程序。应用程序必须具有“自动隐藏”等同于“Command+H”快捷方式的能力。我正在尝试使用 JFrame 中的 setVisible(False) 来实现。但它不起作用。我该怎么做?

这是可能的代码:

void hide(){
setNormalScreen(); //disable fullscreen mode
//this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(false);
this.setState(JFrame.ICONIFIED);
}

这就是我得到的: The main window

最佳答案

请看下面的例子。您可以按照您的建议使用 setVisible(false) 通过 Java 代码隐藏它,然后当用户单击 dock 中的应用程序时将调用 appReOpened() 事件。发生这种情况时,您只需调用 setVisible(true)。这应该模仿 Command-H 行为。

另请参阅下面的注释代码以获得更丑陋的解决方案。

public class Test extends JFrame implements ActionListener, com.apple.eawt.AppReOpenedListener {

public static void main(String[] args) {
Test frame = new Test();
JButton test = new JButton("test");
test.addActionListener(frame);

com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
app.addAppEventListener(frame);

frame.getContentPane().add(test);
frame.pack();
frame.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);

// try {
// Robot robot = new Robot();
// robot.keyPress(KeyEvent.VK_META);
// robot.keyPress(KeyEvent.VK_H);
// robot.keyRelease(KeyEvent.VK_H);
// robot.keyRelease(KeyEvent.VK_META);
// } catch (AWTException ex) {
// // TODO Auto-generated catch block
// ex.printStackTrace();
// }
}

@Override
public void appReOpened(AppReOpenedEvent arg0) {
setVisible(true);
}
}

关于java - 如何以编程方式在 Mac 中隐藏 Java 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764824/

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