[英]How to Hide a Java application in Mac programatically?
我正在為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);
}
這就是我得到的:
請參見以下示例。 您可以按照建議使用setVisible(false)
通過Java代碼將其隱藏,然后在用戶單擊擴展塢中的應用程序時調用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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.