简体   繁体   English

Java 应用程序托盘图标在 CentOS 上无法正确显示

[英]Java app Tray Icon not displaying properly on CentOS

I've written a Java app that can be used on both Windows and Linux.我编写了一个可以在 Windows 和 Linux 上使用的 Java 应用程序。 The app uses a TrayIcon.该应用程序使用 TrayIcon。 On Windows, this works perfectly, but on Linux (CentOS) the TrayIcon has 2 problems: 1) I've lost the transparency in my png image) and 2) the image looks like it is shifted up (more on this later).在 Windows 上,这可以完美运行,但在 Linux (CentOS) 上,TrayIcon 有两个问题:1) 我的 png 图像丢失了透明度) 和 2) 图像看起来像是向上移动了(稍后会详细介绍)。

I take into account the the different environments by getting the tray icon size and then scaling accordingly.我通过获取托盘图标大小然后相应地缩放来考虑不同的环境。 Here is my code:这是我的代码:

Dimension trayIconSize = tray.getTrayIconSize();
Image originalImage = toolkit.getImage("tray_icon.png");
Image scaledImage = originalImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);
trayIcon = new TrayIcon(scaledImage, "Some Text");

On CentOS, the returned dimension for .getTrayIconSize() is 24x24, but after testing, it's actually fits a 24x32 (WxH) image, which accounts for the image appearing shifted up when it is set to 24x24.在 CentOS 上,.getTrayIconSize() 的返回尺寸是 24x24,但经过测试,它实际上适合 24x32 (WxH) 的图像,这说明了设置为 24x24 时图像向上移动的原因。

Is there any way to maintain the background transparency?有没有办法保持背景透明度? Also, any suggestions on getting a properly sized icon dynamically?另外,关于动态获取大小合适的图标有什么建议吗?

Size尺寸

Although the documentation states that SystemTray.getTrayIconSize() "returns the size, in pixels, of the space that a tray icon will occupy in the system tray", the implementation actually returns a constant value depending on the operating system.尽管文档指出 SystemTray.getTrayIconSize() “返回托盘图标在系统托盘中所占空间的大小(以像素为单位)”,但该实现实际上返回一个常数值,具体取决于操作系统。

This is the actual implementation of the method in XSystemTrayPeer.java (Oracle JRE 1.8, OpenJDK is similar):这是XSystemTrayPeer.java中方法的实际实现(Oracle JRE 1.8,OpenJDK类似):

public Dimension getTrayIconSize() {
    return new Dimension(24, 24);
}

This is however not a Linux restriction.然而,这不是 Linux 限制。 The Windows specific implementation returns a constant dimension of 16x16. Windows 特定实现返回 16x16 的常量维度。 But while most Windows systems actually seem to stick to that size, the vast multitude of Linux desktops also comes with an equally large variety of system trays in all shapes and sizes.但是,尽管大多数 Windows 系统实际上似乎都坚持这个大小,但大量的 Linux 桌面还带有同样多的各种形状和大小的系统托盘。 The method is therefore error prone and cannot be relied on.因此,该方法容易出错并且不能依赖。

Transparency透明度

Transparency is not supported in the X11 implementation of the system tray.系统托盘的 X11 实现不支持透明度。 See this answer and this bug report for details.有关详细信息,请参阅此答案此错误报告

Alternatives备择方案

Take a look at dorkbox/SystemTray .看看dorkbox/SystemTray It's an Apache 2.0 licensed tray lib which is also available through Maven/Gradle.它是一个 Apache 2.0 许可的托盘库,也可以通过 Maven/Gradle 获得。 It supports dynamic scaling and transparency on all platforms and also supports AppIndicator.它支持所有平台上的动态缩放和透明度,还支持 AppIndicator。 I'm not affiliated with the project.我不隶属于该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM