【问题标题】:Mac application menu for java program that also runs on windows也可在 Windows 上运行的 Java 程序的 Mac 应用程序菜单
【发布时间】:2018-07-25 14:18:22
【问题描述】:

我正在开发一个可以在 Windows 和 Mac 机器上运行的程序。我找到了这个link,它解释了如何在 Mac 上实现应用程序菜单。但由于它使用 mac 特定类作为接口,我不确定如何编写该类,因此它也可以在 Windows 中编译。

【问题讨论】:

  • 顺便说一句,不必拥有 OSX JDK(我认为实际上不再支持它,此外,该站点讨论的所有类都已弃用),请执行 System.setProperty("apple.laf.useScreenMenuBar", "true");将您的JMenuBar 放入屏幕顶部的栏中,它应该在哪里

标签: java windows macos swing


【解决方案1】:

我可以推荐 JavaFx。界面看起来像 windows 上的 windows 和 mac 上的 mac。您可以将普通 java 用于在每台机器上运行的所有内容。此外,Jfx 场景构建器可以帮助您设计应用程序。

【讨论】:

  • 实际上,基于 JavaFX 的界面看起来不像 Windows Metro 或 Mac Aqua,它看起来像 Modena(除非你使用像 AquaFX 这样的东西)。但是,如果您使用MenuBar 并将useSystemMenuBarProperty 设置为true,那么JavaFX 将为Java 程序提供“Mac 应用程序菜单”是对的。无论如何,这个问题被标记为 Swing,所以可能不需要 JavaFX。
【解决方案2】:

我应该再用谷歌搜索一下。在old google group 上找到了这门课。只需将其扩展为包含首选项。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class OSXAdapter implements InvocationHandler {

    private OSXQuitListener _quitListener;
    private OSXAboutListener _aboutListener;
    private OSXPreferenceListener _perferenceListener;

    /**
     * creates this adapter, only does stuff when we're on a mac, if it's unable to
     * register the quit adapter, then we throw an exception.
     *
     * @throws ClassNotFoundException
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public OSXAdapter() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

        // get's the os name
        String vers = System.getProperty("os.name").toLowerCase();

        // only attempt to the do the following if we're on a mac
        if (vers.indexOf("mac") != -1) {
            Class quitHandlerClass = Class.forName("com.apple.mrj.MRJQuitHandler");
            Class aboutHandlerClass = Class.forName("com.apple.mrj.MRJAboutHandler");
            Class prefHandlerClass = Class.forName("com.apple.mrj.MRJPrefsHandler");

            Class mrjapputilsClass = Class.forName("com.apple.mrj.MRJApplicationUtils");
            Object methodHandler = Proxy.newProxyInstance(quitHandlerClass.getClassLoader(), new Class[] { quitHandlerClass, aboutHandlerClass, prefHandlerClass }, this);

            Method appUtilsObj = mrjapputilsClass.getMethod("registerQuitHandler", new Class[] { quitHandlerClass });
            appUtilsObj.invoke(null, new Object[] { methodHandler });

            appUtilsObj = mrjapputilsClass.getMethod("registerAboutHandler", new Class[] { aboutHandlerClass });
            appUtilsObj.invoke(null, new Object[] { methodHandler });

            appUtilsObj = mrjapputilsClass.getMethod("registerPrefsHandler", new Class[] { prefHandlerClass });
            appUtilsObj.invoke(null, new Object[] { methodHandler });

        }
    }

    /**
     * registers an about dialog. When the os x system fires the event which
     * triggers an about class
     * 
     * @param listener
     */
    public void setAboutListener(OSXAboutListener listener) {
        _aboutListener = listener;
    }

    /**
     * registers an preference listener. When the os x fires the preference event this will be
     * fired.
     * 
     * @param listener
     */
    public void setPerferenceListener(OSXPreferenceListener listener) {
        _perferenceListener = listener;
    }

    /**
     * register an quit listener. When the os x fires the quit event this will be
     * fired.
     *
     * @param listener
     */
    public void setQuitListener(OSXQuitListener listener) {
        _quitListener = listener;
    }

    /**
     * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
     *      java.lang.reflect.Method, java.lang.Object[])
     */
    public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
        if (meth.getName().equals("handleQuit")) {
            if (null != _quitListener) {
                _quitListener.handleQuit();
            }
        } else if (meth.getName().equals("handleAbout")) {
            if (null != _aboutListener) {
                _aboutListener.handleAbout();
            }
        } else if (meth.getName().equals("handlePrefs")) {
            if (null != _perferenceListener) {
                _perferenceListener.handlePrefs();
            }
        }

        return null;
    }

    /**
    * listener which listens to the about event from the os x
    * system
    *
    * @author Chris Shorrock
    */
    public interface OSXAboutListener {

    /**
    * handles the about display event.
    */
    public void handleAbout();

    }



    /**
    * this listener is fired when the os x system quits
    *
    * @author Chris Shorrock
    */
    public interface OSXQuitListener {

    /**
    * this method is called when os x tells this application
    * to quit.
    */
    public void handleQuit();
    }



    /**
    * this listener is fired when the os x system fires preferences
    *
    * @author Chris Shorrock
    */
    public interface OSXPreferenceListener {

    /**
    * this method is called when os x tells this application
    * to open preferences.
    */
    public void handlePrefs();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多