Java by API/javax.swing/JCheckBoxMenuItem

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

JCheckBoxMenuItem: setMnemonic(int mnemonic)

 
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);
  }
}





new JCheckBoxMenuItem(String text)

 
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);
  }
}





new JCheckBoxMenuItem(String text, boolean selected)

 
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);
  }
}