简体   繁体   English

Java将应用程序置于最前端(OSX)

[英]Java bring application to front (OSX)

I have been searching all over the internet for this, going from stack overflow answer to stack overflow answer, Trying rococoa, or Class.forName ("com.apple.cocoa.application.NSApplication"); 我一直在互联网上搜索这个问题,从堆栈溢出答案到堆栈溢出答案,尝试rococoa或Class.forName ("com.apple.cocoa.application.NSApplication"); amongst other things. 除其他外。

The bottom line of the matter is, I cannot, for the love of god, figure out how to get my Java application to focus its self on OSX! 事情的底线是,为了上帝的爱,我不能弄清楚如何使我的Java应用程序专注于OSX!

Let me be clear: My application has no windows (It will in the future, but sometimes it may not have any windows at all). 让我清楚一点:我的应用程序没有窗口(将来会出现,但有时可能根本没有窗口)。 I need a way to focus my application that does not rely on windows. 我需要一种方法来使我的应用程序不依赖于Windows。

Having not found anything, I desperately decided to try a solution that relied on there being a window: 没找到任何东西,我拼命决定尝试一种依赖于有一个窗口的解决方案:

private static void BringSelfToFocus()
{
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            Window window = new JFrame("Test");
            window.toFront();
            window.repaint();
        }
    });
}

That, however, like every other futile attempt of mine, failed. 但是,这与我其他所有徒劳的尝试一样都失败了。

So, yes, while this is technically a duplicate question, I have tried every single other answer I could find, and for whatever reason, none of them worked. 因此,是的,尽管从技术上讲这是一个重复的问题,但我尝试了所有其他可以找到的答案,无论出于何种原因,它们都没有起作用。

Can anyone please lend a helping hand on this matter? 任何人都可以在这件事上伸出援手吗? Thankyou. 谢谢。

-Georges -Georges

Well! 好! I had one final idea, and it worked! 我有一个最后的想法,它奏效了! I used Applescript. 我用了Applescript。

private static void BringSelfToFocus()
{
    AppleScript("tell me to activate");
}


private static String AppleScript(String script)
{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");

    if (engine != null) {
        try
        {
            return (String)engine.eval(script);
        }
        catch (ScriptException e)
        {
            e.printStackTrace();
        }
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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