import java.awt.TrayIcon; //导入方法依赖的package包/类
/**
* This method inits the system tray. if supported, the program's window
* does not deiconfy/minimize to the taskbar, but hides and displays an icon
* in the system tray instead.
*/
private void initSystemTray() {
// if systemtray is not supported, leave method
if (!SystemTray.isSupported()) {
return;
}
// create tray-icon with tooltip
trayIcon = new TrayIcon((new ImageIcon(org.jdesktop.application.Application.getInstance(de.danielluedecke.zettelkasten.ZettelkastenApp.class).getClass().getResource("/de/danielluedecke/zettelkasten/resources/icons/zkn3_16x16.png"), "Zettelkasten")).getImage());
// retrieve system tray
tray = SystemTray.getSystemTray();
// try to add the tray icon to the systray
try {
tray.add(trayIcon);
} catch (AWTException e) {
Constants.zknlogger.log(Level.WARNING, "Tray Icon could not be added.");
return;
}
// if tray icon was successfully added, add tooltip
trayIcon.setToolTip("Zettelkasten");
// and mouse listener, so the window will be restored when the user clicks on the tray icon
trayIcon.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
// set main frame visible
getFrame().setVisible(true);
// restore frame state to normal state
getFrame().setExtendedState(java.awt.Frame.NORMAL);
// if we have a tray icon, remove it
if (tray != null) {
// clear popup menu
trayIcon.setPopupMenu(null);
// remove tray icon
tray.remove(trayIcon);
}
// and say that tray icon is currently not installed
trayIconInstalled = false;
}
});
trayIconInstalled = true;
}