下面列出了java.awt.TrayIcon#setPopupMenu ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void createSystemTrayIcons() {
final TrayIcon trayIcon = new TrayIcon(createSystemTrayIconImage());
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Update Popup Menu items");
try {
trayIcon.setPopupMenu(createPopupMenu(trayIcon, 2));
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException ex) {
throw new RuntimeException("System Tray cration failed");
}
}
private static void createSystemTrayIcon() {
final TrayIcon trayIcon = new TrayIcon(createTrayIconImage());
trayIcon.setImageAutoSize(true);
try {
// Add tray icon to system tray *before* adding popup menu to demonstrate buggy behaviour
trayIcon.setPopupMenu(createTrayIconPopupMenu());
SystemTray.getSystemTray().add(trayIcon);
iconWeakReference.set(new WeakReference<>(trayIcon));
popupWeakReference.set(new WeakReference<>(trayIcon.getPopupMenu()));
} catch (final AWTException awte) {
awte.printStackTrace();
}
}
private TrayIcon create(Image image) throws IOException {
TrayIcon trayIcon = new java.awt.TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip(tooltip);
trayIcon.setPopupMenu(createMenu(false));
return trayIcon;
}
private void addTrayIcon() throws AWTException {
final TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icons/The_Explorer_card_art.png")).getImage(), "Path of Leveling");
// Create a pop-up menu components
final PopupMenu popup = new PopupMenu();
final MenuItem shutdownItem = new MenuItem("Exit");
final MenuItem settingsItem = new MenuItem("Settings");
//Add components to pop-up menu
popup.add(settingsItem);
popup.add(shutdownItem);
trayIcon.setPopupMenu(popup);
trayIcon.setImageAutoSize(true); //So the icon auto-sizes
SystemTray.getSystemTray().add(trayIcon);
trayIcon.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(java.awt.event.MouseEvent e) {
if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) { //Left click on tray icon (single click !)
Platform.runLater(() -> {
//code to show things...
});
}
}
});
shutdownItem.addActionListener(evnt -> {
//code to exit the program
//save stuff
if(saveBuildsToMemory()){
System.out.println("Successfully saved checkpoint");
}else{
System.out.println("Checkpoint save failed");
}
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException e1) {
e1.printStackTrace();
}
System.exit(20);
});
settingsItem.addActionListener(evnt -> {
//code to exit the program
//save stuff
if(controller!=null){
controller.settings_event();
}
});
}
/**
* Installs a system tray icon.
*/
private void installTrayIcon() {
if ( !SystemTray.isSupported() )
return;
final TrayIcon trayIcon = new TrayIcon( Icons.MY_APP_ICON.get().getImage(), Consts.APP_NAME_FULL + " is running." );
trayIcon.setImageAutoSize( true );
try {
SystemTray.getSystemTray().add( trayIcon );
this.trayIcon = trayIcon;
trayIcon.addActionListener( Actions.SHOW_MAIN_FRAME );
final PopupMenu popup = new PopupMenu();
final MenuItem restoreMenuItem = new MenuItem( "Show Main Window" );
restoreMenuItem.addActionListener( Actions.SHOW_MAIN_FRAME );
popup.add( restoreMenuItem );
final MenuItem hideMenuItem = new MenuItem( "Hide Main Window" );
hideMenuItem.addActionListener( Actions.MINIMIZE_TO_TRAY );
popup.add( hideMenuItem );
popup.addSeparator();
final MenuItem restoreDefPosMenuItem = new MenuItem( "Restore Main Window to defaults" );
restoreDefPosMenuItem.addActionListener( new ActionAdapter() {
@Override
public void actionPerformed( final ActionEvent e ) {
// First ensure it's visible and active:
Actions.SHOW_MAIN_FRAME.actionPerformed( null );
// And the default position:
Actions.RESTORE_DEF_WIN_POSITION.actionPerformed( null );
}
} );
popup.add( restoreDefPosMenuItem );
popup.addSeparator();
final MenuItem exitMenuItem = new MenuItem( "Exit" );
exitMenuItem.addActionListener( Actions.EXIT );
popup.add( exitMenuItem );
trayIcon.setPopupMenu( popup );
Actions.MINIMIZE_TO_TRAY.setEnabled( true );
} catch ( final AWTException ae ) {
Env.LOGGER.debug( "Failed to install tray icon!", ae );
}
}