Drop-down menus help to organize and reduce the clutter of command options. GUI-based programs add labeled menus to a menu bar, and then add labeled command options to a menu. In a BreezySwing application, the programmer adds a command option, also called a menu item, to a menu in one operation, which has the form
menuItem = addMenuItem(menuName,
menuItemName)
This method expects two
strings as arguments and returns an inance of the Java class JMenuItem. The next code segment adds a simple File menu, with the options New, Open, and Save, and an Edit menu, with the options Copy, Cut, and Paste, to the window's menu bar.
JMenuItem newMI = addMenuItem("File",
"New");
JMenuItem openMI = addMenuItem("File", "Open");
JMenuItem saveMI = addMenuItem("File", "Save");
JMenuItem copyMI = addMenuItem("Edit", "Copy");
JMenuItem cutMI = addMenuItem("Edit",
"Cut");
JMenuItem pasteMI = addMenuItem("Edit",
"Paste");
The menus are added to the
menu bar, from left to right, in the order in which their names appear in your
code. Likewise for the menu items, from top to bottom within each menu.
BreezySwing includes a method named menuItemSelected to respond to the user's menu item
selections. When the user
selects a name in a drop-down menu, this event handler method is triggered. As
with button clicks, the user overrides this method to determine which option
was selected and to provide the appropriate response.
To demonstrate the use of
menus, the next example program displays a label and a set of menus to set the
label's font and color properties. A font has three basics properties: a name,
a style, and a size. A label has a font and a foreground color as properties. The
program's menus are named Name,
Size, and Color (we ignore the style for
now). When the user selects a menu option, the program updates the label with
the changes. Here is a screen shot of the window, followed by the code for the
program:
public class MenuDemo
extends GBFrame{
// Add the window components
JLabel
textLabel = addLabel
("Text font", 1,1,1,1);
JMenuItem
itemCourier = addMenuItem
("Name", "Courier");
JMenuItem
itemVerdana = addMenuItem
("Name", "Verdana");
JMenuItem
item14 = addMenuItem ("Size", "14");
JMenuItem
item24 = addMenuItem ("Size", "24");
JMenuItem
itemRed = addMenuItem
("Color", "Red");
JMenuItem
itemBlue = addMenuItem
("Color", "Blue");
// The event handler method for
menu items
public
void menuItemSelected(JMenuItem
menuItem){
Font font = textLabel.getFont();
String
name = font.getFontName();
int style = font.getStyle();
int size = font.getSize();
Color color = textLabel.getForeground();
if (menuItem == itemVerdana)
textLabel.setFont
(new Font ("Verdana", style, size));
else if (menuItem == itemCourier)
textLabel.setFont
(new Font ("Courier", style, size));
else if (menuItem == item14)
textLabel.setFont
(new Font (name, style, 14));
else if (menuItem == item24)
textLabel.setFont
(new Font (name, style, 24));
else if (menuItem == itemRed)
textLabel.setForeground
(Color.red);
else if (menuItem == itemBlue)
textLabel.setForeground
(Color.blue);
}
// Create and display the window
when the app launches
public
static void main(String[] args){
JFrame frm = new MenuDemo();
frm.setTitle("Text Fonts");
frm.setSize (200, 100);
frm.setVisible (true);
}
}
Back to tutorial | Next topic: Text areas |