【问题标题】:How to Hide a Java application in Mac programatically?如何以编程方式在 Mac 中隐藏 Java 应用程序?
【发布时间】:2014-03-12 22:56:50
【问题描述】:

我正在为 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);
}

这就是我得到的:

【问题讨论】:

  • 将您的 JAR 包装在 Mac OS X 应用程序包中,讨论过 herehere

标签: java macos swing


【解决方案1】:

请参见下面的示例。您可以按照您的建议使用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);
    }
}

【讨论】:

  • 谢谢。经过一番研究,我意识到如果应用程序处于全屏状态,setVisible 不起作用,所以我必须等到应用程序恢复正常,然后我才能做机器人的伎俩
  • 是否有可能当它隐藏到停靠并且在最小化期间​​在这里实现的计时器将完美工作,或者计时器不会卡住。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2018-11-17
  • 2018-08-12
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
相关资源
最近更新 更多