Fri Feb 08, 2013 10:10 pm
import java.awt.*;
public class MenuWindow extends Frame {
boolean startApplet = true;
TextArea output;
public MenuWindow() {
MenuBar menuBar;
Menu menuOne, menuTwo, menuThree, menuFour, menuFour_1, menuFive;
MenuItem menuItem_1, menuItem_2, menuItem3_1, menuItem3_2, menuItem3_3, menuItem3_4,
menuItem4_1_1, menuItem5_1, menuItem5_2;
CheckboxMenuItem checkBoxMenuItem_1;
output = new TextArea(5, 30);
output.setEditable(false);
setLayout(new BorderLayout()); //give max space to the output
add("Center", output);
//Build the menu bar.
menuBar = new MenuBar();
setMenuBar(menuBar);
//Build first menu in the menu bar.
//Specifying the second argument as true
//makes this a tear-off menu.
menuOne = new Menu("Menu 1", true);
menuBar.add(menuOne);
menuItem_1 = new MenuItem("Menu Item 1_1");
menuOne.add(menuItem_1);
menuItem_2 = new MenuItem("Menu Item 1_2");
menuOne.add(menuItem_2);
//Build help menu.
menuFive = new Menu("Menu 5");
menuBar.add(menuFive); //just setting the help menu doesn't work; must add it
menuBar.setHelpMenu(menuFive);
menuItem5_1 = new MenuItem("Menu Item 5_1");
menuFive.add(menuItem5_1);
menuItem5_2 = new MenuItem("Menu Item 5_2");
menuFive.add(menuItem5_2);
//Build second menu in the menu bar.
menuTwo = new Menu("Menu 2");
menuBar.add(menuTwo);
checkBoxMenuItem_1 = new CheckboxMenuItem("Menu Item 2_1");
menuTwo.add(checkBoxMenuItem_1);
//Build third menu in the menu bar.
menuThree = new Menu("Menu 3");
menuBar.add(menuThree);
menuItem3_1 = new MenuItem("Menu Item 3_1");
menuThree.add(menuItem3_1);
menuItem3_2 = new MenuItem("Menu Item 3_2");
menuThree.add(menuItem3_2);
menuThree.addSeparator();
menuItem3_3 = new MenuItem("Menu Item 3_3");
menuThree.add(menuItem3_3);
menuItem3_4 = new MenuItem("Menu Item 3_4");
menuItem3_4.disable();
menuThree.add(menuItem3_4);
//Build fourth menu in the menu bar.
menuFour = new Menu("Menu 4");
menuBar.add(menuFour);
menuFour_1 = new Menu("Submenu 4_1");
menuFour.add(menuFour_1);
menuItem4_1_1 = new MenuItem("Menu Item 4_1_1");
menuFour_1.add(menuItem4_1_1);
}
public boolean handleEvent(Event event) {
if (event.id == Event.WINDOW_DESTROY) {
if (startApplet) {
dispose();
} else {
System.exit(0);
}
}
return super.handleEvent(event);
}
public boolean action(Event event, Object arg) {
String str = "Action detected";
if (event.target instanceof MenuItem) {
MenuItem mi=(MenuItem)(event.target);
str += " on " + arg;
if (mi instanceof CheckboxMenuItem) {
str += " (state is "
+ ((CheckboxMenuItem)mi).getState()
+ ")";
}
MenuContainer parent = mi.getParent();
if (parent instanceof Menu) {
str += " in " + ((Menu)parent).getLabel();
} else {
str += " in a container that isn't a Menu";
}
}
str += ".\n";
output.appendText(str);
return true;
}
public static void main(String[] args) {
MenuWindow window = new MenuWindow();
window.startApplet = false;
window.setTitle("MenuWindow Application");
window.pack();
window.show();
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.