簡體   English   中英

同樣適用於Windows的Java程序的Mac應用程序菜單

[英]Mac application menu for java program that also runs on windows

我正在開發一個在Windows和Mac計算機上均可使用的程序。 我發現此鏈接說明了如何在Mac上實現應用程序菜單。 但是由於它使用Mac特定的類作為接口,所以我不確定如何編寫該類,因此它也可以在Windows中編譯。

我可以推薦JavaFx。 該界面在Windows上看起來像Windows,在Mac上看起來像mac。 您可以將普通的Java用於可在每台計算機上使用的所有功能。 另外,“ Jfx場景”構建器可以幫助您設計應用程序。

我應該在Google上搜索更多一點。 在一個舊的Google網上論壇上找到了此類。 只需擴展它以包括首選項。

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM