有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

关闭主阶段时java JavaFX隐藏任务栏图标

我按照this tutorial创建了一个带有托盘图标的应用程序。当我关闭应用程序窗口时,任务栏上留下了一个图标。问题是当我关闭窗口时,如何隐藏任务栏上的图标?如果不可能,那么当我单击任务栏图标时,是否有方法返回窗口?跨平台解决方案将非常受欢迎


共 (1) 个答案

  1. # 1 楼答案

    The gist你链接到的东西都有你想要的

    1. 重新打开JavaFX窗口的方法(包括两部分):

      • Lines 39-40

        // instructs the javafx system not to exit implicitly when the last application window is shut.
        Platform.setImplicitExit(false);
        
      • Lines 106-112

        // if the user double-clicks on the tray icon, show the main app stage. 
        trayIcon.addActionListener(event -> Platform.runLater(this::showStage));
        
        // if the user selects the default menu item (which includes the app name), 
        // show the main app stage. 
        java.awt.MenuItem openItem = new java.awt.MenuItem("hello, world"); 
        openItem.addActionListener(event -> Platform.runLater(this::showStage));
        
    2. 完全退出应用程序的方法:

      • Lines 120-128

        // to really exit the application, the user must go to the system tray icon
        // and select the exit option, this will shutdown JavaFX and remove the
        // tray icon (removing the tray icon will also shut down AWT).
        java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
        exitItem.addActionListener(event -> {
            notificationTimer.cancel();
            Platform.exit();
            tray.remove(trayIcon);
        });