gpt4 book ai didi

java - 系统托盘气球消息不显示 java

转载 作者:行者123 更新时间:2023-12-04 05:47:42 24 4
gpt4 key购买 nike

我想让我的系统尝试使用图标弹出气球消息,但它没有这样做。这是代码。如果满足简单的 if 语句但没有任何反应,它应该弹出。系统托盘图标可见,左键单击它会显示菜单。

package systemtray;

import java.awt.*;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Systemtray {

public static void main(String[] args) {

TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
Image image = Toolkit.getDefaultToolkit().getImage("D:/xxx/facebook.jpg");
// create a action listener to listen for default action executed on the tray icon
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// execute default action of the application
}
};

// create a popup menu
PopupMenu popup = new PopupMenu();
//create menu item for the default action
MenuItem defaultItem = new MenuItem("A menu item");
defaultItem.addActionListener(listener);
popup.add(defaultItem);
/// ... add other items
// construct a TrayIcon*/
trayIcon = new TrayIcon(image, "Tray Demo", popup);


int a = 0;
int b = 1;
if (a + b == 1){
trayIcon.displayMessage("Message Title",
"Message Content",
TrayIcon.MessageType.INFO);
}
// set the TrayIcon properties
trayIcon.addActionListener(listener);
// ...
// add the tray image
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
// ...
} else {
// disable tray option in your application or
// perform other actions
}
// ...
// some time later
// the application state has changed - update the image
// if (trayIcon != null) {
// trayIcon.setImage(updatedImage);
//}
// ...
}
}

最佳答案

除非显示图标,否则无法显示弹出气球。您必须先显示图标(我假设是您代码中的 tray.add)。

关于java - 系统托盘气球消息不显示 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464324/

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