Java by API/javax.swing/JMenuItem
Содержание
- 1 JMenuItem: addActionListener(ActionListener l)
- 2 JMenuItem: addChangeListener(ChangeListener l)
- 3 JMenuItem: addItemListener(ItemListener l)
- 4 JMenuItem: addMenuDragMouseListener(MenuDragMouseListener l)
- 5 JMenuItem: addMenuKeyListener(MenuKeyListener l)
- 6 JMenuItem.RIGHT
- 7 JMenuItem: setAccelerator(KeyStroke key)
- 8 JMenuItem: setMnemonic(char mnemonic)
- 9 new JMenuItem(Action act)
JMenuItem: addActionListener(ActionListener l)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
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);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
ActionListener aListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
AbstractButton aButton = (AbstractButton) event.getSource();
boolean selected = aButton.getModel().isSelected();
String newLabel;
Icon newIcon;
if (selected) {
newLabel = "A";
} else {
newLabel = "B";
}
aButton.setText(newLabel);
}
};
caseMenuItem.addActionListener(aListener);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
JMenuItem: addChangeListener(ChangeListener l)
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.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
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);
fileMenu.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("File Menu Changed");
}
});
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New");
fileMenu.add(newMenuItem);
newMenuItem.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("new menu item changed");
}
});
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
JMenuItem: addItemListener(ItemListener l)
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
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);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
ItemListener iListener = new ItemListener() {
public void itemStateChanged(ItemEvent event) {
Icon girlIcon = new ImageIcon("g.jpg");
Icon boyIcon = new ImageIcon("b.jpg");
AbstractButton aButton = (AbstractButton)event.getSource();
int state = event.getStateChange();
String newLabel;
Icon newIcon;
if (state == ItemEvent.SELECTED) {
newLabel = "Girl";
newIcon = girlIcon;
} else {
newLabel = "Boy";
newIcon = boyIcon;
}
aButton.setText(newLabel);
aButton.setIcon(newIcon);
}
};
caseMenuItem.addItemListener(iListener);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
JMenuItem: addMenuDragMouseListener(MenuDragMouseListener l)
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 Main {
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);
}
}
JMenuItem: addMenuKeyListener(MenuKeyListener l)
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 Main {
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);
}
}
JMenuItem.RIGHT
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class MainClass extends JPanel {
public JPopupMenu popup;
public MainClass() {
popup = new JPopupMenu();
ActionListener menuListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
}
};
JMenuItem item;
popup.add(item = new JMenuItem("Left", new ImageIcon("left.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.addSeparator();
popup.add(item = new JMenuItem("Settings . . ."));
item.addActionListener(menuListener);
popup.setLabel("Justification");
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
popup.addPopupMenuListener(new PopupPrintListener());
addMouseListener(new MousePopupListener());
}
// An inner class to check whether mouse events are the popup trigger
class MousePopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
checkPopup(e);
}
public void mouseClicked(MouseEvent e) {
checkPopup(e);
}
public void mouseReleased(MouseEvent e) {
checkPopup(e);
}
private void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(MainClass.this, e.getX(), e.getY());
}
}
}
// An inner class to show when popup events occur
class PopupPrintListener implements PopupMenuListener {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
System.out.println("Popup menu will be visible!");
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
System.out.println("Popup menu will be invisible!");
}
public void popupMenuCanceled(PopupMenuEvent e) {
System.out.println("Popup menu is hidden!");
}
}
public static void main(String s[]) {
JFrame frame = new JFrame("Popup Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new MainClass());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
JMenuItem: setAccelerator(KeyStroke key)
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Mnemonic/Accelerator Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Print");
KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P,
InputEvent.CTRL_MASK);
menuItem.setAccelerator(ctrlP);
menuItem.addActionListener(printAction);
menu.add(menuItem);
JButton fileButton = new JButton("About");
fileButton.setMnemonic(KeyEvent.VK_A);
fileButton.addActionListener(printAction);
frame.setJMenuBar(menuBar);
frame.add(fileButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
private static final Icon printIcon = new MyIcon();
PrintHelloAction() {
super("Print", printIcon);
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}
class MyIcon implements Icon {
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.drawString("jexp.ru", 0, 20);
}
}
JMenuItem: setMnemonic(char mnemonic)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
public class MainClass extends JPanel {
public JTextPane pane;
public JMenuBar menuBar;
public MainClass() {
menuBar = new JMenuBar();
JMenu formatMenu = new JMenu("Justify");
formatMenu.setMnemonic("J");
MenuAction leftJustifyAction = new MenuAction("Left", new ImageIcon("left.gif"));
MenuAction rightJustifyAction = new MenuAction("Right", new ImageIcon("right.gif"));
MenuAction centerJustifyAction = new MenuAction("Center", new ImageIcon("center.gif"));
MenuAction fullJustifyAction = new MenuAction("Full", new ImageIcon("full.gif"));
JMenuItem item;
item = formatMenu.add(leftJustifyAction);
item.setMnemonic("L");
item = formatMenu.add(rightJustifyAction);
item.setMnemonic("R");
item = formatMenu.add(centerJustifyAction);
item.setMnemonic("C");
item = formatMenu.add(fullJustifyAction);
item.setMnemonic("F");
menuBar.add(formatMenu);
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
}
class MenuAction extends AbstractAction {
public MenuAction(String text, Icon icon) {
super(text, icon);
}
public void actionPerformed(ActionEvent e) {
try {
pane.getStyledDocument().insertString(0,
"Action [" + e.getActionCommand() + "] performed!\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String s[]) {
MainClass example = new MainClass();
example.pane = new JTextPane();
example.pane.setPreferredSize(new Dimension(250, 250));
example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
JFrame frame = new JFrame("Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(example.menuBar);
frame.getContentPane().add(example.pane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
new JMenuItem(Action act)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Action Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Action printAction = new PrintHelloAction();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
menu.add(new JMenuItem(printAction));
JToolBar toolbar = new JToolBar();
toolbar.add(new JButton(printAction));
JButton enableButton = new JButton("Enable");
ActionListener enableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(true);
}
};
enableButton.addActionListener(enableActionListener);
JButton disableButton = new JButton("Disable");
ActionListener disableActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.setEnabled(false);
}
};
disableButton.addActionListener(disableActionListener);
JButton relabelButton = new JButton("Relabel");
ActionListener relabelActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
printAction.putValue(Action.NAME, "Hello, World");
}
};
relabelButton.addActionListener(relabelActionListener);
JPanel buttonPanel = new JPanel();
buttonPanel.add(enableButton);
buttonPanel.add(disableButton);
buttonPanel.add(relabelButton);
frame.setJMenuBar(menuBar);
frame.add(toolbar, BorderLayout.SOUTH);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class PrintHelloAction extends AbstractAction {
PrintHelloAction() {
super("Print");
putValue(Action.SHORT_DESCRIPTION, "Hello, World");
}
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Hello, World");
}
}