下面列出了java.awt.event.MouseMotionListener#javax.swing.JMenuItem 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Pushes menu.
*
* @param choosers Array of choosers to find menuItems to push.
* @return Last pushed JMenuItem.
* @throws TimeoutExpiredException
*/
public JMenuItem pushMenu(final ComponentChooser[] choosers) {
return ((JMenuItem) produceTimeRestricted(new Action<Object, Void>() {
@Override
public Object launch(Void obj) {
//TDB 1.5 menu workaround
getQueueTool().waitEmpty();
Object result = driver.pushMenu(JPopupMenuOperator.this,
JMenuOperator.converChoosers(choosers));
getQueueTool().waitEmpty();
return result;
}
@Override
public String getDescription() {
return JMenuOperator.createDescription(choosers);
}
@Override
public String toString() {
return "JPopupMenuOperator.pushMenu.ComponentChooser{description = " + getDescription() + '}';
}
}, "JMenuOperator.PushMenuTimeout"));
}
/**
* Add an item to pay arrears on the given goods type.
*
* @param goodsType The {@code GoodsType} to pay arrears on.
*/
private void addPayArrears(final GoodsType goodsType) {
final InGameController igc = freeColClient.getInGameController();
JMenuItem menuItem = Utility.localizedMenuItem("payArrears");
menuItem.addActionListener((ActionEvent ae) -> {
igc.payArrears(goodsType);
// FIXME: fix pcls so this hackery can go away
if (parentPanel instanceof CargoPanel) {
CargoPanel cargoPanel = (CargoPanel) parentPanel;
cargoPanel.initialize();
}
parentPanel.revalidate();
});
this.add(menuItem);
}
public @Override JMenuItem getPopupMenuItem(JTextComponent target) {
String menuText = org.openide.util.NbBundle.getBundle (NbEditorKit.class).
getString("Menu/View/CodeFolds");
JMenu menu = new JMenu(menuText);
Mnemonics.setLocalizedText(menu, menuText);
setAddSeparatorBeforeNextAction(false);
addAction(target, menu, BaseKit.collapseFoldAction);
addAction(target, menu, BaseKit.expandFoldAction);
setAddSeparatorBeforeNextAction(true);
addAction(target, menu, BaseKit.collapseAllFoldsAction);
addAction(target, menu, BaseKit.expandAllFoldsAction);
// this is a hack, which assumes a certain action name from editor.fold.ui
// if the action does not exist, nothing will be added to the menu.
addAction(target, menu, "collapse-fold-tree"); // NOI18N
addAction(target, menu, "expand-fold-tree"); // NOI18N
// By default add separator before next actions (can be overriden if unwanted)
setAddSeparatorBeforeNextAction(true);
if (target != null) addAdditionalItems(target, menu);
return menu;
}
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target) {
// Try to get the accelerator
Keymap km = target.getKeymap();
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if ((keys != null) && (keys.length > 0)) {
item.setAccelerator(keys[0]);
} else if (a != null) {
KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
if (ks != null) {
item.setAccelerator(ks);
}
}
}
}
/** Get a custom, context-sensitive popup menu for the selected node. */
private JPopupMenu getPopupMenu(DefaultMutableTreeNode node) {
Object ob = node.getUserObject();
LayerThing thing = null;
if (ob instanceof LayerThing) {
thing = (LayerThing)ob;
} else {
return null;
}
// context-sensitive popup
JMenuItem[] item = thing.getPopupItems(this);
if (0 == item.length) return null;
JPopupMenu popup = new JPopupMenu();
for (int i=0; i<item.length; i++) {
if (null == item[i] || "" == item[i].getText()) popup.addSeparator();
else popup.add(item[i]);
}
return popup;
}
@Override
public void actionPerformed (ActionEvent e)
{
JMenuItem source = (JMenuItem) e.getSource();
current = ShapeSet.valueOf(source.getText());
if (current != null) {
banner.setText(current.getName());
Color color = current.getColor();
if (color != null) {
//colorChooser.setColor(color);
refreshBanner();
} else {
banner.setForeground(Color.black);
}
paste.setEnabled(false);
shapes.setRange();
} else {
banner.setText("");
}
}
/**
* Creates a menu item for hiding showing the neighbouring circle.
*/
protected void createDisEnableNeighCircMenuItem() {
final String enableNeighbCirc = Messages
.getString("Viz.EnableNeighbouringCircle");
final String disableNeighbCirc = Messages
.getString("Viz.DisableNeighbouringCircle");
final JMenuItem disEnableNeighCircMenuItem =
new JMenuItem(controller.showNeighbouringCircle ?
disableNeighbCirc : enableNeighbCirc);
disEnableNeighCircMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (disEnableNeighCircMenuItem.getText().equals(enableNeighbCirc)) {
disEnableNeighCircMenuItem.setText(disableNeighbCirc);
controller.showNeighbouringCircle = true;
controller.showNeighboursOnMouseover = true;
} else {
disEnableNeighCircMenuItem.setText(enableNeighbCirc);
controller.showNeighbouringCircle = false;
controller.showNeighboursOnMouseover = false;
}
}
});
add(glPopup, disEnableNeighCircMenuItem, ITEM_ID_ENABLE_NEIGH_CIRC);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu helpMenu = new JMenu("Help");
JMenuItem versionItem = new JMenuItem("White Rabbit v" + Version.getVersion(this.getClass()));
versionItem.setEnabled(false);
helpMenu.add(versionItem);
menuBar.add(helpMenu);
JMenuItem helpItem = new JMenuItem(ACTION_CMD_HELP);
helpItem.addActionListener(this);
helpItem.setActionCommand(ACTION_CMD_HELP);
helpMenu.add(helpItem);
return menuBar;
}
private JMenuItem createFlipArmsJMenuItem() {
JMenuItem item = new JMenuItem("Flip Arms");
item.setActionCommand(Integer.toString(myEntity.getId()));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
FiringDisplay display = (FiringDisplay) currentPanel;
int id = Integer.parseInt(e.getActionCommand());
display.updateFlipArms(!game.getEntity(id).getArmsFlipped());
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
return item;
}
protected void addPopupMenu( final JTable table )
{
final JPopupMenu popupMenu = new JPopupMenu();
final JMenuItem deleteItem = new JMenuItem( "Delete" );
deleteItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed( final ActionEvent e )
{
delete();
System.out.println( "Right-click performed on table and choose DELETE" );
}
});
popupMenu.add( deleteItem );
table.setComponentPopupMenu( popupMenu );
}
static ComponentBuilder getBuilder(Instance instance, Heap heap) {
if (DetailsUtils.isSubclassOf(instance, JButton.class.getName())) {
return new JButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JCheckBox.class.getName())) {
return new JCheckBoxBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JRadioButton.class.getName())) {
return new JRadioButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JToggleButton.class.getName())) {
return new JToggleButtonBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JCheckBoxMenuItem.class.getName())) {
return new JCheckBoxMenuItemBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JRadioButtonMenuItem.class.getName())) {
return new JRadioButtonMenuItemBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenu.class.getName())) {
return new JMenuBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenuBar.class.getName())) {
return new JMenuBarBuilder(instance, heap);
} else if (DetailsUtils.isSubclassOf(instance, JMenuItem.class.getName())) {
return new JMenuItemBuilder(instance, heap);
}
return null;
}
public void testCheckPrioritiesOfIcons() {
AbstractAction aa = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
Icon icon = ImageUtilities.loadImageIcon("org/openide/awt/TestIcon_big.png", true);
aa.putValue(Action.SMALL_ICON, icon);
aa.putValue("iconBase", "org/openide/awt/data/testIcon.gif");
JButton b = new JButton();
Actions.connect(b, aa);
JMenuItem m = new JMenuItem();
Actions.connect(m, aa, false);
assertSame("Using the same icon (small" + icon, b.getIcon(), m.getIcon());
}
private static void createUI() throws Exception {
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame();
frame.setTitle("Test");
JMenuBar menuBar = new JMenuBar();
ImageIcon icon = createIcon();
menuItem = new JMenuItem("Command", icon);
menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
menuBar.add(menuItem);
frame.setJMenuBar(menuBar);
frame.setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
});
}
private void rebuild(Store<HubModel> hubs, JMenu menu) {
menu.removeAll();
menu.add(registerHub);
menu.add(registerHubLegacy);
menu.add(reloadHub);
menu.addSeparator();
menu.add(decodeHubInfo);
menu.addSeparator();
if(hubs.size() == 0) {
JMenuItem emptyMenu = new JMenuItem("Account has no hubs");
emptyMenu.setEnabled(false);
menu.add(emptyMenu);
}
else {
for(HubModel hub: hubs.values()) {
menu.add(new HubMenu(hub));
}
}
}
/**
* Triggered when mouse is pressed.
*
* @param e mouse event
*/
@Override
public void mousePressed (MouseEvent e)
{
if (isContextWanted(e)) {
JPopupMenu popup = new SeparablePopupMenu();
// A title for this menu
JMenuItem head = new JMenuItem("Boards for selection:");
head.setHorizontalAlignment(SwingConstants.CENTER);
head.setEnabled(false);
popup.add(head);
popup.addSeparator();
for (Board board : boards) {
JMenuItem item = new JCheckBoxMenuItem(board.getName(), board.isSelected());
item.addItemListener(this);
item.setToolTipText(
board.isSelected() ? "Deselect this board?" : "Select this board?");
popup.add(item);
}
popup.show(component, e.getX(), e.getY());
}
}
private JMenuItem getLoadLocalSessionMenuItem(final boolean clear) {
JMenuItem loadLocalSessionMenuItem = new JMenuItem();
loadLocalSessionMenuItem.setText("Open local session...");
if (clear) {
loadLocalSessionMenuItem.setText("Open local session...");
loadLocalSessionMenuItem.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
} else {
loadLocalSessionMenuItem.setText("Local session...");
}
loadLocalSessionMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try {
application.loadSession(false, false, clear);
} catch (Exception ioe) {
application.reportException(ioe);
}
}
});
return loadLocalSessionMenuItem;
}
/**
* Get project weakly-referenced from an item.
*
* @param menuItem Menu item.
*
* @return The project, or null if it is not set or has been
* garbage-collected.
*/
private static Project getItemProject(JMenuItem menuItem) {
Reference<Project> p = (Reference<Project>) menuItem.getClientProperty(PROJECT_KEY);
if (p == null) {
return null;
} else {
return p.get();
}
}
/**
* Creates a JRadioButtonMenuItem for the Look and Feel menu.
*
* @param menu the menu
* @param label the label
* @param mnemonic the mnemonic
* @param accessibleDescription the accessible description
* @param laf the laf
* @return the j menu item
*/
public JMenuItem createLafMenuItem(JMenu menu, String label, String mnemonic,
String accessibleDescription, String laf) {
JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
lafMenuGroup.add(mi);
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
// mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
// mi.setEnabled(isAvailableLookAndFeel(laf));
return mi;
}
private JMenu createHelpMenu()
{
JMenu menu = new JMenu();
menu.setText(LanguageBundle.getString("in_mnuHelp"));
menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_DOCS_COMMAND)));
menu.addSeparator();
menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_OGL_COMMAND)));
menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_TIPOFTHEDAY_COMMAND)));
menu.addSeparator();
menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_ABOUT_COMMAND)));
return menu;
}
public static void setMnemonic(JMenuItem item, String labelKey, String indexKey) {
try {
setMnemonic(item, Language.I18N.getString(labelKey), Integer.valueOf(Language.I18N.getString(indexKey)));
} catch (NumberFormatException e) {
//
}
}
/**
* Creates the recent text file item.
*
* @param num the num
* @param file the file
* @return the j menu item
*/
private final JMenuItem createRecentTextFileItem(int num, File file) {
String fileShortName = file.getName();
JMenuItem item = new JMenuItem(num + " " + fileShortName, getMnemonic(num));
item.addActionListener(new LoadRecentTextFileEventHandler(this, file.getAbsolutePath()));
item.setToolTipText(file.getAbsolutePath());
return item;
}
/**
* Create a radio button menu menu item for items that are part of a
* button group.
*
* @param menu the menu
* @param label the label
* @param mnemonic the mnemonic
* @param accessibleDescription the accessible description
* @param action the action
* @param buttonGroup the button group
* @return the j menu item
*/
private JMenuItem createButtonGroupMenuItem(JMenu menu, String label,
String mnemonic,
String accessibleDescription,
Action action,
ButtonGroup buttonGroup) {
JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add(
new JRadioButtonMenuItem(getString(label)));
buttonGroup.add(mi);
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(
accessibleDescription));
mi.addActionListener(action);
return mi;
}
private static void setContextMenu(JTextField field) {
JMenuItem paste =
new JMenuItem(Constant.messages.getString(MESSAGE_PREFIX + "pasteaction"));
paste.addActionListener(e -> field.paste());
JPopupMenu jPopupMenu = new JPopupMenu();
jPopupMenu.add(paste);
field.setComponentPopupMenu(jPopupMenu);
}
protected void prepareControls() {
frame = new JFrame("Mixing : Dropdown Overlapping test");
frame.setLayout(new GridLayout(0,1));
frame.setSize(200, 200);
frame.setVisible(true);
menuBar = new JMenuBar();
JMenu menu = new JMenu("Test Menu");
ActionListener menuListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
lwClicked = true;
}
};
JMenuItem item;
menu.add(item = new JMenuItem("first"));
item.addActionListener(menuListener);
separator = new JSeparator();
separator.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
spClicked = true;
}
});
menu.add(separator);
for (int i = 0; i < petStrings.length; i++) {
menu.add(item = new JMenuItem(petStrings[i]));
item.addActionListener(menuListener);
}
menuBar.add(menu);
frame.setJMenuBar(menuBar);
propagateAWTControls(frame);
frame.setVisible(true);
}
/**
* ActionListener interface implementation
* This would presumably handle menu selections, but none of them actually
* do anything now.
*/
public void actionPerformed(ActionEvent e) {
if ( DEBUG ) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = "Action event detected." // NOI18N
+ "\n" // NOI18N
+ " Event source: " + source.getText() // NOI18N
+ " (an instance of " + getClassName(source) + ")"; // NOI18N
System.out.println (s + "\n"); // NOI18N
}
}
private void addPopupCommands() {
JMenuItem clearItem = jPopupMenu1.add("Clear");
clearItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}});
}
/**
* Getter method for helpAboutMenuItem
*
* @return helpAboutMenuItem
*/
public JMenuItem getHelpAboutMenuItem() {
if (this.helpAboutMenuItem == null) {
this.helpAboutMenuItem = new JMenuItem(labelUtil.getString("gui.menu.help.about"));
this.helpAboutMenuItem.setMnemonic(KeyEvent.VK_A);
}
return this.helpAboutMenuItem;
}
private JMenuItem createBestowCurseMenu(WorldObject target) {
JMenuItem bestowCurseMenuItem = MenuFactory.createJMenuItem(new ChooseCurseAction(playerCharacter, imageInfoReader, soundIdReader, world, (WorldPanel)container, dungeonMaster, parentFrame, target), soundIdReader);
bestowCurseMenuItem.setText(Actions.BESTOW_CURSE_ACTION.getSimpleDescription());
bestowCurseMenuItem.setEnabled(Game.canActionExecute(playerCharacter, Actions.BESTOW_CURSE_ACTION, Args.EMPTY, world, target));
addToolTips(Actions.BESTOW_CURSE_ACTION, bestowCurseMenuItem);
setMenuIcon(bestowCurseMenuItem, Actions.BESTOW_CURSE_ACTION.getImageIds(playerCharacter));
return bestowCurseMenuItem;
}
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem) (e.getSource());
String s = "Action event detected." + newline + " Event source: " + source.getText() + " (an instance of "
+ getClassName(source) + ")";
output.append(s + newline);
output.setCaretPosition(output.getDocument().getLength());
}
@Override
public JMenuItem getPopupPresenter() {
List<PhpDocAction> docActions = new ArrayList<>(docProviders.size());
for (PhpDocumentationProvider docProvider : docProviders) {
docActions.add(new PhpDocAction(docProvider.getDisplayName(), phpModule, docProvider, true));
}
return new DocumentationSubMenu(docActions);
}