Sunday, March 8, 2009

Monday, March 9: Menus Part three

OK, so by now you have a program that has one menubar, one menu, and one menuitem in that menu that closes the program. Now it's to time to get serious, and do the following:
A) add a second menu

B) add one menuitem to that menu

C) give that item an actionlistener, and put in a new "else if" in your action performed that makes this menu item DO something. In this case, its going to open a window, which is a frame that has not border and no standard controls



Here's how we do that:



1) right below the line that declares a frame

Frame Gumby; //declare Frame

declare a window

Window helpWindow; //declare a window



2) right below the line that declares a menu called "myRoom" declare a menu called OMGhelp



3) You have a long list of menuitems declared: at the end of it but before the ";", add a comma then another menuitem called "about"



OK, so now you've declared everything, now its time to put that stuff in memory. Go to the line that says Gumby.add(exit); and add these two lines:



4)
helpWindow = new Window(Gumby);//window has to be attached to a frame helpWindow.setBounds(600,200,200,200);



5) right below where you created the first menu, you will create the second one and add it to the menubar, like so:

OMGhelp = new Menu("HELP"); //create a Menu

myHouse.add(OMGhelp);//add the second menu to the menubar



6) Just below that, add in the code block which will add the new menuitem called "about" to this new menu, and give it an actionlistener:

about = new MenuItem("ABOUT"); //create a menu item

about.addActionListener(this);//give it a listener

OMGhelp.add(about); //add this menuitem to the new menu



7) Finally, see if you can figure out on your own how to make this menuitem cause that window to appear by adding in another "else if" to the actionPerfomed method



Cheers,

Uncle Paulie

No comments:

Post a Comment