Swing enables you to create menus, menu bars, and menu items for applications, using the subclasses of the
JComponent
and JAbstractButton
classes.
A menu enables users to choose one of several options. There are two types of menus:
- menu bar
- pop-up
- menu bar
- A menu bar contains one or more menus and is usually placed at the top of a window.
- pop-up
- A pop-up - shortcut - menu appears only when you perform a mouse action, such as clicking the right mouse button.
Swing contains several menu-related components:
- menu bars
- menus
- menu items
- radio button menu items
- checkbox menu items
- separators
The menu-related classes -
JMenuBar
, JPopUpMenu
, and JSeparator
- inherit directly from the JComponent
class.
The
JMenuBar
class enables you to create a menu bar.
JMenuBar menuBar = new JMenuBar() ;
After you create a menu bar, you can add menu items to it. Menu items are buttons that can display text or an image, or both.
Because a menu is a button, it displays menu items by automatically bringing up a pop-up menu when activated.
Because a menu is a button, it displays menu items by automatically bringing up a pop-up menu when activated.
You can add menu items to menus using the following menu-related classes:
JMenu
JMenuItem
JCheckboxMenuItem
JRadioButtonMenuItem
JMenu
- The
JMenu
class is a subclass ofJMenuItem
. You can create an empty menu, a menu that contains text, or a menu from a specificAction
object.
The syntax for the constructor is
public JMenu()
public JMenu(String str)
public JMenu(Action act) JMenuItem
- The
JMenuItem
class inherits directly from theJAbstractButton
class. You can create a menu item that displays text, an image, or both. You can also pass it an integer argument, which specifies the keyboard alternative to use. AnAction
parameter in theJMenuItem
constructor sets the action for the menu item.
The syntax for the constructor is
public JMenuItem()
public JMenuItem(String st)
public JMenuItem(Icon ic)
public JMenuItem(String st, Icon ic)
public JMenuItem(String st, int in)
public JMenuItem(Action ac) JCheckboxMenuItem
- The
JCheckboxMenuItem
class inherits from theJMenuItem
class. This class enables you to create a menu item that looks and acts like a checkbox. You can create a checkbox item and specify a text string, an icon, or both, for the menu item. You can also pass a boolean argument that indicates whether the checkbox is initially selected.
The syntax for the constructor is
public JCheckboxMenuItem()
public JCheckboxMenuItem(String str)
public JCheckboxMenuItem(Icon ico)
public JCheckboxMenuItem(String str, Icon ico)
public JCheckboxMenuItem(String str, boolean bool)
public JCheckboxMenuItem(String str, Icon ico, boolean bool) JRadioButtonMenuItem
- The
JRadioButtonMenuItem
class is a subclass ofJMenuItem
. You use this class to create a menu item that looks and acts like a radio button. When creating a radio button, you can specify a text string, an image, or both. You can also pass a boolean argument that indicates whether the radio button is initially selected.
The syntax for the constructor is
public JRadioButtonMenuItem()
public JRadioButtonMenuItem(String str)
public JRadioButtonMenuItem(Icon ico)
public JRadioButtonMenuItem(String str, Icon ico)
public JRadioButtonMenuItem(String str, boolean bool)
public JRadioButtonMenuItem(String str, Icon ico, boolean bool)
To create a menu, you need to perform the following steps:
- create a menu bar
- create a menu
- add the menu to the menu bar
- add menu items to the menu
Suppose you are creating a basic menu application with two drop-down menus - File and Edit. Both these menus contain text menu items.
You start by creating a menu bar to contain the menus.
You start by creating a menu bar to contain the menus.
public class UsingMenus { JTextArea display ; JScrollPane scroller ; public JMenuBar createMenuBar() { JMenuBar menuBar ; JMenu menu ; JMenuItem menuItem ; //Step 1: Create the menu bar menuBar = new JMenuBar() ;
Then you create a File menu.
//Step 2: Build a menu and add to menu bar menu = new JMenu ("File") ;
You can now add the menu to the menu bar.
public class UsingMenus { JTextArea display ; JScrollPane scroller ; public JMenuBar createMenuBar() { JMenuBar menuBar ; JMenu menu ; JMenuItem menuItem ; //Step 1: Create the menu bar menuBar = new JMenuBar() ; //Step 2: Build a menu and add to menu bar menu = new JMenu ("File") ; //Step 3: Add the menu to the menu bar MISSING CODE ;
You type
menuBar.add(menu)
.
You use the
add
method of the JMenuBar
class to add the menu to the menu bar.public class UsingMenus { JTextArea display ; JScrollPane scroller ; public JMenuBar createMenuBar() { JMenuBar menuBar ; JMenu menu ; JMenuItem menuItem ; //Step 1: Create the menu bar menuBar = new JMenuBar() ; //Step 2: Build a menu and add to menu bar menu = new JMenu ("File") ; //Step 3: Add the menu to the menu bar menuBar.add(menu) ;
Next you create three menu items - New, Open, and Save - and add them to the File menu using the
You specify a keyboard alternative, as a second argument, for each menu item.
add
method of the JMenu
class.You specify a keyboard alternative, as a second argument, for each menu item.
public JMenuBar createMenuBar() { JMenuBar menuBar ; JMenu menu ; JMenuItem menuItem ; //Step 1: Create the menu bar menuBar = new JMenuBar() ; //Step 2: Build a menu and add to menu bar menu = new JMenu ("File") ; //Step 3: Add the menu to the menu bar menuBar.add(menu) ; //Step 4: Add menu items menuItem = new JMenuItem("New", KeyEvent.VK_N) ; menu.add(menuItem) ; menuItem = new JMenuItem("Open", KeyEvent.VK_O) ; menu.add(menuItem) ; menuItem = new JMenuItem("Save", KeyEvent.VK_S) ; menu.add(menuItem) ;
Note
The
JMenu
class also provides insert
and remove
methods that enable you to add an item at a particular position in a menu or to remove a menu item from a menu.
Once you've created the File menu, you create the Edit menu and add it to the menu bar.
//Step 2: Build a menu and add to menu bar menu = new JMenu ("File") ; //Step 3: Add the menu to the menu bar menuBar.add(menu) ; //Step 4: Add menu items menuItem = new JMenuItem("New", KeyEvent.VK_N) ; menu.add(menuItem) ; menuItem = new JMenuItem("Open", KeyEvent.VK_O) ; menu.add(menuItem) ; menuItem = new JMenuItem("Save", KeyEvent.VK_S) ; menu.add(menuItem) ; //Build second menu in the menu bar. menu = new JMenu("Edit") ; menuBar.add(menu) ;
Then you create two menu items - Find and Replace - and add them to the Edit menu using the
add
method of the JMenu
class.menuItem = new JMenuItem("Find", KeyEvent.VK_F) ; menu.add(menuItem) ; menuItem = new JMenuItem("Replace", KeyEvent.VK_R) ; menu.add(menuItem) ;
You have now created a menu bar containing two menus with menu items.
0 comments:
Post a Comment