java - 仅在 Java 中没有停靠栏/任务栏图标的系统托盘中运行

标签 java

Java 应用程序(准确地说是 .jar)是否可以仅在 SystemTray 中运行,而用户不会在任务栏/dock 上看到任何内容,但是拥有像 JWindow 这样的可见组件?
一个例子是适用于 MacOS 的 Dropbox 应用,它的 SystemTray 中会出现以下窗口,但 Dock 上仍然没有可见的图标。

如果是这样,那该怎么办呢?

最佳答案

放置此行:

System.setProperty("apple.awt.UIElement", "true");

作为 main 中的第一个语句:

public static void main(String[] args) {
    System.setProperty("apple.awt.UIElement", "true");
    // Your stuff...
}

我已经成功地尝试了@DevilsHnd 的“TrayIconDemo2”示例,您可以在本页中找到它作为另一个答案。


顺便说一下,我会添加更多的东西给this answer作者:@穆罕默德·乌斯曼。 我已将所有已检查正确的答案粘贴在下面:

According to JavaFX you cannot hide dock icon in JavaFX application. Please view this link.

There are two ways to hide dock icon.

  • Apple standard way, just modify *.app/Contents/Info.plist and add <key>LSUIElement</key> <string>1</string>.
  • Start your application as AWT application and hide dock icon using system property. After setting system property call the JavaFX main method and JavaFX application will take over now with no dock icon. Please see the sample code snippet below.

/**
 - This class is intended to start application as AWT application before initializing
 - JavaFX application. JavaFX does not support dock-icon-less application so we are 
 - creating JavaFX application from AWT application so that we can achieve the desired
 - functionality.
 - */

public class AWTMain {

  public static void main(String[] args) {

      // This is awt property which enables dock-icon-less
      // applications 
      System.setProperty("apple.awt.UIElement", "true");
      java.awt.Toolkit.getDefaultToolkit();

      // This is a call to JavaFX application main method.
      // From now on we are transferring control to FX application. 
      FXMain.main(args);
  }
}

Here FXMain is referred as previous class with main method.

You will also need to modify your .pom file if you are using maven and other places too where you have mentioned main class for application.

关于java - 仅在 Java 中没有停靠栏/任务栏图标的系统托盘中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669797/

相关文章:

java - 获取 SQLServerException makeFromDriverError 消息 'could not prepare statement' 和 'The connection is closed'

java - 不要求输入

java - 在 JavaFX 项目中加载 fxml 文件时出错

java - 线程无法正常工作?

java - 有没有Mysql备份和恢复java api

java - 如何从按钮单击获取当前 JPanel

java - 字母表可以组成多少个 N 长的字符串?需要一个高效的算法

java - super() 如何在构造函数中工作

java - java中的主线程是否在它可能使用运行时类创建的任何进程完成执行之前完成

java - Libgdx 分块 map 碰撞检测