java - 请求关注 JavaFX 阶段不会更改 macOS 左上角菜单栏标题

标签 java macos javafx menubar scene

当我尝试请求焦点(我在 macOS Mojave 10.14.2 上)时,当用户单击托盘图标菜单中的“显示”按钮时,它不会更改左上角的菜单栏(例如Chrome -> java),但至少它把它带到了前面。在全屏应用程序中(例如,在全屏中单击 Sublime Text 中的托盘图标),它根本不会出现,而是在主桌面空间中显示而不移动到它。


菜单栏示例:

About to show window应该变成 this is what should happen而是 fail: it brings to top but doesn't actually gain focus

全屏示例:

About to show window when in a fullscreen app 单击“显示”时...

Nothing happens?!! 好像什么都没发生!它打开时没有焦点出现在顶部,而是出现在主要的“桌面”空间。


我试过先执行 toFront()requestFocus() 的组合,或者只执行一个或另一个,但它似乎没有工作。

是否有人对此问题有任何修复/解决方法?

这是上面用来演示问题的简单应用程序:

package me.matetoes.dockvisibility;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

public class DockVisibilityTester extends Application {

    public javafx.scene.control.Button hideButton;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        hideButton = new javafx.scene.control.Button("Hide");
        hideButton.setOnAction(e -> handleHide());
        Scene scene = new Scene(hideButton, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Testing");
        Platform.setImplicitExit(false);
        createTrayIcon(primaryStage);
        primaryStage.show();
    }

    private void createTrayIcon(final Stage stage) {
        if (SystemTray.isSupported()) {
            SystemTray tray = SystemTray.getSystemTray(); // get the SystemTray instance

            Image icon = null;
            try { // load an image
                URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
                icon = ImageIO.read(url);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            stage.setOnCloseRequest(e -> hide(stage)); //hide instead of close

            // to be added on "show" MenuItem and trayIcon itself
            ActionListener showListener = e -> show(stage);

            PopupMenu popup = new PopupMenu(); // create a popup menu

            MenuItem showItem = new MenuItem("Show");
            showItem.addActionListener(showListener);

            MenuItem closeItem = new MenuItem("Close");
            closeItem.addActionListener(e -> System.exit(0));

            popup.add(showItem);
            popup.addSeparator();
            popup.add(closeItem);

            assert icon != null;
            TrayIcon trayIcon = new TrayIcon(icon, "Test", popup); // construct a TrayIcon
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(showListener);

            try { // add the tray image
                tray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

    private void hide(final Stage stage) {
        Platform.runLater(() -> {
            if (SystemTray.isSupported()) {
                stage.hide();
            } else {
                System.exit(0);
            }
        });
    }

    private void show(final Stage stage) {
        Platform.runLater(() -> {
            stage.show();

            // doesn't work!
            stage.requestFocus();
            stage.toFront();
        });
    }

    public void handleHide() {
        Stage stage = (Stage) hideButton.getScene().getWindow();
        hide(stage);
    }
}

谢谢! :)

最佳答案

您可以尝试使用 useSystemMenuBar()

有点像

MenuBar mnuBar;
mnuBar.useSystemMenuBar();

关于java - 请求关注 JavaFX 阶段不会更改 macOS 左上角菜单栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54357706/

相关文章:

具有值的 JavaFX 折线图

java - Integer.Parse 在 excel 获取的字符串上抛出 NumberFormatException

java - 为什么 appcompat-v7 :22. 2.0 会破坏 ActionMode 覆盖 ActionBar?

macos - 如何在 Mac OS X 上模拟 'cp --update' 行为?

java - JTable 基于对象列表,如 TableView 的项目列表

javafx - 为 JavaFX 桌面应用程序设置全屏独占模式

java - Swing 播放视频(不带 VLC 的 VLCJ)

java - 与 Android 中的 e.printStackTrace() 混淆

c++ - Mac OSX 上的 Valgrind Qt 应用程序 - 报告不兼容的处理器

macos - 在 AppleScript 中使用 "~"和 POSIX 路径