In this Java AWT Tutorial, we will create Menus and Menu Items. Then we will hook those to the Menu bar. We will display the menu bar with these menu items in an AWT Frame Window. Moreover, we will handle the menu item click events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.Label; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class FrameWin extends Frame implements WindowListener, ActionListener { //Sample 01: Class Members Label lblDisplay; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(400, 400); setLocation(100,100); addWindowListener(this); //Sample 02: Add a Label Control lblDisplay = new Label(); lblDisplay.setBackground(Color.LIGHT_GRAY); add(BorderLayout.SOUTH, lblDisplay); //Sample 03: Add menu Bar MenuBar AwtMenuBar = new MenuBar(); setMenuBar(AwtMenuBar); //Sample 04: Create Menus Menu mnuHelp = new Menu("Help"); Menu mnuDraw = new Menu("Draw"); AwtMenuBar.add(mnuDraw); AwtMenuBar.add(mnuHelp); //Sample 05: Create Menu Items MenuItem siLine = new MenuItem("Line"); MenuItem miLine = new MenuItem("Multi Line"); MenuItem Rect = new MenuItem("Rectangle"); MenuItem Pentagon = new MenuItem("Pentagon"); MenuItem Circle = new MenuItem("Circle"); MenuItem help = new MenuItem("F1 Help"); MenuItem about = new MenuItem("About Menu Demo"); //Sample 06: Add Menu Items to Menu mnuDraw.add(siLine); mnuDraw.add(miLine); mnuDraw.add(Rect); mnuDraw.add(Pentagon); mnuDraw.add(Circle); mnuHelp.add(help); mnuHelp.add(about); //Sample 07: Set ActionCommand & register with Listeners siLine.setActionCommand("Draw Line"); miLine.setActionCommand("Draw Multi Line"); Rect.setActionCommand("Draw Rectangle"); Pentagon.setActionCommand("Draw Pentagon"); Circle.setActionCommand("Draw Circle"); help.setActionCommand("F1 Help"); about.setActionCommand("About Menu Demo"); //Sample 08: Add Action Listener siLine.addActionListener(this); miLine.addActionListener(this); Rect.addActionListener(this); Pentagon.addActionListener(this); Circle.addActionListener(this); help.addActionListener(this); about.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } @Override public void actionPerformed(ActionEvent ae) { //Sample 09: Set the Label lblDisplay.setText(ae.getActionCommand()); } } |
- AWT Drawing Part 5 – setXORMode & Rubber Banding of Line, Rect #27
- AWT CheckboxMenuItem and Separator #30
Categories: AWT-Tube