Java Tutorial/Swing/JMenuItem
Содержание
- 1 Adding Drop-Down Menus
- 2 Adding Mnemonic and Accelerator to Menuitem
- 3 Creating a Menu Item That Listens for Changes to Its Selection Status
- 4 Creating an About Menu Item
- 5 Creating JMenuItem Components
- 6 Creating Menuitem from Action class
- 7 Customizing JMenuItem Look and Feel
- 8 Disable Menuitem
- 9 Listening to JMenuItem Events with a MenuDragMouseListener
- 10 Listening to JMenuItem Events with a MenuKeyListener
- 11 Listening to JMenuItem Events with an ActionListener
- 12 Separating Menu Items in a Menu
- 13 Using Function key as Accelerator
Adding Drop-Down Menus
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class AddingDropDownMenus extends JFrame {
private JMenuBar menuBar = new JMenuBar(); // Window menu bar
private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
private JRadioButtonMenuItem lineItem, rectangleItem, circleItem;
private JCheckBoxMenuItem redItem, yellowItem;
public AddingDropDownMenus() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu elementMenu = new JMenu("Elements");
newItem = fileMenu.add("New");
openItem = fileMenu.add("Open");
closeItem = fileMenu.add("Close");
fileMenu.addSeparator();
saveItem = fileMenu.add("Save");
saveAsItem = fileMenu.add("Save As...");
fileMenu.addSeparator();
printItem = fileMenu.add("Print");
elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
ButtonGroup types = new ButtonGroup();
types.add(lineItem);
types.add(rectangleItem);
types.add(circleItem);
elementMenu.addSeparator();
elementMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
elementMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
menuBar.add(fileMenu);
menuBar.add(elementMenu);
}
public static void main(String[] a) {
AddingDropDownMenus window = new AddingDropDownMenus();
window.setBounds(30, 30, 300, 300); // Size
window.setVisible(true);
}
}
Adding Mnemonic and Accelerator to Menuitem
A menu accelerator activates a menu item when it is not visible.
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class JMenuItemKeyStroke {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
// Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T);
KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X");
cutMenuItem.setAccelerator(ctrlXKeyStroke);
fileMenu.add(cutMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Creating a Menu Item That Listens for Changes to Its Selection Status
import javax.swing.JMenuItem;
public class Main {
public static void main(String[] argv) throws Exception {
JMenuItem item = new JMenuItem("Label") {
public void menuSelectionChanged(boolean isSelected) {
super.menuSelectionChanged(isSelected);
if (isSelected) {
System.out.println("The menu item is selected");
} else {
System.out.println("The menu item is no longer selected");
}
}
};
}
}
Creating an About Menu Item
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class FramewithAboutMenu extends JFrame {
public FramewithAboutMenu(String title) {
JMenuBar menuBar = new JMenuBar();
setTitle(title);
setJMenuBar(menuBar);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
JMenu elementMenu = new JMenu("Elements");
JMenu helpMenu = new JMenu("Help");
fileMenu.setMnemonic("F");
elementMenu.setMnemonic("E");
helpMenu.setMnemonic("H");
JMenuItem aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
menuBar.add(helpMenu);
}
public static void main(String[] a) {
FramewithAboutMenu window = new FramewithAboutMenu("Sketcher");
window.setBounds(30, 30, 300, 300);
window.setVisible(true);
}
}
Creating JMenuItem Components
public JMenuItem()
JMenuItem jMenuItem = new JMenuItem();
public JMenuItem(Icon icon)
Icon atIcon = new ImageIcon("at.gif");
JMenuItem jMenuItem = new JMenuItem(atIcon);
public JMenuItem(String text)
JMenuItem jMenuItem = new JMenuItem("Cut");
public JMenuItem(String text, Icon icon)
Icon atIcon = new ImageIcon("at.gif");
JMenuItem jMenuItem = new JMenuItem("Options", atIcon);
public JMenuItem(String text, int mnemonic)
JMenuItem jMenuItem = new JMenuItem("Cut", KeyEvent.VK_T);
public JMenuItem(Action action)
Action action = ...;
JMenuItem jMenuItem = new JMenuItem(action);
Creating Menuitem from Action class
The mnemonic for a menu item usually appears underlined within the text label for the menu
class ShowAction extends AbstractAction {
Component parentComponent;
public ShowAction(Component parentComponent) {
super("About");
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
this.parentComponent = parentComponent;
}
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(parentComponent, "About Swing", "About Box V2.0",
JOptionPane.INFORMATION_MESSAGE);
}
}
public class ContructMenuWithAction {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem(new ShowAction(frame));
fileMenu.add(newMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Customizing JMenuItem Look and Feel
Property StringObject TypeMenuItem.acceleratorDelimiterStringMenuItem.acceleratorFontFontMenuItem.acceleratorForegroundColorMenuItem.acceleratorSelectionForegroundColorMenuItem.actionMapActionMapMenuItem.arrowIconIconMenuItem.backgroundColorMenuItem.borderBorderMenuItem.borderPaintedBooleanMenuItem.checkIconIconMenuItem.rumandSoundStringMenuItem.disabledForegroundColorMenuItem.fontFontMenuItem.foregroundColorMenuItem.marginInsetsMenuItem.opaqueBooleanMenuItem.selectionBackgroundColorMenuItem.selectionForegroundColorMenuItem.textIconGapIntegerMenuItemUIString
Disable Menuitem
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class JMenuItemDisabled {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
// Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
JMenuItem pasteMenuItem = new JMenuItem("Paste", KeyEvent.VK_P);
KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V");
pasteMenuItem.setAccelerator(ctrlVKeyStroke);
pasteMenuItem.setEnabled(false);
fileMenu.add(pasteMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Listening to JMenuItem Events with a MenuDragMouseListener
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.MenuDragMouseEvent;
import javax.swing.event.MenuDragMouseListener;
public class ContructMenuMenuDragMouseListener {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("asdf");
fileMenu.add(newMenuItem);
newMenuItem.addMenuDragMouseListener(new MenuDragMouseListener(){
public void menuDragMouseEntered(MenuDragMouseEvent e) {
System.out.println("menuDragMouseEntered");
}
public void menuDragMouseExited(MenuDragMouseEvent e) {
System.out.println("menuDragMouseExited");
}
public void menuDragMouseDragged(MenuDragMouseEvent e) {
System.out.println("menuDragMouseDragged");
}
public void menuDragMouseReleased(MenuDragMouseEvent e) {
System.out.println("menuDragMouseReleased");
}});
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Listening to JMenuItem Events with a MenuKeyListener
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
public class ContructMenuMenuKeyListener {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("asdf");
fileMenu.add(newMenuItem);
newMenuItem.addMenuKeyListener(new MenuKeyListener(){
public void menuKeyTyped(MenuKeyEvent e) {
System.out.println("KeyTyped");
}
public void menuKeyPressed(MenuKeyEvent e) {
System.out.println("KeyPressed");
}
public void menuKeyReleased(MenuKeyEvent e) {
System.out.println("KeyReleased");
}});
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Listening to JMenuItem Events with an ActionListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + e.getActionCommand());
}
}
public class ContructMenuActionListener {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new MenuActionListener());
fileMenu.add(newMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Separating Menu Items in a Menu
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
public class Main {
public static void main(String[] argv) throws Exception {
JMenu menu = new JMenu("Menu Label");
JMenuItem item1 = new JMenuItem("Item Label");
menu.add(item1);
// Add separator
menu.add(new JSeparator());
// Add another menu item
JMenuItem item2 = new JMenuItem("Item Label");
menu.add(item2);
}
}
Using Function key as Accelerator
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class JMenuItemAccelerator {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
// Edit->Find, F - Mnemonic, F3 - Accelerator
JMenuItem findMenuItem = new JMenuItem("Find", KeyEvent.VK_F);
KeyStroke f3KeyStroke = KeyStroke.getKeyStroke("F3");
findMenuItem.setAccelerator(f3KeyStroke);
fileMenu.add(findMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}