如何使用JavaFX在mac桌面工具栏上设置菜单?
在Mac OS上,在后台运行的应用程序有时会将其图标附加到屏幕右角的gui或菜单上。我相信它与Windows在右下角的功能类似。 不过,我希望我的JavaFX应用程序也能使用这个。我不知道怎么用谷歌搜索。我只找到了JavaFX的菜单栏,不幸的是,这不是我想要的如何使用JavaFX在mac桌面工具栏上设置菜单?,java,macos,javafx,Java,Macos,Javafx,在Mac OS上,在后台运行的应用程序有时会将其图标附加到屏幕右角的gui或菜单上。我相信它与Windows在右下角的功能类似。 不过,我希望我的JavaFX应用程序也能使用这个。我不知道怎么用谷歌搜索。我只找到了JavaFX的菜单栏,不幸的是,这不是我想要的 您需要设置系统属性才能将其从JFrame中移出: System.setProperty("apple.laf.useScreenMenuBar", "true"); 这将在MacOS工具栏中显示JMenuBar 或者,您可以检查是否正在
您需要设置系统属性才能将其从JFrame中移出:
System.setProperty("apple.laf.useScreenMenuBar", "true");
这将在MacOS工具栏中显示JMenuBar
或者,您可以检查是否正在运行MacOS,然后设置属性:
if (System.getProperty("os.name").contains("Mac")) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
为了澄清,使用with set To
true
不是您想要的,对吗?你在找类似的东西吗?如果是,请看一看。谢谢!SystemTray成功了。这不是OP想要的。他要的是系统托盘而不是工具栏,他说的是JavaFX而不是Swing。
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());
}
}
}