如何为 JFrame 窗口和托盘设置图标 [英] how to set icon for JFrame window and tray
本文介绍了如何为 JFrame 窗口和托盘设置图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在窗口中显示我自己的图标而不是 Java 杯.
I would like to show my own icon instead of the Java cup in the window.
此外,当最小化时,我想显示我自己的图像.我将如何能够做到这一点?
Also when minimized, I would like to display, my own image. How will I be able to do so?
我应该将我的图像相对于源文件放在什么位置?
And where should I position my image relative to the source file?
[更新]
我试过了,但没有运气
TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage("image/accounting.gif"));
//setIconImage();
SystemTray tray = SystemTray.getSystemTray();
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}
我也试过
TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
但是严重怀疑createImage(
,即使是Object也不知道要导入什么.
But seriously doubt createImage(
and even if it is Object don't know what to import.
问候,
推荐答案
关于你的TrayIcon
问题,你可以参考以下解决方案:
Regarding your TrayIcon
issue, you can refer below for a solution:
public static void createSystemTrayIcon() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(
System.getenv("MY_PROGRAM_HOME") + "game.ico"
);
PopupMenu popup = new PopupMenu();
final MenuItem menuExit = new MenuItem("Quit");
MouseListener mouseListener =
new MouseListener() {
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
};
ActionListener exitListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime r = Runtime.getRuntime();
System.out.println("Exiting...");
r.exit(0);
}
};
menuExit.addActionListener(exitListener);
popup.add(menuExit);
final TrayIcon trayIcon = new TrayIcon(image, "My program", popup);
ActionListener actionListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage(
"My program ",
"version: blahblah",
TrayIcon.MessageType.INFO
);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
// System Tray is not supported
}
}
这篇关于如何为 JFrame 窗口和托盘设置图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文