Tải bản đầy đủ (.pptx) (34 trang)

SWING 5 menu (lập TRÌNH NÂNG CAO SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (202.88 KB, 34 trang )

PHẦN 2 - SWING

MENUS


Menu look

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

2/34


The Menu Component Hierarchy

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

3/34


The Menu Structure
JMenuBar: mainMenu

Jmenu: fileMenu

Jmenu: editMenu

Jmenu: ……..

JMenuItem: open

JMenuItem: close


JMenuItem: exit …

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

4/34


Creating Menus
 //in the constructor for a JFrame subclass:
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JCheckBoxMenuItem cbMenuItem;
JRadioButtonMenuItem rbMenuItem



//Create the menu bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

5/34


Creating 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(KeyEvent.VK_1,
ActionEvent.ALT_MASK));

menuItem.addActionListener(this);

menu.add(menuItem);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

6/34


Creating 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);
menu.add(menuItem);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

7/34


Creating Menus
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio

button menu item 1“);

rbMenuItem.setMnemonic(KeyEvent.VK_1);
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
rbMenuItem.addActionListener(this);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("A radio

button menu item 2“);

rbMenuItem.setMnemonic(KeyEvent.VK_2);
group.add(rbMenuItem);
rbMenuItem.addActionListener(this);
menu.add(rbMenuItem);


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

8/34


Creating Menus
//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box

menu item 1");

cbMenuItem.setMnemonic(KeyEvent.VK_C);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("A check box

menu item 2");

cbMenuItem.setMnemonic(KeyEvent.VK_A);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

9/34


Creating Menus
//a submenu

menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
submenu.add(menuItem);
menuItem = new JMenuItem("Another item in the submenu");
menuItem.addActionListener(this);
submenu.add(menuItem);
menu.add(submenu);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

10/34


Creating Menus
// a Exit Menu Item
menu.addSeparator();
menuItem = new JMenuItem("Exit",KeyEvent.VK_E);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEv

ent.VK_X,

ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);


//Build second menu in the menu bar.
menu = new JMenu("Options");
menu.setMnemonic(KeyEvent.VK_P);
menuBar.add(menu);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

11/34


Handling Events from Menu Items


public class MenuDemo ... implements ActionListener, ItemListener {
...
public MenuDemo() {
//...for each JMenuItem instance:
menuItem.addActionListener(this);
... //for each JRadioButtonMenuItem:
rbMenuItem.addActionListener(this);
... //for each JCheckBoxMenuItem:
cbMenuItem.addItemListener(this);
...
}
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...

}

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

12/34


How to Write an Item Listener
//where initialization occurs
checkbox.addItemListener(this);
...
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
label.setVisible(true);
label.revalidate();
label.repaint();
} else {
label.setVisible(false);
}
}

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

13/34


The 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 handy methods:



Object getItem()
Returns the component-specific object associated with the item whose state changed. Often this
is a String containing the text on the selected item.



ItemSelectable getItemSelectable()
Returns the component that fired the item event. You can use this instead of the getSource
method.



int getStateChange()
Returns the new state of the item. The ItemEvent class defines two states: SELECTED and
DESELECTED.

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

14/34



How 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);

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

15/34


Action
 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);


Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

16/34


Enabling 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", KeyEvent.VK_T);
//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);
//Setting the accelerator:
menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_T, ActionEvent.ALT_MASK));

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

17/34



Enabling 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:

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

18/34


Enabling 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())

}

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

19/34


The 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

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

20/34



The Menu API : javax.swing.JMenu
 public JMenu(String s)
Constructs a new JMenu with the supplied string as its text.
Parameters:s - the text for the menu label

 public JMenu(Action a)
Constructs a menu whose properties are taken from the Action supplied.
Parameters:a - an Action

 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 given position. If index equals -1,
the component will be appended to the end.

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

21/34


The 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

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

22/34


The 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 the item as the specified position is not a menu item

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

23/34


The 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.

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

24/34


The 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
icon - the icon of the JMenuItem

Khoa CNTT – ĐH Nông Lâm TP. HCM 2014

25/34


×