【问题标题】:How to have a menu on the mac top toolbar with JavaFX?如何在带有 JavaFX 的 mac 顶部工具栏上有一个菜单?
【发布时间】:2019-04-07 04:31:56
【问题描述】:

在 Mac OS 上,在后台运行的应用程序有时会将其图标附加到屏幕右上角的 gui 或菜单上。我相信它类似于 Windows 右下角的内容。 但是,我也希望将它用于我的 JavaFX 应用程序。而且我不知道如何用谷歌搜索。我只找到了 JavaFX 的 MenuBar,不幸的是这不是我想要的。

【问题讨论】:

标签: java macos javafx


【解决方案1】:
public class SystemTray {
    private static final Logger logger = LogManager.getLogger(Main.class);
    public static java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();              // set up a system tray icon.
    public static TrayIcon trayIcon;
    public static Image trayIconImage;
    public static MenuItem startRecording;
    public static MenuItem stopRecording;
    public static MenuItem playRecording;
    public static MenuItem pauseRecording;
    public static MenuItem startOver;
    public static MenuItem exitItem;
    final static PopupMenu popup=new PopupMenu();
    public static boolean windows = Main.checkOS();

    public SystemTray() {
    }

    public static void addAppToTray() {
        try {
            // ensure awt toolkit is initialized.
            Toolkit.getDefaultToolkit();
            URL IconPath;
            // app requires system tray support, just exit if there is no support.
            if (!java.awt.SystemTray.isSupported()) {
                System.out.println("No system tray support, application exiting.");
                return;
                //Platform.exit();
            }

            if (windows) {
            File file = new File("./resources/main/cogwheel-win.png");
           // IconPath =this.getClass().getResource("cogwheel-windows.png");
            IconPath =file.toURI().toURL();

            }
            else {
            File file = new File("./resources/main/cogwheel-mac.png");
           // IconPath =this.getClass().getResource("cogwheel-mac.png");
            IconPath =file.toURI().toURL();
            }

            logger.info(IconPath.getFile().toString());
            trayIconImage = ImageIO.read(IconPath);
            trayIcon = new TrayIcon(trayIconImage);
            startRecording = new MenuItem("Start Recording (Shift+Space)");
            stopRecording = new MenuItem("Stop Recording (Shift+Space)");
            playRecording = new MenuItem("Play Recording (Shift+P)");
            pauseRecording = new MenuItem("Pause Recording (Shift+P)");
            startOver = new MenuItem("Start Over (Shift+R)");
            //openItem.addActionListener(event -> Platform.runLater(this::showStage));

            // and select the exit option, this will shutdown JavaFX and remove the
            // tray icon (removing the tray icon will also shut down AWT).

            exitItem = new MenuItem("Quit CAD");
            exitItem.addActionListener(event -> {
                Platform.exit();
                tray.remove(trayIcon);
            });

            // setup the popup menu for the application.
            popup.add(startRecording);
            popup.add(stopRecording);
            popup.addSeparator();
            popup.add(playRecording);
            popup.add(pauseRecording);
            popup.addSeparator();
            popup.add(startOver);
            popup.addSeparator();
            popup.add(exitItem);
            trayIcon.setPopupMenu(popup);
            // add the application tray icon to the system tray.
            tray.add(trayIcon);

           // startRecording.addActionListener(event -> Platform.runLater(Creator::startRecording));

        } catch (AWTException | IOException e) {
            System.out.println("Unable to init system tray");
           logger.info(e.getMessage());
        }
    }
}

【讨论】:

    【解决方案2】:

    您需要设置系统属性才能将其从 JFrame 中移出:

    System.setProperty("apple.laf.useScreenMenuBar", "true");
    

    这将在 MacOS 工具栏中显示您的 JMenuBar。

    或者您可以检查您是否正在运行 MacOS,然后设置属性:

      if (System.getProperty("os.name").contains("Mac")) {
      System.setProperty("apple.laf.useScreenMenuBar", "true");
    
    }
    

    【讨论】:

    • 这不是 OP 所要求的。他要的是系统托盘而不是工具栏,而且他说的是 JavaFX 而不是 Swing。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2014-06-04
    相关资源
    最近更新 更多