Java 100-006:菜单栏的创建-包括嵌套菜单,禁用菜单项,复选框和单选按钮菜单项,弹出菜单以及快捷键和加速器

package java01;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *   我的java每天100行代码006
 *   菜单栏的创建:包括嵌套菜单,禁用菜单项,复选框和单选按钮菜单项,弹出菜单以及快捷键和加速器;
 * @author Administrator
 *
 */
public class java006 {
		public static void main(String[] args) {
			
			EventQueue.invokeLater(new Runnable() {
				public void run() {
				//显示窗口
					JFrame frame = new MenuFrame();
					frame.setTitle("MenuFrame");
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setVisible(true);
				
				}
			});
	}
}

class MenuFrame extends JFrame{
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 300;
	
	private Action saveAction;
	private Action saveAsAction;
	
	private JCheckBoxMenuItem readonlyItem;
	private JPopupMenu popup;
	
	public MenuFrame(){
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new TestAction("new"));
		
		JMenuItem openItem = fileMenu.add(new TestAction("open"));
		openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));//设置快捷键
		fileMenu.addSeparator();//添加菜单项中的分界线
		saveAction = new TestAction("save");
		JMenuItem saveItem =fileMenu.add(saveAction);//只有菜单才能直接添加事件,菜单项不行
		saveItem.setAccelerator(KeyStroke.getKeyStroke("ctel S"));
		saveAsAction = new TestAction("saveAs");
		JMenuItem saveAsItem =fileMenu.add(saveAsAction);
		fileMenu.addSeparator();
		
		fileMenu.add(new AbstractAction("exit") {
			public void actionPerformed(ActionEvent e) {
				System.out.println("0");
			}
		});
		
		readonlyItem = new JCheckBoxMenuItem("Read-only");
		readonlyItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				boolean saveOk = !readonlyItem.isSelected();
				saveAction.setEnabled(saveOk);
				saveAsAction.setEnabled(saveOk);
			}
		});
		
		//单选按钮菜单项
		ButtonGroup group = new ButtonGroup();
		
		JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
		insertItem.setSelected(true);
		JRadioButtonMenuItem overTypeItem =
				new JRadioButtonMenuItem("Overtype");
		
		group.add(insertItem);
		group.add(overTypeItem);
		
		Action cutAction = new TestAction("Cut");
		cutAction.putValue(Action.NAME, "Cut");
		Action copyAction = new TestAction("Copy");
		cutAction.putValue(Action.NAME, "Copy");
		Action pasteAction = new TestAction("Paste");
		cutAction.putValue(Action.NAME, "Paste");
		
		JMenu editMenu = new JMenu("Edit");
		editMenu.add(cutAction);
		editMenu.add(copyAction);
		editMenu.add(pasteAction);
		
		JMenu optionMenu = new JMenu("Options");
		optionMenu.add(readonlyItem);
		optionMenu.addSeparator();
		optionMenu.add(insertItem);
		optionMenu.add(overTypeItem);
		
		editMenu.addSeparator();
		editMenu.add(optionMenu);
		
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('H');//设置快捷键Alt+H即可调出Help菜单,这里要用单引号
		
		JMenuItem indexItem = new JMenuItem("Index");
		indexItem.setMnemonic('I');
		helpMenu.add(indexItem);
		
		Action aboutAction = new TestAction("About");
		aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
		helpMenu.add(aboutAction);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		//设计菜单项可弹出
		popup = new JPopupMenu();
		popup.add(cutAction);
		popup.add(copyAction);
		popup.add(pasteAction);
		
		JPanel panel = new JPanel();
		panel.setComponentPopupMenu(popup);
		add(panel);
		
		panel.addMouseListener(new MouseAdapter() {});
		
	}
	
	class TestAction extends AbstractAction{	
		public TestAction(String name) {
			super(name);
		}
		public void actionPerformed(ActionEvent e) {
			System.out.println(getValue(Action.NAME)+"selected");		
		}	
	}
}

你可能感兴趣的:(java,java100)