So basically, my JMenuBar has to appear everywhere in my applet (I have CardLayout, 3 cards/screens), in every screen and in the same location (looks nice, consistency). How would I go about doing this?
So, I was making a menu for my applet. I followed the exact instructions at:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
My initial approach was to place the entire menu code in the global part, but then my compiler said I needed { } around the entire menu code, which I added. Everything seemed to work fine...but then when I started adding stuff to the second and third screen (instructions and game, respectively), everything collapsed. It was as if the menu could only appear once. I tried making the menu "global" but I failed.
As of now, the code is dirty since I just copied and pasted the menu (code is below) into every screen. It works, but it's not efficient (as I can eliminate 150 lines).
The basic code of my applet is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.Insets;
public class Othello extends Applet implements ActionListener, MouseListener
JMenuBar menuBar; //Global variables
JMenu menu, submenu;
JMenuItem menuItem;
Panel screens;
Panel opening, instructions, game;
CardLayout cdLayout = new CardLayout ();
JTextArea textArea;
public void init ()
{
screens = new Panel ();
screens.setLayout (cdLayout);
opening ();
instructions ();
game ();
resize (800, 680); //Resizes applet
setLayout (new BorderLayout ());
add ("Center", screens);
}
public void opening ()
{
resize (800, 680);
opening = new Panel ();
//Menu code (below, in another CODE box. I removed it so the length of this code is shorter.
//Add widgets
opening.add (menuBar);
opening.add (grid); //Add every other widgets (not in this code)
screens.add ("1", opening);
}
public void instructions ()
{
resize (800, 680);
instructions = new Panel ();
//Menu
/Add widgets
instructions.add (menuBar);
screens.add ("2", instructions);
}
public void game ()
{
resize (800, 680);
game = new Panel ();
//Menu code
//Add widgets
grid.add (menuBar);
grid.add (board); //my othello board
game.add (grid);
screens.add ("3", game);
}
public void actionPerformed (ActionEvent e)
{
}
public void mousePressed (MouseEvent e)
{
saySomething ("Mouse pressed; # of clicks: "
+ e.getClickCount (), e);
}
public void mouseReleased (MouseEvent e)
{
saySomething ("Mouse released; # of clicks: "
+ e.getClickCount (), e);
}
public void mouseEntered (MouseEvent e)
{
saySomething ("Mouse entered", e);
}
public void mouseExited (MouseEvent e)
{
saySomething ("Mouse exited", e);
}
public void mouseClicked (MouseEvent e)
{
saySomething ("Mouse clicked (# of clicks: "
+ e.getClickCount () + ")", e);
}
void saySomething (String eventDescription, MouseEvent e)
{
textArea.append (eventDescription + " detected on "
+ e.getComponent ().getClass ().getName ()
+ "." + "\n");
}
//Method needed to display images, and produce an error if the image file is not found.
protected static ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = Othello.class.getResource (path);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}
In case the menu code is relevant (used above three times):
//Create the menu bar.
menuBar = new JMenuBar ();
//Build a menu category. FILE
menu = new JMenu ("File");
menu.setFont (new Font ("Arial", Font.BOLD, 14));
menu.setMnemonic (KeyEvent.VK_A);
menu.getAccessibleContext ().setAccessibleDescription ("The only menu in this program that has menu items");
menuBar.add (menu);
//File --> New
menuItem = new JMenuItem ("New", KeyEvent.VK_T);
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext ().setAccessibleDescription ("This doesn't really do anything");
menu.add (menuItem);
//File --> Exit Game
menuItem = new JMenuItem ("Exit Game", new ImageIcon ("images/middle.gif"));
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_F4, ActionEvent.ALT_MASK));
menuItem.setMnemonic (KeyEvent.VK_B);
menu.add (menuItem);
//Build a menu category. EDIT
menu = new JMenu ("Edit");
menu.setFont (new Font ("Arial", Font.BOLD, 14));
menu.setMnemonic (KeyEvent.VK_A);
menu.getAccessibleContext ().setAccessibleDescription ("The only menu in this program that has menu items");
menuBar.add (menu);
//Edit --> Maximize Window
menuItem = new JMenuItem ("Maximize Window", KeyEvent.VK_T);
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext ().setAccessibleDescription ("This doesn't really do anything");
menu.add (menuItem);
//Edit --> Minimize Window
menuItem = new JMenuItem ("Minimize Window", new ImageIcon ("images/middle.gif"));
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.setMnemonic (KeyEvent.VK_B);
menu.add (menuItem);
//Build a menu category. HELP
menu = new JMenu ("Help");
menu.setFont (new Font ("Arial", Font.BOLD, 14));
menu.setMnemonic (KeyEvent.VK_A);
menu.getAccessibleContext ().setAccessibleDescription ("The only menu in this program that has menu items");
menuBar.add (menu);
//Help --> Instructions
menuItem = new JMenuItem ("Instructions", KeyEvent.VK_T);
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext ().setAccessibleDescription ("This doesn't really do anything");
menu.add (menuItem);
//Help --> Strategies
menuItem = new JMenuItem ("Strategies", new ImageIcon ("images/middle.gif"));
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.setMnemonic (KeyEvent.VK_B);
menu.add (menuItem);
//Help --> About
menuItem = new JMenuItem ("About", new ImageIcon ("images/middle.gif"));
//Shortcut key
menuItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.setMnemonic (KeyEvent.VK_B);
menu.add (menuItem);
//Build a menu category. EXIT
menu = new JMenu ("Exit");
menu.setFont (new Font ("Arial", Font.BOLD, 14));
menu.setMnemonic (KeyEvent.VK_A);
menu.getAccessibleContext ().setAccessibleDescription ("The only menu in this program that has menu items");
menuBar.add (menu);
//a submenu
//Exit --> Exit Game?
//menu.addSeparator ();
submenu = new JMenu ("Exit Game?");
submenu.setMnemonic (KeyEvent.VK_S);
//Exit --> Exit Game? --> No
menuItem = new JMenuItem ("No");
submenu.add (menuItem);
menu.add (submenu);
//Exit --> Exit Game? --> Yes
menuItem = new JMenuItem ("Yes");
submenu.add (menuItem);
menu.add (submenu);
/*menuItem = new JMenuItem (new ImageIcon ("images/middle.gif"));
menuItem.setMnemonic (KeyEvent.VK_D);
menu.add (menuItem);*/
Compiler: Ready to Program v1.7.1
Java: 1.4.2
Thank you