Menus are made of three pieces:
This example makes a menu that has these options
File | Navigate |
Exit | Opening |
Rules | |
Play Game | |
Help |
This code makes the main MenuBar. It also makes a menu and menuItem to build the menus with.
JMenuBar menuBar = new JMenuBar (); JMenu menu; JMenuItem menuItem;
This makes the File Menu:
menu = new JMenu ("File"); menuBar.add (menu);menuItem = new JMenuItem ("Close"); menuItem.addActionListener (this); menuItem.setActionCommand ("Close"); menu.add (menuItem);
This makes the Navigate Menu:
menu = new JMenu ("Navigate"); menuBar.add (menu);menuItem = new JMenuItem ("Opening"); menuItem.addActionListener (this); menuItem.setActionCommand ("1"); menu.add (menuItem);menuItem = new JMenuItem ("Rules"); menuItem.addActionListener (this); menuItem.setActionCommand ("2"); menu.add (menuItem);menuItem = new JMenuItem ("Play Game"); menuItem.addActionListener (this); menuItem.setActionCommand ("3"); menu.add (menuItem);menuItem = new JMenuItem ("Help"); menuItem.addActionListener (this); menuItem.setActionCommand ("4"); menu.add (menuItem);
Then, the menu must be added to the applet. It assumes that you have a borderlayout.
add ("North", menuBar);
Finally, you need to handle the events in ActionPerformed:
public void actionPerformed (ActionEvent e) { if (e.getActionCommand ().equals ("4")) cdLayout.show (p_card, "4"); else if (e.getActionCommand ().equals ("2")) cdLayout.show (p_card, "2"); else if (e.getActionCommand ().equals ("3")) cdLayout.show (p_card, "3"); else if (e.getActionCommand ().equals ("1")) cdLayout.show (p_card, "1"); else if (e.getActionCommand ().equals ("Close")) System.exit (0); }
The complete code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuBase extends Applet implements ActionListener
{
Panel p_card; //to hold all of the cards
Panel card1, card2, card3, card4; //the four screens
CardLayout cdLayout = new CardLayout ();
public void init ()
{
p_card = new Panel ();
p_card.setLayout (cdLayout);
//make the 4 screens
initOpening ();
initRules ();
initGame ();
initHelp ();
resize (400, 260);
setLayout (new BorderLayout ());
initMenu (); //<---------
add ("Center", p_card);
}
public void initMenu ()
{
JMenuBar menuBar = new JMenuBar ();
JMenu menu;
JMenuItem menuItem;
menu = new JMenu ("File");
menuBar.add (menu);
menuItem = new JMenuItem ("Close");
menuItem.addActionListener (this);
menuItem.setActionCommand ("Close");
menu.add (menuItem);
menu = new JMenu ("Navigate");
menuBar.add (menu);
menuItem = new JMenuItem ("Opening");
menuItem.addActionListener (this);
menuItem.setActionCommand ("1");
menu.add (menuItem);
menuItem = new JMenuItem ("Rules");
menuItem.addActionListener (this);
menuItem.setActionCommand ("2");
menu.add (menuItem);
menuItem = new JMenuItem ("Play Game");
menuItem.addActionListener (this);
menuItem.setActionCommand ("3");
menu.add (menuItem);
menuItem = new JMenuItem ("Help");
menuItem.addActionListener (this);
menuItem.setActionCommand ("4");
menu.add (menuItem);
add ("North", menuBar);
}
public void initOpening ()
{ //Pre: p_card is a cdLayout, card1 is declared
//Post: initializes opening screen's widgets.
card1 = new Panel ();
card1.setBackground (Color.white);
JLabel title = new JLabel ("My Game");
JButton next = new JButton ("Next");
next.setActionCommand ("2");
next.addActionListener (this);
card1.add (title);
card1.add (next);
p_card.add ("1", card1);
}
public void initRules ()
{ //Pre: p_card is a cdLayout, card1 is declared
//Post: initializes opening screen's widgets.
card2 = new Panel ();
card2.setBackground (Color.orange);
JLabel title = new JLabel ("Rules");
JButton next = new JButton ("Next");
next.setActionCommand ("3");
next.addActionListener (this);
card2.add (title);
card2.add (next);
p_card.add ("2", card2);
}
public void initGame ()
{ //Pre: p_card is a cdLayout, card1 is declared
//Post: initializes opening screen's widgets.
card3 = new Panel ();
card3.setBackground (Color.yellow);
JLabel title = new JLabel ("The Actual Game");
JButton next = new JButton ("Next");
next.setActionCommand ("4");
next.addActionListener (this);
card3.add (title);
card3.add (next);
p_card.add ("3", card3);
}
public void initHelp ()
{ //Pre: p_card is a cdLayout, card1 is declared
//Post: initializes opening screen's widgets.
card4 = new Panel ();
card4.setBackground (Color.pink);
JLabel title = new JLabel ("Help");
JButton next = new JButton ("Next");
next.setActionCommand ("1");
next.addActionListener (this);
card4.add (title);
card4.add (next);
p_card.add ("4", card4);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("4"))
cdLayout.show (p_card, "4");
else if (e.getActionCommand ().equals ("2"))
cdLayout.show (p_card, "2");
else if (e.getActionCommand ().equals ("3"))
cdLayout.show (p_card, "3");
else if (e.getActionCommand ().equals ("1"))
cdLayout.show (p_card, "1");
else if (e.getActionCommand ().equals ("Close"))
System.exit (0);
}
}