The Menu API : javax.swing.JMenu public JMenuItem addString s Creates a new menu item with the specified text and appends it to the end of this menu.. Parameters:s - the string for the
Trang 1PHẦN 2 - SWING
MENUS
Trang 2Menu look
Trang 3The Menu Component Hierarchy
Trang 4The Menu Structure
Trang 5 //Create the menu bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);
Trang 6Creating Menus
//Build the first menu.
menu = new JMenu(“File");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("New ", KeyEvent.VK_N);
//used constructor instead
//menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyE vent.VK_1, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
Trang 7Creating Menus
//Menu item with Text and Icon
menuItem = new JMenuItem("Open ",
new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_O);
menuItem.addActionListener(this);
menu.add(menuItem);
//Menu item has only a Icon
menuItem = new JMenuItem(new
ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_D);
menuItem.addActionListener(this);
Trang 8Creating Menus
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio
button menu item 1“);
rbMenuItem = new JRadioButtonMenuItem("A radio
button menu item 2“);
rbMenuItem.setMnemonic(KeyEvent.VK_2);
group.add(rbMenuItem);
rbMenuItem.addActionListener(this);
menu.add(rbMenuItem);
Trang 11//Build second menu in the menu bar.
menu = new JMenu("Options");
menu.setMnemonic(KeyEvent.VK_P);
menuBar.add(menu);
Trang 12Handling Events from Menu Items
public class MenuDemo implements ActionListener,
public void actionPerformed(ActionEvent e) {
// Get information from the action event
// Display it in the text area }
public void itemStateChanged(ItemEvent e) {
// Get information from the item event
// Display it in the text area
}
Trang 13How to Write an Item Listener
//where initialization occurs
Trang 14The Item Event API
void itemStateChanged(ItemEvent)
Called just after a state change in the listened-to component
The itemStateChanged method hod has a single parameter: an ItemEvent object The ItemEvent class defines the following
Trang 15How to Use Actions
If you have two or more components that perform the same function, consider using an Action object to
implement the function An Action object is an
ActionListener that provides not only action-event
handling, but also centralized handling of the text ,
icon , and enabled state of tool bar buttons or menu
items By adding an Action to a JToolBar , JMenu , or
JPopupMenu , you should to use Actions
Action leftAction = new AbstractAction ( );
Button button = toolBar.add( leftAction );
JMenuItem menuItem = mainMenu.add( leftAction );
Trang 16 Action exitAction = new AbstractAction("Exit") {
public void actionPerformed(ActionEvent event)
{ // action code goes here
System.exit(0);
} };
You can then add the action to the menu:
JMenuItem exitItem = fileMenu.add(exitAction);
This command adds a menu item to the menu, using the action name The action object becomes its
listener This is just a convenient shortcut for
JMenuItem exitItem = new JMenuItem(exitAction); fileMenu.add(exitItem);
Trang 17Enabling Keyboard Operation
Menus support two kinds of keyboard alternatives:
mnemonics and accelerators
You can specify a mnemonic either when constructing the menu item or with the setMnemonic method To specify an accelerator, use the setAccelerator method Here are
examples of setting mnemonics and accelerators:
//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
Trang 18Enabling and Disabling Menu Items
To enable or disable a menu item, use the setEnabled
method:
saveItem.setEnabled(false);
There are two strategies for enabling and disabling menu items Each time circumstances change, you can call
setEnabled on the relevant menu items or actions For
example, as soon as a document has been set to
read-only mode, you can locate the Save and Save As menu
items and disable them Alternatively, you can disable
items just before displaying the menu To do this, you
must register a listener for the "menu selected" event The javax.swing.event package defines a MenuListener
interface with three methods:
Trang 19Enabling and Disabling Menu Items
void menuSelected(MenuEvent evt)
void menuDeselected(MenuEvent evt)
void menuCanceled(MenuEvent evt)
The menuSelected method is called before the menu is displayed It can therefore be used to disable or enable menu items The following code shows how to disable the Save and Save As actions whenever the Read Only check box menu item is selected:
public void menuSelected(MenuEvent evt) {
saveAction.setEnabled(!readonlyItem.isSelected());
saveAsAction.setEnabled(!readonlyItem.isSelected())
Trang 20The Menu API : javax.swing.JMenuBar
public JMenuBar()
Creates a new menu bar
public JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar Parameters:c - the JMenu component to add
Returns:the menu component
public JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar Parameters:c - the JMenu component to add
Returns:the menu component
public int getMenuCount()
Returns the number of items in the menu bar Returns:the number of items in the menu bar
Trang 21The Menu API : javax.swing.JMenu
public JMenuItem add(JMenuItem menuItem)
Appends a menu item to the end of this menu
Returns the menu item added
Parameters:menuItem - the JMenuitem to be added
Returns:the JMenuItem added
public Component add(Component c, int index)
Adds the specified component to this container at the
Trang 22The Menu API : javax.swing.JMenu
public JMenuItem add(String s)
Creates a new menu item with the specified text and
appends it to the end of this menu
Parameters:s - the string for the menu item to be added
public JMenuItem add(Action a)
Creates a new menu item attached to the specified Action object and appends it to the end of this menu
Parameters:a - the Action for the menu item to be added
public void addSeparator()
Appends a new separator to the end of the menu
public void insert(String s, int pos)
Inserts a new menu item with the specified text at a given position
Parameters:s - the text for the menu item to addpos - an integer specifying the position at which to add the new
menu item
Trang 23The Menu API : javax.swing.JMenu
public JMenuItem insert(JMenuItem mi,int pos)
Inserts the specified JMenuitem at a given position
Parameters:mi - the JMenuitem to addpos - an integer
specifying the position at which to add the new Jmenuitem
public JMenuItem insert(Action a, int pos)
Inserts a new menu item attached to the specified Action object at a given position
Parameters:a - the Action object for the menu item to
addpos - an integer specifying the position at which to add the new menu item
public JMenuItem getItem(int pos)
Returns the JMenuItem at the specified position If the
component at pos is not a menu item, null is returned
Parameters:pos - an integer specifying the position
Returns:the menu item at the specified position; or null if
Trang 24The Menu API : javax.swing.JMenu
public void remove(int pos)
Removes the menu item at the specified index from this menu
Parameters:pos - the position of the item to be
removed
public void removeAll()
Removes all menu items from this menu
public void addMenuListener(MenuListener l)
Adds a listener for menu events
Trang 25The Menu API : javax.swing.JMenuItem
public JMenuItem(Icon icon)
Creates a JMenuItem with the specified icon
Parameters:icon - the icon of the JMenuItem
public JMenuItem(String text)
Creates a JMenuItem with the specified text
Parameters:text - the text of the JMenuItem
public JMenuItem(Action a)
Creates a menu item whose properties are taken from the specified Action
Parameters:a - the action of the JMenuItem
public JMenuItem(String text, Icon icon)
Creates a JMenuItem with the specified text and icon
Parameters:text - the text of the JMenuItem
Trang 26The Menu API : javax.swing.JMenuItem
public JMenuItem(String text, int mnemonic)
Creates a JMenuItem with the specified text and keyboard mnemonic
Parameters:text - the text of the JMenuItem
mnemonic - the keyboard mnemonic for the JMenuItem
public void setEnabled(boolean b)
Enables or disables the menu item
public void setAccelerator(KeyStroke keyStroke)
Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy
Note that when the keyboard accelerator is typed, it will work whether or not the menu is currently displayed
Parameters:keyStroke - the KeyStroke which will serve as
an accelerator
Trang 27The Menu API : javax.swing.JFrame
void setJMenuBar(JMenuBar menubar)
sets the menu bar for this frame.
Trang 28 public JCheckBoxMenuItem(Icon icon)
Creates an initially unselected check box menu item with
an icon
Parameters:icon - the icon of the CheckBoxMenuItem
public JCheckBoxMenuItem(String text)
Creates an initially unselected check box menu item with text
Parameters:text - the text of the CheckBoxMenuItem
public JCheckBoxMenuItem(Action a)
Creates a menu item whose properties are taken from the Action supplied
public JCheckBoxMenuItem(String text, Icon icon)
Creates an initially unselected check box menu item with the specified text and icon
Parameters:text - the text of the CheckBoxMenuItem
icon - the icon of the CheckBoxMenuItem
Trang 29Parameters:text - the text of the check box menu item.
b - the selected state of the check box menu item
public JCheckBoxMenuItem(String text, Icon icon,
boolean b)
Creates a check box menu item with the specified text,
icon, and selection state
Parameters:text - the text of the check box menu item
icon - the icon of the check box menu item
b - the selected state of the check box menu item
Trang 30 public boolean getState()
Returns the selected-state of the item This method
exists for AWT compatibility only New code should
use isSelected() instead
Returns:true if the item is selected
public void setState(boolean b)
Sets the selected-state of the item This method exists for AWT compatibility only New code should use
setSelected() instead
Parameters:b - a boolean value indicating the item's selected-state, where true=selected
Trang 31 public JRadioButtonMenuItem(Icon icon)
Creates a JRadioButtonMenuItem with an icon
Parameters:icon - the Icon to display on the
JRadioButtonMenuItem
public JRadioButtonMenuItem(String text)
Creates a JRadioButtonMenuItem with text
Parameters:text - the text of the JRadioButtonMenuItem
public JRadioButtonMenuItem(Action a)
Creates a radio button menu item whose properties are
taken from the Action supplied
Parameters:a - the Action on which to base the radio button menu item
public JRadioButtonMenuItem(String text, Icon icon)
Creates a radio button menu item with the specified text and Icon
Parameters:text - the text of the JRadioButtonMenuItem
Trang 32Parameters:text - the text of the CheckBoxMenuItem
selected - the selected state of the CheckBoxMenuItem
public JRadioButtonMenuItem(Icon icon, boolean
selected)
Creates a radio button menu item with the specified image and selection state, but no text
Parameters:icon - the image that the button should display
selected - if true, the button is initially selected;
otherwise, the button is initially unselected
Trang 33 public JRadioButtonMenuItem(String text, Icon icon,boolean selected)
Creates a radio button menu item that has the
specified text, image, and selection state All other
constructors defer to this one
Parameters:text - the string displayed on the radio
button
icon - the image that the button should display
Trang 34 public ButtonGroup()
Creates a new ButtonGroup
public void add(AbstractButton b)
Adds the button to the group
Parameters:b - the button to be added
public void remove(AbstractButton b)
Removes the button from the group
Parameters:b - the button to be removed