//MenuExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MenuExample
{
public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MenuFrame extends JFrame
{
public MenuFrame()
{
setTitle("MenuExample");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
JPanel textPanel=new JPanel();
//创建显示菜单操作的文本区
myTextField = new JTextField("", 20);
textPanel.add(myTextField);
contentPane.add(textPanel);
//创建菜单栏,并将菜单栏加入到框架中
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//创建菜单及菜单项,并添加快捷键和加速器
//“File”菜单
JMenu fileMenu = new JMenu("File");
//设置菜单的快捷键
fileMenu.setMnemonic('F');
//新建“New”菜单项,并添加共用的事件监听器
newItem = fileMenu.add(new ShowAction("New"));
//设置菜单项的加速器
newItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, InputEvent.CTRL_MASK));
//新建“Open”菜单项和监听器
openItem = fileMenu.add(new ShowAction("Open"));
openItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_O, InputEvent.CTRL_MASK));
//增加分隔线
fileMenu.addSeparator();
//新建“Save”菜单项和监听器
saveItem = fileMenu.add(new ShowAction("Save"));
saveItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, InputEvent.CTRL_MASK));
//初始设置“Save”菜单项为禁用状态
saveItem.setEnabled(false);
//新建“Close”菜单项和监听器
closeItem = fileMenu.add(new ShowAction("Close"));
closeItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_L, InputEvent.CTRL_MASK));
closeItem.setEnabled(false);
fileMenu.addSeparator();
//新建“Exit”菜单项和监听器
JMenuItem exitItem = new JMenuItem("Exit", 'T');
exitItem.addActionListener(new AbstractAction("Exit")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
exitItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_T, InputEvent.CTRL_MASK));
fileMenu.add(exitItem);
//“Edit”菜单
JMenu editMenu = new JMenu("Edit");
//设置菜单的快捷键
editMenu.setMnemonic('E');
//新建“Cut”菜单项和监听器
cutItem = new JMenuItem("Cut", 'C');
editMenu.add(cutItem);
//添加事件监听器
cutItem.addActionListener(new ShowAction("Cut"));
cutItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_U, InputEvent.CTRL_MASK));
cutItem.setEnabled(false);
//新建“Copy”菜单项和监听器
copyItem = new JMenuItem("Copy", 'Y');
copyItem.addActionListener(new ShowAction("Copy"));
copyItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Y, InputEvent.CTRL_MASK));
copyItem.setEnabled(false);
//新建“Paste”菜单项和监听器
pasteItem = new JMenuItem("Paste", 'P');
pasteItem.addActionListener(new ShowAction("Paste"));
pasteItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_P, InputEvent.CTRL_MASK));
pasteItem.setEnabled(false);
editMenu.add(copyItem);
editMenu.add(pasteItem);
//“Search”菜单
JMenu searchMenu = new JMenu("Search");
searchMenu.setMnemonic('S');
//创建单选按钮菜单项“Forward”和“Backward”
ButtonGroup group = new ButtonGroup();
forwardItem = new JRadioButtonMenuItem("Forward");
forwardItem.addActionListener(new ShowAction("Forward"));
forwardItem.setSelected(true);
forwardItem.setEnabled(false);
backwardItem = new JRadioButtonMenuItem("Backward");
backwardItem.addActionListener(new ShowAction("Backward"));
backwardItem.setEnabled(false);
group.add(forwardItem);
group.add(backwardItem);
searchMenu.add(forwardItem);
searchMenu.add(backwardItem);
//"View"菜单
JMenu viewMenu = new JMenu("View");
viewMenu.setMnemonic('V');
//创建子菜单
JMenu optionMenu = new JMenu("Option");
optionMenu.setMnemonic('O');
//创建复选框菜单项“ToolBar”和“Status”
JCheckBoxMenuItem toolItem = new JCheckBoxMenuItem("ToolBar");
JCheckBoxMenuItem statusItem = new JCheckBoxMenuItem("Status");
toolItem.addActionListener(new ShowAction("ToolBar"));
statusItem.addActionListener(new ShowAction("Status"));
//将菜单项添加到子菜单中
optionMenu.add(toolItem);
optionMenu.add(statusItem);
//将子菜单添加到“View”菜单中
viewMenu.add(optionMenu);
//将菜单加入到菜单栏中
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(searchMenu);
menuBar.add(viewMenu);
//弹出式菜单
popup = new JPopupMenu();
//新建弹出式菜单中的“Cut”菜单项和监听器
cutItemPop = new JMenuItem("Cut", 'C');
cutItemPop.addActionListener(new ShowAction("CutPop"));
cutItemPop.setEnabled(false);
//新建弹出式菜单中的“Copy”菜单项和监听器
copyItemPop = new JMenuItem("Copy", 'Y');
copyItemPop.addActionListener(new ShowAction("CopyPop"));
copyItemPop.setEnabled(false);
//新建弹出式菜单中的“Paste”菜单项和监听器
pasteItemPop = new JMenuItem("Paste", 'P');
pasteItemPop.addActionListener(new ShowAction("PastePop"));
pasteItemPop.setEnabled(false);
popup.add(cutItemPop);
popup.add(copyItemPop);
popup.add(pasteItemPop);
//实现弹出式菜单的弹出触发器
getContentPane().addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
if (event.isPopupTrigger())
//指定弹出式菜单的父组件以及显示位置
popup.show(event.getComponent(),
event.getX(), event.getY());
}
});
}
//菜单项的共用事件监听器
private class ShowAction extends AbstractAction
{
public ShowAction(String name) { super(name); }
public void actionPerformed(ActionEvent event)
{
//实现菜单项的启用和禁用控制
if (event.getSource() == newItem ||
event.getSource() == openItem)
{
saveItem.setEnabled(true);
closeItem.setEnabled(true);
cutItem.setEnabled(true);
copyItem.setEnabled(true);
pasteItem.setEnabled(true);
forwardItem.setEnabled(true);
backwardItem.setEnabled(true);
cutItemPop.setEnabled(true);
copyItemPop.setEnabled(true);
pasteItemPop.setEnabled(true);
}
if (event.getSource() == closeItem)
{
saveItem.setEnabled(false);
closeItem.setEnabled(false);
cutItem.setEnabled(false);
copyItem.setEnabled(false);
pasteItem.setEnabled(false);
forwardItem.setEnabled(false);
backwardItem.setEnabled(false);
cutItemPop.setEnabled(false);
copyItemPop.setEnabled(false);
pasteItemPop.setEnabled(false);
}
//在文本域中显示菜单选择结果
myTextField.setText(getValue(Action.NAME)+ " selected.");
}
}
public static final int WIDTH = 250;
public static final int HEIGHT = 200;
private JMenuItem newItem;
private JMenuItem openItem;
private JMenuItem closeItem;
private JMenuItem saveItem;
private JMenuItem cutItem;
private JMenuItem copyItem;
private JMenuItem pasteItem;
private JMenuItem cutItemPop;
private JMenuItem copyItemPop;
private JMenuItem pasteItemPop;
private JRadioButtonMenuItem forwardItem;
private JRadioButtonMenuItem backwardItem;
private JPopupMenu popup;
private JTextField myTextField;
}
评论7
最新资源