Java JMenu_2

package com.han;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
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 JMenu_2 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1395124747782445698L;

	static Dimension dimension;

	public JMenu_2() {
		super();
		// TODO Auto-generated constructor stub
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("文件(F)");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);

		JMenuItem newItem = new JMenuItem("新建(N)");
		newItem.setMnemonic('N');
		newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				InputEvent.CTRL_MASK));
		fileMenu.add(newItem);
		newItem.addActionListener(new newItemListener());

		JMenu openMenu = new JMenu("打开(O)");
		JMenuItem openMenuItem = new JMenuItem("未打开过的(N)");
		JMenuItem openMenuItem2 = new JMenuItem("刚打开过的(C)");
		fileMenu.add(openMenu);
		openMenu.add(openMenuItem);
		openMenu.add(openMenuItem2);
		openMenu.setMnemonic('O');
		openMenuItem.setMnemonic('N');
		openMenuItem2.setMnemonic('C');
		openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		openMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		openMenuItem.addActionListener(new openMenuItemListener());
		openMenuItem2.addActionListener(new openMenuItem2Listener());
		fileMenu.addSeparator();
		openMenuItem2.setEnabled(false);

		JMenuItem saveMenuItem = new JMenuItem("保存(S)");
		saveMenuItem.setMnemonic('S');
		saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
				InputEvent.CTRL_MASK));
		fileMenu.add(saveMenuItem);
		saveMenuItem.addActionListener(new saveMenuItemListener());
		fileMenu.addSeparator();

		JMenuItem exitMenuItem = new JMenuItem("退出(E)");
		exitMenuItem.setMnemonic('E');
		exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
				InputEvent.CTRL_MASK));
		fileMenu.add(exitMenuItem);
		exitMenuItem.addActionListener(new exitMenuItemListener());

		JMenu editMenu = new JMenu("编辑(E)");
		editMenu.setMnemonic('E');
		menuBar.add(editMenu);

		JMenuItem cutMenuItem = new JMenuItem("剪切(T)");
		cutMenuItem.setMnemonic('T');
		cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
				InputEvent.CTRL_MASK));
		editMenu.add(cutMenuItem);
		cutMenuItem.addActionListener(new cutMenuItemListener());

		JMenuItem copyMenuItem = new JMenuItem("复制(C)");
		copyMenuItem.setMnemonic('C');
		copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				InputEvent.CTRL_MASK));
		editMenu.add(copyMenuItem);
		copyMenuItem.addActionListener(new copyMenuItemListener());
		URL resource = this.getClass().getResource("/images/Luxun.jpg");
		BufferedImage imageScaled = null;
		BufferedImage in;
		try {
			in = ImageIO.read(resource);
			imageScaled = ImageScale.scale(in, 0.05, 0.05, 1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ImageIcon imageIcon = new ImageIcon(imageScaled);
		copyMenuItem.setIcon(imageIcon);

		JMenuItem pasteMenuItem = new JMenuItem("粘贴(P)");
		pasteMenuItem.setMnemonic('P');
		pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
				InputEvent.CTRL_MASK));
		editMenu.add(pasteMenuItem);
		pasteMenuItem.addActionListener(new pasteMenuItemListener());
		editMenu.addSeparator();

		JMenu fontMenu = new JMenu("字体(F)");
		JCheckBoxMenuItem fontMenuItem = new JCheckBoxMenuItem("加粗(B)");
		JCheckBoxMenuItem fontMenuItem2 = new JCheckBoxMenuItem("斜体(I)");
		/*ButtonGroup buttonGroup2 = new ButtonGroup();
		buttonGroup2.add(fontMenuItem);
		buttonGroup2.add(fontMenuItem2);*/
		editMenu.add(fontMenu);
		fontMenu.add(fontMenuItem);
		fontMenu.add(fontMenuItem2);
		fontMenu.setMnemonic('F');
		fontMenuItem.setMnemonic('B');
		fontMenuItem2.setMnemonic('I');
		fontMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		fontMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		fontMenuItem.addActionListener(new fontMenuItemListener());
		fontMenuItem2.addActionListener(new fontMenuItem2Listener());
		editMenu.addSeparator();

		JMenu attributeMenu = new JMenu("属性(A)");
		JRadioButtonMenuItem attributeMenuItem = new JRadioButtonMenuItem(
				"只读(R)");
		JRadioButtonMenuItem attributeMenuItem2 = new JRadioButtonMenuItem(
				"编辑(E)");
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(attributeMenuItem);
		buttonGroup.add(attributeMenuItem2);
		editMenu.add(attributeMenu);
		attributeMenu.add(attributeMenuItem);
		attributeMenu.add(attributeMenuItem2);
		attributeMenu.setMnemonic('A');
		attributeMenuItem.setMnemonic('R');
		attributeMenuItem2.setMnemonic('E');
		attributeMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		attributeMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
				InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		attributeMenuItem.addActionListener(new attributeMenuItemListener());
		attributeMenuItem2.addActionListener(new attributeMenuItem2Listener());

		JMenu helpMenu = new JMenu("帮助(H)");
		helpMenu.setMnemonic('H');
		menuBar.add(helpMenu);

		dimension = menuBar.getPreferredSize();
		System.out.println(dimension.width + "\t" + dimension.height);
	}

	// action listener procession
	private class newItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class openMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class openMenuItem2Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class saveMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class exitMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class cutMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class copyMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class pasteMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class fontMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class fontMenuItem2Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class attributeMenuItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	private class attributeMenuItem2Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	public static void main(String[] args) {
		JMenu_2 frame = new JMenu_2();
		frame.setTitle("订制个性化菜单");
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Rectangle rectangle = new Rectangle(100, 100, dimension.width + 300,
				dimension.width + 100);
		frame.setBounds(rectangle);
	}
}


你可能感兴趣的:(java)