gpt4 book ai didi

java - 系统托盘上的空白图标

转载 作者:行者123 更新时间:2023-11-30 02:06:10 28 4
gpt4 key购买 nike

我正在尝试向系统托盘添加一个图标,但是,当我运行该应用程序时,该图标为空白。

我的项目结构是这样的:

root/
libs/
...
src/
com/
projname/
logic/
...
ui/
MyClass.java
...
res/
icon.png

我正在尝试在 MyClass.java 中添加图标,如下所示:

private void addToSystemTray() throws AWTException {

if (!SystemTray.isSupported()) {
return;
}

// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();

// load an image
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("icon.png"));

// construct tray icon
TrayIcon trayIcon = new TrayIcon(image, "My Project");

// add the tray image
tray.add(trayIcon);
}

我在 IntelliJ 上将 res/文件夹标记为资源根,并且我还构建了 .jar 来检查图标是否已添加到其中 - 它是.

我做错了什么?

最佳答案

很可能您的图像大于托盘图标大小并被裁剪。这就是您看到图标为空白的原因。所以你必须设置,trayIcon.setImageAutoSize(true);

Java文档:

public void setImageAutoSize(boolean autosize)

Sets the auto-size property. Auto-size determines whether the tray image is automatically sized to fit the space allocated for the image on the tray. By default, the auto-size property is set to false.

If auto-size is false, and the image size doesn't match the tray icon space, the image is painted as-is inside that space — if larger than the allocated space, it will be cropped.

If auto-size is true, the image is stretched or shrunk to fit the tray icon space.

关于java - 系统托盘上的空白图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com