Java by API/javax.swing/JMenu — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

JMenu: add(Component c)

 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
public class Main extends JFrame {
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"));
    setJMenuBar(bar);
    add(new JLabel("A placeholder"));
    pack();
    setSize(300, 300);
    setVisible(true);
  }
  public static void main(String arg[]) {
    new Main();
  }
}





JMenu: add(JMenuItem menuItem)

 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
public class Main extends JFrame {
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"));
    setJMenuBar(bar);
    add(new JLabel("A placeholder"));
    pack();
    setSize(300, 300);
    setVisible(true);
  }
  public static void main(String arg[]) {
    new Main();
  }
}





JMenu: addMenuListener(MenuListener l)

 
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class Main {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);
    fileMenu.addMenuListener(new MenuListener() {
      public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected");
      }
      public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeselected");
      }
      public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled");
      }
    });
    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}





JMenu: addSeparator()

 
import java.awt.Event;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
public class MainClass extends JFrame {
  public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic("F");
    elementMenu.setMnemonic("E");
    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");
    JMenu colorMenu = new JMenu("Color");
    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");
    newItem.setAccelerator(KeyStroke.getKeyStroke("N", Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke("O", Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke("S", Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke("P", Event.CTRL_MASK));
    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu
        .add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);
    lineItem.setAccelerator(KeyStroke.getKeyStroke("L", Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke("E", Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke("I", Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke("V", Event.CTRL_MASK));
    elementMenu.addSeparator();
    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke("R", Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke("Y", Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke("G", Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke("B", Event.CTRL_MASK));
    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
  }
  private JMenuBar menuBar = new JMenuBar();
  // File menu items
  private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem,
      printItem;
  // Element menu items
  private JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem,
      textItem;
  private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
  public static void main(String[] args) {
    JFrame aWindow = new MainClass();
    aWindow.setSize(200,200);
    aWindow.setVisible(true);
  }
}





JMenu: add(String text)

 
import java.awt.Event;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
public class MainClass extends JFrame {
  public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic("F");
    elementMenu.setMnemonic("E");
    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");
    JMenu colorMenu = new JMenu("Color");
    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");
    newItem.setAccelerator(KeyStroke.getKeyStroke("N", Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke("O", Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke("S", Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke("P", Event.CTRL_MASK));
    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu
        .add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);
    lineItem.setAccelerator(KeyStroke.getKeyStroke("L", Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke("E", Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke("I", Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke("V", Event.CTRL_MASK));
    elementMenu.addSeparator();
    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke("R", Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke("Y", Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke("G", Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke("B", Event.CTRL_MASK));
    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
  }
  private JMenuBar menuBar = new JMenuBar();
  // File menu items
  private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem,
      printItem;
  // Element menu items
  private JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem,
      textItem;
  private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
  public static void main(String[] args) {
    JFrame aWindow = new MainClass();
    aWindow.setSize(200,200);
    aWindow.setVisible(true);
  }
}





JMenu: getMenuComponentCount()

 
import java.awt.ruponent;
import java.awt.ruponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
public class MainClass extends JFrame {
  public MainClass() {
    super("MainClass");
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(new JButton("OK"));
    contentPane.add(new JButton("Cancel"));
    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
  }
  public static void main(String[] argv) {
    JFrame frame = new MainClass();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.setVisible(true);
  }
  private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);
    if (c instanceof JMenu) {
      JMenu menu = (JMenu) c;
      int ncomponents = menu.getMenuComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(menu.getMenuComponent(i), o);
      }
    } else if (c instanceof Container) {
      Container container = (Container) c;
      int ncomponents = container.getComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(container.getComponent(i), o);
      }
    }
  }
}





JMenu: setMnemonic(KeyEvent.VK_O)

 
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JRadioButtonMenuItem Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Options");
    menu.setMnemonic(KeyEvent.VK_O);
    ButtonGroup group = new ButtonGroup();
    JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("A");
    group.add(menuItem);
    menu.add(menuItem);
    menuItem = new JRadioButtonMenuItem("B");
    group.add(menuItem);
    menu.add(menuItem);
    menuItem = new JRadioButtonMenuItem("C");
    group.add(menuItem);
    menu.add(menuItem);
    bar.add(menu);
    f.setJMenuBar(bar);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





new JMenu(String s)

 
import java.awt.event.KeyEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);
    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);
    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);
    
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}