如何使用JavaFX在Mac顶部工具栏上显示菜单?

时间:2018-11-02 23:16:30

标签: java macos javafx

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

Example of what I want for my application

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

}

答案 1 :(得分:0)

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