下面列出了怎么用java.awt.PopupMenu的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
public void loadPopupMenu() {
//添加右键菜单
PopupMenu popupMenu = new PopupMenu();
MenuItem showItem = new MenuItem(I18nUtil.getMessage("gui.tray.show"));
showItem.addActionListener(event -> Platform.runLater(() -> loadUri("", true)));
MenuItem setItem = new MenuItem(I18nUtil.getMessage("gui.tray.set"));
setItem.addActionListener(event -> loadUri("/#/setting", true));
MenuItem aboutItem = new MenuItem(I18nUtil.getMessage("gui.tray.about"));
aboutItem.addActionListener(event -> loadUri("/#/about", true));
MenuItem supportItem = new MenuItem(I18nUtil.getMessage("gui.tray.support"));
supportItem.addActionListener(event -> loadUri("/#/support", true));
MenuItem closeItem = new MenuItem(I18nUtil.getMessage("gui.tray.exit"));
closeItem.addActionListener(event -> {
Platform.exit();
System.exit(0);
});
popupMenu.add(showItem);
popupMenu.addSeparator();
popupMenu.add(setItem);
popupMenu.add(aboutItem);
popupMenu.add(supportItem);
popupMenu.addSeparator();
popupMenu.add(closeItem);
trayIcon.setPopupMenu(popupMenu);
}
public ThisCanvas(Thread threadApplet, LinkList first, NodeShow nodeView
, PopupMenu nodeMenu, JTextPane rightBotJTextPane){
this.setLayout(null);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.start();
this.setOpaque(false);
this.threadApplet= threadApplet;
this.first= first;
this.nodeView= nodeView;
this.nodeMenu= nodeMenu;
this.rightBotJTextPane= rightBotJTextPane;
chooseCheck= new ChooseCheckVPS();
dynamicLineUpdater= new DynamicLineUpdaterVPS();
drawArrow= new DrawArrowVPS();
checkRange= new CheckRangeVPS();
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, () -> {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
WPopupMenuPeer peer = AWTAccessor.getMenuComponentAccessor()
.getPeer(popup);
peer.show(popupParent, new Point(x, y));
}
});
}
private PopupMenu createPopupMenu(final TrayIcon trayIcon,
final int menuCount) {
final PopupMenu trayIconPopupMenu = new PopupMenu();
for (int i = 1; i <= menuCount; ++i) {
final MenuItem popupMenuItem = new MenuItem("MenuItem_" + i);
popupMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent ae) {
trayIcon.setPopupMenu(createPopupMenu(trayIcon,
menuCount + 1));
}
});
trayIconPopupMenu.add(popupMenuItem);
}
return trayIconPopupMenu;
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
@Override
public synchronized void showPopupMenu(final int x, final int y) {
if (isDisposed())
return;
SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
@Override
public void run() {
PopupMenu newPopup = ((TrayIcon)target).getPopupMenu();
if (popup != newPopup) {
if (popup != null) {
popupParent.remove(popup);
}
if (newPopup != null) {
popupParent.add(newPopup);
}
popup = newPopup;
}
if (popup != null) {
((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y));
}
}
});
}
StaveActionHandler(Stave stave) {
theApp = stave;
noteContextMenu = new PopupMenu();
editNote = new MenuItem("Edit Note");
editNote.addActionListener(this);
noteContextMenu.add(editNote );
repeatNote = new MenuItem("Repeat Note");
repeatNote.addActionListener(this);
noteContextMenu.add(repeatNote );
makeRest = new MenuItem("Change to Rest");
makeRest.addActionListener(this);
noteContextMenu.add(makeRest);
deleteNote = new MenuItem("Delete Note");
deleteNote.addActionListener(this);
noteContextMenu.add(deleteNote );
theApp.add(noteContextMenu);
}
private PopupMenu createPopupMenu() {
PopupMenu menu = new PopupMenu(getString("Server") + " "
+ Metadata.getApplicationName());
menu.add(createStart());
menu.add(createStop());
menu.addSeparator();
menu.add(createRestart());
menu.addSeparator();
menu.add(createPreferences());
menu.addSeparator();
menu.add(createExit());
return menu;
}
@Override
public synchronized void setMenu(PopupMenu m) {
this.menu = m;
if (menu != null && menu.getItemCount() > 0) {
int msize = menu.getItemCount();
MenuItem[] items = new MenuItem[msize];
for (int i = 0; i < msize; i++) {
items[i] = menu.getItem(i);
}
setNativeMenu(items);
} else {
setNativeMenu(null);
}
}
private static void addNotifyPopup() {
PopupMenu menu = popupWeakReference.get().get();
if (menu == null) {
throw new RuntimeException("Failed: popup collected too early");
}
menu.addNotify();
}
/**
* Maps {@code Component.add(PopupMenu)} through queue
*/
public void add(final PopupMenu popupMenu) {
runMapping(new MapVoidAction("add") {
@Override
public void map() {
getSource().add(popupMenu);
}
});
}
public static void setupTray() throws AWTException {
final SystemTray tray = SystemTray.getSystemTray();
final PopupMenu popup = new PopupMenu();
final MenuItem info = new MenuItem();
final MenuItem exit = new MenuItem();
final TrayIcon trayIcon = new TrayIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), "MLGA", popup);
try {
InputStream is = FileUtil.localResource("icon.png");
trayIcon.setImage(ImageIO.read(is));
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
info.addActionListener(e -> {
String message = "Double-Click to lock/unlock the overlay for dragging";
JOptionPane.showMessageDialog(null, message, "Information", JOptionPane.INFORMATION_MESSAGE);
});
exit.addActionListener(e -> {
running = false;
tray.remove(trayIcon);
ui.close();
System.out.println("Terminated UI...");
System.out.println("Cleaning up system resources. Could take a while...");
handle.close();
System.out.println("Killed handle.");
System.exit(0);
});
info.setLabel("Help");
exit.setLabel("Exit");
popup.add(info);
popup.add(exit);
tray.add(trayIcon);
}
private void createComponents() {
startedImage = createImage("/images/subsonic-started-16.png");
stoppedImage = createImage("/images/subsonic-stopped-16.png");
PopupMenu menu = new PopupMenu();
menu.add(createMenuItem(openAction));
menu.add(createMenuItem(controlPanelAction));
menu.addSeparator();
menu.add(createMenuItem(hideAction));
trayIcon = new TrayIcon(stoppedImage, "Subsonic Music Streamer", menu);
}
public PopupMenu create(boolean isUserLoggedIn) {
root = new PopupMenu();
if (isUserLoggedIn) {
root.add(createRootFolderMenu());
// root.add(createRecentFilesMenu()); // TODO: implement additional feature.
root.addSeparator();
root.add(createSettingsMenu());
root.addSeparator();
}
root.add(createActivityMenu());
root.add(createQuitMenu());
return root;
}
/**
* Tray icon handler.
*/
private void trayIconHandler(InputStream iconInputStream) throws IOException, AWTException {
// set up the system tray
var tray = SystemTray.getSystemTray();
var image = ImageIO.read(iconInputStream);
// use the loaded icon as tray icon
var trayIcon = new TrayIcon(image);
// show the primary stage if the icon is right clicked
trayIcon.addActionListener(event -> Platform.runLater(this::showStage));
// add app name as a menu item
var openItem = new MenuItem(APPLICATION_TITLE);
// show the primary stage if the app name item is clicked
openItem.addActionListener(event -> Platform.runLater(this::showStage));
// add Preferences menu item
var settingItem = new MenuItem("Preferences");
settingItem.addActionListener(event -> Platform.runLater(() -> UIUtils.showPreferencesDialog(0)));
// add check for updates menu item
var updateCheckItem = new MenuItem("Check for Updates");
// add current version info menu item
var versionItem = new MenuItem("Version: " + PROPERTIES.getProperty("version"));
// add click action listener
updateCheckItem.addActionListener(event -> {
try {
Desktop.getDesktop().browse(new URI(IOUtils.GITHUB_RELEASES_URL));
} catch (IOException | URISyntaxException ignored) {
}
});
// add quit option as the app cannot be closed by clicking the window close button
var exitItem = new MenuItem("Quit");
// add action listener for cleanup
exitItem.addActionListener(event -> {
// remove the icon
tray.remove(trayIcon);
Platform.exit();
System.exit(0);
});
// set up the popup menu
final var popup = new PopupMenu();
popup.add(openItem);
popup.addSeparator();
popup.add(settingItem);
popup.addSeparator();
popup.add(versionItem);
popup.add(updateCheckItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
// add icon to the system
tray.add(trayIcon);
}
CPopupMenu(PopupMenu target) {
super(target);
}
CPopupMenu(PopupMenu target) {
super(target);
}
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();
}
});
}
CPopupMenu(PopupMenu target) {
super(target);
}
protected PopupMenuPeer createPopupMenu(PopupMenu target) throws HeadlessException {
throw new IllegalStateException("Method not implemented");
}