001: package examples.swingdemos;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.*;
007:
008: /*
009: * This class exists solely to show you what menus look like.
010: * It has no menu-related event handling.
011: */
012: public class MenuLookDemo extends JFrame {
013: JTextArea output;
014: JScrollPane scrollPane;
015:
016: public MenuLookDemo() {
017: JMenuBar menuBar;
018: JMenu menu, submenu;
019: JMenuItem menuItem;
020: JCheckBoxMenuItem cbMenuItem;
021: JRadioButtonMenuItem rbMenuItem;
022:
023: addWindowListener(new WindowAdapter() {
024: public void windowClosing(WindowEvent e) {
025: System.exit(0);
026: }
027: });
028:
029: //Add regular components to the window, using the default BorderLayout.
030: Container contentPane = getContentPane();
031: output = new JTextArea(5, 30);
032: output.setEditable(false);
033: scrollPane = new JScrollPane(output);
034: contentPane.add(scrollPane, BorderLayout.CENTER);
035:
036: //Create the menu bar.
037: menuBar = new JMenuBar();
038: setJMenuBar(menuBar);
039:
040: //Build the first menu.
041: menu = new JMenu("A Menu");
042: menu.setMnemonic(KeyEvent.VK_A);
043: menu.getAccessibleContext().setAccessibleDescription(
044: "The only menu in this program that has menu items");
045: menuBar.add(menu);
046:
047: //a group of JMenuItems
048: menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
049: //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
050: menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
051: ActionEvent.ALT_MASK));
052: menuItem.getAccessibleContext().setAccessibleDescription(
053: "This doesn't really do anything");
054: menu.add(menuItem);
055:
056: menuItem = new JMenuItem("Both text and icon", new ImageIcon(
057: "images/middle.gif"));
058: menuItem.setMnemonic(KeyEvent.VK_B);
059: menu.add(menuItem);
060:
061: menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
062: menuItem.setMnemonic(KeyEvent.VK_D);
063: menu.add(menuItem);
064:
065: //a group of radio button menu items
066: menu.addSeparator();
067: ButtonGroup group = new ButtonGroup();
068:
069: rbMenuItem = new JRadioButtonMenuItem(
070: "A radio button menu item");
071: rbMenuItem.setSelected(true);
072: rbMenuItem.setMnemonic(KeyEvent.VK_R);
073: group.add(rbMenuItem);
074: menu.add(rbMenuItem);
075:
076: rbMenuItem = new JRadioButtonMenuItem("Another one");
077: rbMenuItem.setMnemonic(KeyEvent.VK_O);
078: group.add(rbMenuItem);
079: menu.add(rbMenuItem);
080:
081: //a group of check box menu items
082: menu.addSeparator();
083: cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
084: cbMenuItem.setMnemonic(KeyEvent.VK_C);
085: menu.add(cbMenuItem);
086:
087: cbMenuItem = new JCheckBoxMenuItem("Another one");
088: cbMenuItem.setMnemonic(KeyEvent.VK_H);
089: menu.add(cbMenuItem);
090:
091: //a submenu
092: menu.addSeparator();
093: submenu = new JMenu("A submenu");
094: submenu.setMnemonic(KeyEvent.VK_S);
095:
096: menuItem = new JMenuItem("An item in the submenu");
097: menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,
098: ActionEvent.ALT_MASK));
099: submenu.add(menuItem);
100:
101: menuItem = new JMenuItem("Another item");
102: submenu.add(menuItem);
103: menu.add(submenu);
104:
105: //Build second menu in the menu bar.
106: menu = new JMenu("Another Menu");
107: menu.setMnemonic(KeyEvent.VK_N);
108: menu.getAccessibleContext().setAccessibleDescription(
109: "This menu does nothing");
110: menuBar.add(menu);
111: }
112:
113: public static void main(String[] args) {
114: MenuLookDemo window = new MenuLookDemo();
115:
116: window.setTitle("MenuLookDemo");
117: window.setSize(450, 260);
118: window.setVisible(true);
119: }
120: }
|