开发者

In order to Macify a Java app, to catch the about event, do I have to implement a menu?

The macify Notepad example is the only example of 'macification' I could find, and it's all fine and dandy I guess, apart from the menu which completely screws up my layout. I'm very, very new at Java so I follow examples like these to the letter, but this menu really has to go. Is the开发者_Python百科re a way to catch the 'about' stuff without a menu? After all, this about thing on Mac OSes seems to be there even without one. Standard procedure, etc. I don't have a Mac to test the code, so trial and error is severely limited...

How is this done?


Bit of a necro-post but it's code I use all the time. It's complicated and uses reflection to avoid throwing errors on non-Mac systems, however.

In the initialization of your app or as a static code block:

if (System.getProperty("os.name").contains("Mac")) {
    try {
        Object app = Class.forName("com.apple.eawt.Application")
            .getMethod("getApplication")
            .invoke(null);

        Object al = Proxy.newProxyInstance(
            Class.forName("com.apple.eawt.AboutHandler").getClassLoader(),
            new Class[]{Class.forName("com.apple.eawt.AboutHandler")},
            new AboutListener()
        );

        app.getClass()
            .getMethod("setAboutHandler", Class.forName("com.apple.eawt.AboutHandler"))
            .invoke(app, al);
    }
    catch (Exception e) {
        //fail quietly
    }
}

At the bottom of the source file after the last curly brace

public class AboutListener implements InvocationHandler {

    public Object invoke(Object proxy, Method method, Object[] args) {
        //Show About Dialog
        return null;
    }
}

When releasing a full application in java things like this make nice small touches. This should be mostly copy-and-paste-able but you will need to add a line or two to display an about box. If you need to test really badly use web-start, dropbox public links, and a neighborhood Apple Store.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜