Swing 给菜单按钮添加快捷键

package Assis;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.SwingUtilities;

public class Sketcher {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				creatGUI();
			}
		});

	}

	static void creatGUI() {
		window = new SketchFrame("Sketcher");
		Toolkit theKit = window.getToolkit();
		Dimension wndSize = theKit.getScreenSize();

		window.setBounds(wndSize.width / 4, wndSize.height / 4, // 位置
				wndSize.width / 2, wndSize.height / 2); // 大小

		window.setVisible(true);

	}

	private static SketchFrame window;
}

 

package Assis;

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;
import static java.awt.event.InputEvent.*;

public class SketchFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	// 构造函数
	public SketchFrame(String title) {
		setTitle(title); // 设置窗口标题
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setJMenuBar(menuBar); // 添加菜单栏到窗口

		JMenu fileMenu = new JMenu("File"); // 创建File菜单
		JMenu elementMenu = new JMenu("Elements"); // 创建Elements菜单
		fileMenu.setMnemonic('F'); // 创建快捷键
		elementMenu.setMnemonic('E'); // 创建快捷键

		// 构造file下拉菜单
		newItem = fileMenu.add("new"); // 添加New项目
		openItem = fileMenu.add("Open"); // 添加Open项目
		closeItem = fileMenu.add("Close"); // 添加Close项目
		fileMenu.addSeparator(); // 添加Separator
		saveItem = fileMenu.add("save"); // 添加Save项目
		saveAsItem = fileMenu.add("Save as"); // 添加Save As项目
		fileMenu.addSeparator(); // 添加Separator
		printItem = fileMenu.add("print"); // 添加Print项目

		// 添加File菜单加速键
		newItem.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
		openItem.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
		saveItem.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
		printItem.setAccelerator(KeyStroke.getKeyStroke('P', CTRL_DOWN_MASK));

		// 构造Element下拉菜单
		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);

		// 添加element类型加速键
		lineItem.setAccelerator(KeyStroke.getKeyStroke('L', CTRL_DOWN_MASK));
		rectangleItem.setAccelerator(KeyStroke
				.getKeyStroke('E', CTRL_DOWN_MASK));
		circleItem.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
		curveItem.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));

		elementMenu.addSeparator();

		// 创建color子菜单
		JMenu colorMenu = new JMenu("Color"); // Color子菜单
		elementMenu.add(colorMenu); // 添加子菜单
		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));

		// 添加element color加速键
		redItem.setAccelerator(KeyStroke.getKeyStroke('R', CTRL_DOWN_MASK));
		yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', CTRL_DOWN_MASK));
		greenItem.setAccelerator(KeyStroke.getKeyStroke('G', CTRL_DOWN_MASK));
		blueItem.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));

		menuBar.add(fileMenu); // 添加file菜单
		menuBar.add(elementMenu); // 添加element菜单
	}

	private JMenuBar menuBar = new JMenuBar(); // 窗口菜单栏

	// File菜单项目
	JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;

	// Element菜单项目
	JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem,
			textItem;
	private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
}

 

你可能感兴趣的:(swing,F#)