package Menu;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
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.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;
public class MenuTest extends JFrame{
private JMenuBar menuBar;
private JPanel panel;
public MenuTest() {
//添加一个菜单栏
menuBar = new JMenuBar();
panel = new JPanel();
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = fileMenu.add("Exit");
exitMenuItem.addActionListener(new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
JMenuItem cutMenuItem = new JMenuItem("Cut",new ImageIcon("Icon/cut.png"));
cutMenuItem.setHorizontalTextPosition(SwingConstants.LEFT);
JMenuItem copyMenuItem = new JMenuItem("Copy");
JMenuItem pasteMenuItem = new JMenuItem("Paste");
JMenu optionMenu = new JMenu("Options");
JCheckBoxMenuItem readOnlyItem = new JCheckBoxMenuItem("Read-Only");
optionMenu.add(readOnlyItem);
//菜单里添加复选框和单选框
optionMenu.addSeparator();//添加分格线
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
insertItem.setSelected(true);
group.add(insertItem);
optionMenu.add(insertItem);
JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");
group.add(overtypeItem);
optionMenu.add(overtypeItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
editMenu.add(optionMenu);
menuBar.add(editMenu);
//在菜单栏添加一个Help菜单选项
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
this.add(panel,BorderLayout.CENTER);
this.setLocation(300, 400);
this.setSize(300,350);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MenuTest();
}
}
菜单二
package Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import javax.swing.AbstractAction;
import javax.swing.Action;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
public class MenuFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 350;
private Action saveAction; //直接存储操作的监听器
private Action saveAsAction; //选择存储操作监听器
private JMenuBar menuBar; //菜单条
private JCheckBoxMenuItem readOnly; //只读的复选框
private ButtonGroup buttonGroup; //单选框组
private JPopupMenu popup; //弹出菜单
private JPanel panel;
public MenuFrame() {
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
fileMenu.add(newItem);
JMenuItem openItem = new JMenuItem("Open");
openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); //快捷键
openItem.addActionListener(new MyMonitor("Open")); //添加监听器
fileMenu.add(openItem);
fileMenu.addSeparator(); //分隔线
saveAction = new MyMonitor("Save");
JMenuItem saveItem =fileMenu.add(saveAction);
saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
saveAsAction = new MyMonitor("Save as");
JMenuItem saveAsItem = fileMenu.add(saveAsAction);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("EXit"); //程序停止选项
exitItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
Action cutAction = new MyMonitor("Cut");
cutAction.putValue(Action.SMALL_ICON,new ImageIcon("Icon/cut.gif"));
Action copyAction = new MyMonitor("Copy");
copyAction.putValue(Action.SMALL_ICON, new ImageIcon("Icon/copy.gif"));
Action pasteAction = new MyMonitor("Paste");
pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("Icon/paste.gif"));
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.addSeparator();
JMenu optionMenu = new JMenu("Options");
readOnly = new JCheckBoxMenuItem("readOnly");
readOnly.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
boolean saveOk = !readOnly.isSelected(); //若当前格式为只读,使菜单的存储操作无法选择
saveAction.setEnabled(saveOk);
saveAsAction.setEnabled(saveOk);
}
});
optionMenu.add(readOnly);
optionMenu.addSeparator();
buttonGroup = new ButtonGroup(); //添加单选按钮
JRadioButtonMenuItem insertButton = new JRadioButtonMenuItem("Insert");
JRadioButtonMenuItem overTypeButton = new JRadioButtonMenuItem("OverType");
buttonGroup.add(insertButton);
buttonGroup.add(overTypeButton);
optionMenu.add(insertButton);
optionMenu.add(overTypeButton);
insertButton.setSelected(true);
editMenu.add(optionMenu);
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H); //快捷键
JMenuItem indexItem = new JMenuItem("Index",'I');
helpMenu.add(indexItem);
JMenuItem aboutItem = new JMenuItem("About",'A');
helpMenu.add(aboutItem);
popup = new JPopupMenu();//右键弹出式菜单
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);
panel = new JPanel();
panel.setComponentPopupMenu(popup);
panel.addMouseListener(new MouseAdapter(){});
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
this.add(panel);
this.setBounds(300, 300, DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MenuFrame();
}
class MyMonitor extends AbstractAction {
public MyMonitor(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand()+" selected");
}
}
}