INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM ICS201, SECTIONS Summer Sesstion (003 Semester 2001) INTRODUCTION TO COMPUTER SCIENCE LAB #07 GUI Programming Adding Menu to GUI Applications Instructor: Mustafa A. AbuOsba Objectives: MnuBar, Menu, MenuItem Menu Menus make selection easier, and are widely used in window applications.. In Java, menus can only appear on a frame. Java provides three classes- MenuBar, Menu,n and MenuItem to implement menus in a frame The MenuBar class holds Menus. You use the add() method to add a Menu to the MenuBar, and you associate a MenuBar with a Frame by using the Frame method by using the Frame method setmenubar(menubar mb) A frame can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select. Menu bars can be viewed as a structure to support menus. Menus Java provides several classesjmenubar, JMenu, JMenuItem, JCheckBoxMenuItem, and JRadioButtonMenuItem to implement menus in a frame. A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select (or toggle on or off). Menu bars can The JMenuBar Class A menu bar holds menus; the menu bar can only be added to a frame. Following is the code to create and add a JMenuBar to a frame: JFrame f = new JFrame(); f.setsize(300, 200); f.setvisible(true); JMenuBar mb = new JMenuBar(); f.setjmenubar(mb); The Menu ClassYou attach menus onto a JMenuBar. The following code creates two menus, File and Help, and adds them to the JMenuBar mb: JMenu filemenu = new JMenu("File", false); JMenu helpmenu = new JMenu("Help", true); mb.add(filemenu); mb.add(helpmenu); The JMenuItem ClassYou add menu items on a menu. The following code adds menu items and item separators in menu filemenu: filemenu.add(new JMenuItem("new")); filemenu.add(new JMenuItem("open")); filemenu.add(new JMenuItem("-")); filemenu.add(new JMenuItem("print")); filemenu.add(new JMenuItem("exit")); 1
filemenu.add(new JMenuItem("-")); Submenus JMenu softwarehelpsubmenu = new JMenu("Software"); JMenu hardwarehelpsubmenu = new JMenu("Hardware"); helpmenu.add(softwarehelpsubmenu); helpmenu.add(hardwarehelpsubmenu); softwarehelpsubmenu.add(new JMenuItem("Unix")); softwarehelpsubmenu.add(new JMenuItem("NT")); softwarehelpsubmenu.add(new JMenuItem("Win95")); 2
Example: the following creates the below menu import javax.swing.*; class DemonstratingMenus extends JFrame { private JMenuBar menubar = new JMenuBar(); private JMenu filemenu = new JMenu("File"); private JMenuItem new, open, close, save, print; private JMenuItem savecurrent, saveas, saveall; DemonstratingMenus(String title){ super(title); setjmenubar(menubar); menubar.add(filemenu); filemenu.setmnemonic('f'); filemenu.add(new = new JMenuItem ("New")); filemenu.add(open = new JMenuItem ("Open")); open.setmnemonic('o'); filemenu.add(save = new JMenu ("Save")); save.add(savecurrent = new JMenuItem ("Save Current")); save.add(saveas = new JMenuItem ("Save As")); save.add(saveall = new JMenuItem ("Save All")); filemenu.add(save); filemenu.add(print = new JCheckBoxMenuItem ("Print")); filemenu.addseparator(); filemenu.add("quit"); setsize(400,400); show(); public static void main(string args[]){ new DemonstratingMenus("Demonstrating Menus using Swing Components."); 3
import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.util.random; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jpanel; public class MenuTest { public static void main(string[] args) { MenuFrame frame = new MenuFrame(); frame.settitle("menutest"); frame.show(); class MenuFrame extends JFrame { public MenuFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); addwindowlistener(new WindowCloser()); // add drawing panel to content pane panel = new RectanglePanel(); Container contentpane = getcontentpane(); contentpane.add( panel, "Center"); // construct menu JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu filemenu = new JMenu("File"); menubar.add(filemenu); MenuListener listener = new MenuListener(); newmenuitem = new JMenuItem("New"); filemenu.add(newmenuitem); newmenuitem.addactionlistener(listener); exitmenuitem = new JMenuItem("Exit"); filemenu.add(exitmenuitem); exitmenuitem.addactionlistener(liste ner); JMenu editmenu = new JMenu("Edit"); menubar.add(editmenu); JMenuItem movemenu = new JMenu("Move"); editmenu.add(movemenu); upmenuitem = new JMenuItem("Up"); movemenu.add(upmenuitem); upmenuitem.addactionlistener(listener); downmenuitem = new JMenuItem("Down"); movemenu.add(downmenuitem); downmenuitem.addactionlistener(listener); leftmenuitem = new JMenuItem("Left"); movemenu.add(leftmenuite m); leftmenuitem.addactionlistener(listener); 4
rightmenuitem = new JMenuItem("Right"); movemenu.add(rightmenuitem); rightmenuitem.addactionlistener(listener); randomizemenuitem = new JMenuItem("Randomize"); editmenu.add(randomizemenuitem); randomizemenuitem.addactionlistener(listener); private JMenuItem exitmenuitem; private JMenuItem newmenuitem; private JMenuItem upmenuitem; private JMenuItem downmenuitem; private JMenuItem leftm enuitem; private JMenuItem rightmenuitem; private JMenuItem randomizemenuitem; private RectanglePanel panel; private class MenuListener implements ActionListener { public void actionperformed(actionevent event) { // find the menu that was selected Object source = event.getsource(); if (source == exitmenuitem) System.exit(0); else if (source == newmenuitem) panel.reset(); else if (source == upmenuitem) panel.moverectangle(0, -1); else if (source == downmenuitem) panel.moverectangle(0, 1); else if (source == leftmenuitem) panel.moverectangle(-1, 0); else if (source == rightmenuitem) panel.moverectangle(1, 0); else if (source == randomizemenuitem) panel.randomize(); private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent event) { System.exit(0); class RectanglePanel extends JPanel { public RectanglePanel() { rect = new Rectangle(0, 0, RECT_WIDTH, RECT_HEIGHT); public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D)g; g2.draw(rect); /** Resets the rectangle to the top left corner. */ public void reset() { rect.setlocation(0, 0); repaint(); /** Moves the rectangle to a random position. */ public void randomize() { Random generator = new Random(); rect.setlocation(generator.nextint(getwidth()), generator.nextint(getheight())); repaint(); /** Moves the rectangle and repaints it. The rectangle is moved by multiples of its full width or height. @param dx the number of width units @param dy the number of height units */ public void moverectangle(int dx, int dy) { rect.translate(dx * RECT_WIDTH, dy * RECT_HEIGHT); repaint(); private Rectangle rect; private static final int RECT_WIDTH = 20; private static final int RECT_HEIGHT = 30; 5
Exercise #1 Create the below menu Exercise #2 nversion km=miles * 1.609 1 lbs=0.45359 kg one kilogram = 2.2 pounds HOMEWORK#2 create a menu bar, that holds two nemus: operationmnue and exitmenu. The operationmenu contains four menu items:add, Sbutract, Multiply and Divide for performing arithmetic, The exitmenu contains the menu item Close for exiting the program The user enters two numbers in the number fields Create a user interface that performs arithmetic. The interface contains labels and text fields for Number 1, Number 2, and Result. The Result box displays the result of the arithmetic operation between Number 1 and Number 2. as shown below. 6