gpt4 book ai didi

java - 如何将 Java 应用程序放入系统托盘?

转载 作者:IT老高 更新时间:2023-10-28 20:26:53 25 4
gpt4 key购买 nike

我有一个小控制面板,只是我制作的一个小应用程序。我想用系统最小化/上下控制面板,以及电池生命周期、日期、网络等。

谁能给我线索、教程链接或阅读内容?

最佳答案

从 Java 6 开始,SystemTray 支持此功能。和 TrayIcon类。 SystemTray 在其 Javadocs 中有一个相当广泛的示例:

TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
Image image = Toolkit.getDefaultToolkit().getImage("your_image/path_here.gif");
// 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(...);
defaultItem.addActionListener(listener);
popup.add(defaultItem);
/// ... add other items
// construct a TrayIcon
trayIcon = new TrayIcon(image, "Tray Demo", popup);
// 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);
}
// ...

您也可以查看 this article , 或 this tech tip .

关于java - 如何将 Java 应用程序放入系统托盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/758083/

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