Java Swing Menu Example

Source: Internet
Author: User
Tags final gettext static class

The

menu has made important improvements in swing and is more flexible-for example, we can use them anywhere in almost any program, including in panels and patches. The syntax is the same as they are in the old AWT, and this makes it appear in the old AWT in new swing also: we have to write code hard for our menus, and there are some no longer as resource support menus (some of the other events will make them easier to translate into other programming languages). In addition, the menu code is quite lengthy and sometimes confusing. The following approach is to place all the information about each menu into the two-dimensional array of objects (which can place everything we want to handle into the array), a step in the way of solving the problem. This two-dimensional array is created by the menu, so it first represents the menu name, and in the remaining columns, it represents the items and their attributes. We'll notice that the array columns don't have to be consistent--as long as our code knows all the events that will happen, each column can be completely different.
 

: Menus.java//A menu-building system;
Also demonstrates//icons in labels and menu items.
Package c13.swing;
Import java.awt.*;
Import java.awt.event.*;

Import javax.swing.*;
  public class Menus extends JPanel {static final Boolean BT = new Boolean (true), BF = new Boolean (false);
  Dummy class to create type Identifiers:static class Mtype {mtype (int i) {}}; Static final mtype mi = new Mtype (1),//Normal menu item cb = new Mtype (2),//Checkbox menu item RB = new MT Ype (3);
  Radio button menu item JTextField t = new JTextField (10);
  JLabel L = new JLabel ("Icon Selected", faces.faces[0), jlabel.center); ActionListener A1 = new ActionListener () {public void actionperformed (ActionEvent e) {T.settext ((JMe
    Nuitem) E.getsource ()). GetText ());
  }
  }; ActionListener A2 = new ActionListener () {public void actionperformed (ActionEvent e) {jmenuitem mi = (jmenuite
      m) E.getsource ();
L.settext (Mi.gettext ());      L.seticon (Mi.geticon ());
  }
  }; Store menu data as "resources": public object[][] Filemenu = {//Menu name and accelerator: {"File", New Ch Aracter (' F ')},//Name type Accel Listener enabled {"New", MI, New Character (' N '), A1, BT}, {"Open", MI, N EW Character (' O '), A1, BT}, {"Save", MI, New Character (' S '), A1, BF}, {"Save as", MI, New Character (' A '), A1,
  BF}, {null},//Separator {"Exit", MI, New Character (' X '), A1, BT},}; Public object[][] Editmenu = {//Menu name: {"Edit", New Character (' E ')},//name Type Accel Listener ENABL ed {"Cut", MI, new Character (' t '), A1, bt}, {"Copy", MI, New Character (' C '), A1, BT}, {"Paste", MI, new C
  Haracter (' P '), A1, BT}, {null},//Separator {"Select all", Mi,new Character (' l '), A1,BT},}; Public object[][] Helpmenu = {//Menu name: {"Help", New Character (' H ')},//name Type Accel Listener ENABL ed {"Index", MI, new ChAracter (' I '), A1, BT}, {"Using help", Mi,new Character (' U '), A1,BT}, {null},//Separator {"About", MI, NE
  W Character (' t '), A1, BT},}; Public object[][] Optionmenu = {//Menu name: {"Options", New Character (' O ')},//name Type Accel Listener
  Enabled {"Option 1", CB, New Character (' 1 '), A1,BT}, {"Option 2", CB, New Character (' 2 '), A1,BT},};
    Public object[][] Facemenu = {//Menu name: {"Faces", New Character (' A ')},//optinal last element is icon 
      {"Face 0", RB, New Character (' 0 '), A2, BT, Faces.faces[0]}, {"Face 1", RB, New Character (' 1 '), A2, BT, Faces.faces[1]}, {"Face 2", RB, New Character (' 2 '), A2, BT, Faces.faces[2]}, {"Face 3", RB, new C
  Haracter (' 3 '), A2, BT, Faces.faces[3]}, {"Face 4", RB, New Character (' 4 '), A2, BT, Faces.faces[4]},
  };
  Public object[] MenuBar = {filemenu, editmenu, Facemenu, Optionmenu, Helpmenu,}; static public JMEnubar Createmenubar (object[] menubardata) {JMenuBar MenuBar = new JMenuBar ();
    for (int i = 0; i < menubardata.length i++) Menubar.add (CreateMenu ((object[][)) menubardata[i]);
  return menuBar;
  Static Buttongroup bgroup;
    static public JMenu CreateMenu (object[][] menudata) {jmenu menu = new JMenu ();
    Menu.settext ((String) menudata[0][0]);
    Menu.setmnemonic (((Character) menudata[0][1]). Charvalue ());
    Create redundantly, in case there are//any radio Buttons:bgroup = new Buttongroup ();
      for (int i = 1; i < menudata.length i++) {if (menudata[i][0) = null) menu.add (new Jseparator ());
    Else Menu.add (Createmenuitem (menudata[i));
  } return menu;
    static public JMenuItem Createmenuitem (object[] data) {JMenuItem m = null;
    Mtype type = (mtype) data[1];
    if (type = mi) m = new JMenuItem ();
    else if (type = = cb) m = new JCheckBoxMenuItem (); else if(type = RB)
      {m = new JRadioButtonMenuItem ();
    Bgroup.add (m);
    } m.settext ((String) data[0]);
    M.setmnemonic (((Character) data[2]). Charvalue ());
    M.addactionlistener ((ActionListener) data[3]);
    M.setenabled (((Boolean) data[4]). Booleanvalue ());
    if (data.length = = 6) M.seticon ((Icon) data[5]);
  return m;
    } Menus () {setlayout (New BorderLayout ());
    Add (Createmenubar (MenuBar), Borderlayout.north);
    JPanel p = new JPanel ();
    P.setlayout (New BorderLayout ());
    P.add (t, Borderlayout.north);
    P.add (L, Borderlayout.center);
  Add (P, borderlayout.center);
  public static void Main (String args[]) {show.inframe (New Menus (), 300, 200); }
} ///:~

The purpose of this program is to allow a program designer to simply create a table to describe each menu, instead of entering a line of code to create a menu. Each menu produces a menu, and the first column in the table contains the menu name and keyboard shortcuts. The remaining columns contain the data for each menu item: The position of the string in the menu item, the type of it, the shortcut key, the action receiver that was activated when the menu item was selected, and whether the menus were activated. If the column is empty at the beginning, it will be treated as a separator.
To prevent waste and lengthy multiple boolean-created objects and type flags, the following are created as static final at the beginning of the class: BT and BF Describe the different objects of the Booleans and dumb classes mtype the Standard menu item (MI), CheckBox menu item (CB), and radio buttons menu items (RB). Keep in mind that a set of object can have a single object handle and is no longer the original value.
This program example also shows how Jlables and Jmenuitems (and their derivative) handle icons. An icon is placed in the jlable through its builder and is changed when the corresponding menu item is selected.
The menu bar array control handles all the file menus listed in the File menu list that we want to display on the menu bar. We use the array to Createmenubar (), classify the array into a separate menu data array, and then create the menu from each individual array. This method uses each row of the menu data in turn and creates jmenu with that data, and then calls the Createmenuitem () method for each remaining row in the menu data. Finally, the Createmenuitem () method analyzes each row of the menu data and determines the menu type and its properties, and then creates the menu items appropriately. Finally, as we see in the menu builder, we create menus from tables that represent Createmenubar (MenuBar), and everything is handled recursively.
This program does not create a tandem menu, but we have enough knowledge to add a multilevel menu at any time if we need to.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.