繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM