We understand all upper menu structure of a graphical application that allows us to display a list of operations that in turn can contain other lists of operations. The basic structure consists of a menu JMenuBar (the top bar) from which dangle (JMenu) menus that we add elements (JMenuItem) that can be JCheckBoxMenuItem, and even JRadioButtonMenuItem JMenu. Consider now the methods of each of these classes:
JMenuBar:
public JMenuBar (): creates a new JMenuBar;
public JMenu add (JMenu m): add a JMenu to the MenuBar;
GetMenu public JMenu (int index): Returns the index associated with the corresponding menu.
JMenuItem (JCheckBoxMenuItem superclass, and JradioButtonItem JMenu):
public JMenuItem () creates an empty menu item;
public JMenuItem (String text) creates a string associated;
public JMenuItem (Icon icon) is created with an associated image that will be displayed in the menu;
public void setEnabled (boolean b) indicate whether we want or not can be selected.
JMenu:
public JMenu (String s) Creates a new JMenu that displays the specified string;
public JMenu add (JMenuItem menuItem): adds the JMenu JMenuItem;
public JMenu add (String s) Creates a new menu item with the specified text and puts it at the end of this JMenu;
public void addSeparator () adds a separating horizontal line;
public JMenuItem getItem (int pos): Returns the JMenuItem containing the indicated position
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleMenuControlInJApplet extends JApplet implements ActionListener
{
public void init()
{
JMenuBar MnuBar = new JMenuBar();
JMenu MnuOne = new JMenu("File");
JMenuItem MnuItmOne = new JMenuItem("New..."),
MnuItmTwo = new JMenuItem("Open..."),
MnuItmThree = new JMenuItem("Exit");
JButton BtnPrss = new JButton("Prees Here!");
BtnPrss.setActionCommand("You Pressed the Button");
BtnPrss.addActionListener(this);
MnuOne.add(MnuItmOne);
MnuOne.add(MnuItmTwo);
MnuOne.addSeparator();
MnuOne.add(BtnPrss);
MnuOne.addSeparator();
MnuOne.add(MnuItmThree);
JMenu MnuTwo = new JMenu("Edit");
JMenuItem MnuItmFour = new JMenuItem("Cut"),
MnuItmFive = new JMenuItem("Copy"),
MnuItmSix = new JMenuItem("Paste");
MnuTwo.add(MnuItmFour);
MnuTwo.add(MnuItmFive);
MnuTwo.add(MnuItmSix);
MnuBar.add(MnuOne);
MnuBar.add(MnuTwo);
setJMenuBar(MnuBar);
}
public void actionPerformed(ActionEvent e1)
{
JButton Btn = (JButton)e1.getSource();
showStatus(Btn.getActionCommand());
}
}
/*<APPLET CODE=JavaExampleMenuControlInJApplet.class WIDTH = 350 HEIGHT = 280 ></APPLET>*/