捕获,首选项和退出菜单项

alt*_*ser 6 java cocoa swt menu

我正在使用当前版本的SWT来构建我的应用程序,我想在Mac OS X(Yosemite)下运行它.
我现在的问题是,我无法捕获自动添加到我的应用程序中的"关于","首选项"和"退出"菜单项的点击.
我已经搜索了很多,发现以下课程对我来说非常有帮助http://www.transparentech.com/files/CocoaUIEnhancer.java.

这是我的代码来初始化它:

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class Test {
  private Display display;
  private Shell shell;

  public Test(Display display) {
    this.display = display;
    initUI();
  }

  public void open() {
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }

  private void initUI() {
    shell = new Shell(display);
    shell.setSize(808, 599);
    shell.setText("Test");

    AboutHandler aboutHandler = new AboutHandler();
    PreferencesHandler preferencesHandler = new PreferencesHandler();
    QuitHandler quitHandler = new QuitHandler();

    CocoaUIEnhancer uienhancer = new CocoaUIEnhancer("Test");
    uienhancer.hookApplicationMenu(display, quitHandler, aboutHandler, preferencesHandler);
  }

  private class AboutHandler implements Listener {
    public void handleEvent(Event e) {
    }
  }

  private class PreferencesHandler implements Listener {
    public void handleEvent(Event e) {
    }
  }

  private class QuitHandler implements Listener {
    public void handleEvent(Event e) {
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以编译它没有任何错误,但如果我启动该程序,那么我将得到以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: actionProc
  at org.eclipse.swt.internal.Callback.bind(Native Method)
  at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
  at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
  at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
  at CocoaUIEnhancer.initialize(CocoaUIEnhancer.java:124)
  at CocoaUIEnhancer.hookApplicationMenu(CocoaUIEnhancer.java:92)
  at Test.initUI(Test.java:50)
  at Test.<init>(Test.java:18)
Run Code Online (Sandbox Code Playgroud)

这可能是本机库中的一个错误,但我无法弄明白!

Baz*_*Baz 8

我根本CocoaUIEnhancer没用,因为它也引起了问题.

所以这就是我最终在我的应用程序中做的事情:

/**
 * Convenience method that takes care of special menu items (About, Preferences, Quit)
 *
 * @param name     The name of the menu item
 * @param parent   The parent {@link Menu}
 * @param listener The {@link Listener} to add to the item
 * @param id       The <code>SWT.ID_*</code> id
 */
private void addMenuItem(String name, Menu parent, Listener listener, int id)
{
    if (OSUtils.isMac())
    {
        Menu systemMenu = Display.getDefault().getSystemMenu();

        for (MenuItem systemItem : systemMenu.getItems())
        {
            if (systemItem.getID() == id)
            {
                systemItem.addListener(SWT.Selection, listener);
                return;
            }
        }
    }

    /* We get here if we're not running on a Mac, or if we're running on a Mac, but the menu item with the given id hasn't been found */
    MenuItem item = new MenuItem(parent, SWT.NONE);
    item.setText(name);
    item.addListener(SWT.Selection, listener);
}
Run Code Online (Sandbox Code Playgroud)

只需用SWT.ID_PREFERENCES,SWT.ID_ABOUTSWT.ID_QUIT分别调用它.交出后备菜单项名称,后备MenuListener您要添加到菜单项的实际值.

例如:

addMenuItem("Quit", myMenu, new Listener()
{
    @Override
    public void handleEvent(Event event)
    {
        // Close database connection for example
    }
}, SWT.ID_QUIT);
Run Code Online (Sandbox Code Playgroud)