Access Mac OS X Application Menu from Java

2019-02-23 12:31发布

I'm writing a Java application designed for all platforms, but specifically, I am working on OS X appearance and integration (I use NetBeans on my MacBook Pro). I'd like to access the application's menu, i.e. the bolded menu named after the application. I want to register listeners for the About and Quit items, as well as show the Preferences item. I wouldn't mind adding a few more items of my own, too.

So, how do I do this? I have seen previous posts refer to OSXAdapter, but the geniuses at Apple decided to remove it from their library (or rename it ambiguously) because all links redirect to the main page, and all my searching has been fruitless. I've also seen a MacOSAppAdapter class, but I am unsure how to use it. All the importing and new classes and hierarchies is bit confusing.

EDIT:

This is what I did to tie into the About, Preferences, and Quit items:

I made a new class, MacOSXAboutHandler, that extends

com.apple.eawt.Application

Its constructor simply invokes

setAboutHandler(AboutHandler aH)

and I provide it with my own listener that extends

AboutHandler

In my main class I determine if I am running on a Mac using

System.getProperty("os.name").contains("mac")

If this is true, then I simply create a new instance of MacOSXAboutHandler. The constructor adds my handler, and whenever the application is run (or even tested within NetBeans), clicking the About... item on the application's bolded menu executes the code I specified in my AboutHandler.

The same is done for preferences and quit, simply replacing occurrences of "about" with the appropriate action. All these handlers are written as any other Java listener would be.

标签: java macos menu
1条回答
Juvenile、少年°
2楼-- · 2019-02-23 13:15

You might want to have a look at Bringing your Java Application to Mac OS X and (more importantly) Bringing your Java Application to Mac OS X Part 2 and Bringing your Java Application to Mac OS X Part 3

You might Java System Property Reference for Mac of use

You may want to take a look at Apple's Java 6 Extensions API, from my brief reading, it would appear that you basically want to use the default instance if com.apple.eawt.Application and supply the handlers you need (such as setAboutHandler).

You may also want to read through The Java on Mac OS X About, Quit and Preferences menu items and events article, which should provide some more additional hints.

查看更多
登录 后发表回答