第10章 菜单和工具条
菜单和工具条是现代用户界面的重要组成部分。Swing提供了对这两者的完全支持。Swing提供菜单组件(即菜单条中的菜单和弹出式菜单中的菜单)和菜单项组件(包括复选框和单选按钮菜单项)。
Swing还提供一个工具条组件,根据工具条的方向,工具条组件包含一行按钮或一列按钮。工具条通常提供对普通特性的简单访问。通常,除键盘捷径键提供对相同功能的访问外,菜单栏和工具条不提供对相同功能集的访问。工具条还可以是悬浮的,这样,可以把工具条在窗口中任意拖动或拖到一个单独的窗口中。
Swing菜单和菜单项是按钮,因为JMenuItem扩展AbstractButton,JMenu扩展JMenuItem,如图10-1所示。因此,Swing菜单和菜单项继承了如下功能,即包含文本和(或)图标、显示光标进入时的图标和助记符等。
图10-1示出了JMenuItem和JMenu的父组件,它们最后都归于java.awt.Container,以强调菜单和菜单项都是容器。可以把任何类型的组件(从包含一个动画GIF的标签到JTree的一个实例)添加到一个菜单或菜单项中。
菜单和菜单项还实现MenuElement接口,以便参与菜单事件的处理。MenuElement接口在10.7节“菜单元素”中介绍。
工具条和弹出式菜单都扩展JComponent类,即任意组件都可以添加到一个工具条或弹出式菜单中。
本章将介绍下面的组件:
·JMenuItem
·JCheckBoxMenuItem
·JRadioButtonMenuItem
·JMenu
·JPopupMenu
·JMenuBar
·JToolBar
10.1 菜单、菜单栏和工具条
例10-1 JRootPane中的菜单栏和工具条
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
Icon
dukeStanding = new ImageIcon("duke_standing.gif"),
dukeWaving = new ImageIcon("duke_waving.gif"),
dukeStandingSmall =
new ImageIcon("duke_standing_small.gif"),
dukeWavingSmall = new ImageIcon("duke_waving_small.gif");
public void init() {
Container contentPane = getContentPane();
Action[] actions = {
new NewAction(),
new OpenAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new ExitAction()
};
JToolBar toolbar = new JToolBar();
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JRadioButton
menubarDuke = new JRadioButton(dukeStandingSmall),
menuDuke = new JRadioButton(dukeStandingSmall),
toolbarDuke = new JRadioButton(dukeStanding);
menuDuke.setRolloverIcon(dukeWavingSmall);
menubarDuke.setRolloverIcon(dukeWavingSmall);
toolbarDuke.setRolloverIcon(dukeWaving);
menubar.add(menubarDuke);
toolbar.add(toolbarDuke);
fileMenu.add(menuDuke);
for(int i=0; i < actions.length; ++i) {
fileMenu.add(actions[i]);
if(i != actions.length-1)
toolbar.add(actions[i]);
if(i == 2 || i == actions.length-2){
toolbar.addSeparator();
fileMenu.addSeparator();
}
}
menubar.add(fileMenu);
contentPane.add(toolbar, BorderLayout.NORTH);
getRootPane().setJMenuBar(menubar);
System.out.println(contentPane.getClass().getName());
LayoutManager lm = contentPane.getLayout();
System.out.println(lm.getClass());
}
class NewAction extends AbstractAction {
public NewAction() {
super("New ...", new ImageIcon("new.gif"));
}
public void actionPerformed(ActionEvent event) {
showStatus("new");
}
}
class OpenAction extends AbstractAction {
public OpenAction() {
super("Open ...", new ImageIcon("open.gif"));
}
public void actionPerformed(ActionEvent event) {
showStatus("open");
}
}
class CutAction extends AbstractAction {
public CutAction() {
super("Cut", new ImageIcon("cut.gif"));
}
public void actionPerformed(ActionEvent event) {
showStatus("cut");
}
}
class CopyAction extends AbstractAction {
public CopyAction() {
super("Copy", new ImageIcon("copy.gif"));
}
public void actionPerformed(ActionEvent event) {
showStatus("copy");
}
}
class PasteAction extends AbstractAction {
public PasteAction() {
super("Paste", new ImageIcon("paste.gif"));
}
public void actionPerformed(ActionEvent event) {
showStatus("paste");
}
}
class ExitAction extends AbstractAction {
public ExitAction() {
super("Exit");
putValue(Action.SMALL_ICON, dukeWavingSmall);
}
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
}
10.2 菜单和弹出式菜单
例10-2 一个简单的菜单举例
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu pullRightMenu = new JMenu("pull right");
fileMenu.add("New ...");
fileMenu.add("Open ...");
fileMenu.add("Save");
fileMenu.add("Save As ..");
fileMenu.addSeparator();
fileMenu.add(pullRightMenu);
fileMenu.add("Exit");
pullRightMenu.add(new JCheckBoxMenuItem("Bush"));
pullRightMenu.add(new JCheckBoxMenuItem("Tonic"));
pullRightMenu.add(new JCheckBoxMenuItem("Radio Head"));
pullRightMenu.add(new JCheckBoxMenuItem("Marcy Playground"));
pullRightMenu.add(new JCheckBoxMenuItem("Silver Chair"));
mb.add(fileMenu);
setJMenuBar(mb);
}
}
10.3 JMenuItem
例10-3 安装一些菜单项
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
Icon newIcon = new ImageIcon("new.gif",
"Create a new document");
Icon openIcon = new ImageIcon("open.gif",
"Open an existing document");
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem(newIcon);
JMenuItem openItem = new JMenuItem("Open ...", openIcon);
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem saveAsItem = new JMenuItem("Save As ...");
JMenuItem exitItem = new JMenuItem("Exit", 'x');
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
MenuItemListener listener = new MenuItemListener(this);
newItem.addActionListener(listener);
openItem.addActionListener(listener);
saveItem.addActionListener(listener);
saveAsItem.addActionListener(listener);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mb.add(fileMenu);
setJMenuBar(mb);
}
}
class MenuItemListener implements ActionListener {
private JApplet applet;
public MenuItemListener(JApplet applet) {
this.applet = applet;
}
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
ImageIcon icon = (ImageIcon)item.getIcon();
if(icon != null)
System.out.println(icon.getDescription());
else
System.out.println(item.getText());
}
}
10.3.1 菜单项快捷键助记符键
例10-4 带助记符键和快捷键的菜单项
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
public Test() {
Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add("New ...");
fileMenu.add("Open ...");
fileMenu.add("Save");
fileMenu.add("Save As ..");
fileMenu.addSeparator();
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_X,
Event.ALT_MASK);
exitItem.setAccelerator(ks);
fileMenu.setMnemonic('F');
exitItem.setMnemonic(KeyEvent.VK_X);
mb.add(fileMenu);
setJMenuBar(mb);
}
public static void main(String args[]) {
GJApp.launch(new Test(),
"Accelerators and Mnemonics",300,300,450,300);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void updateStatus(String s) {
status.setText(s);
}
}
10.3.2 JMenuItem属性
10.3.3 JMenuItem事件
例10-5 监听菜单项动作和菜单项变化事件
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New ..."),
openItem = new JMenuItem("Open ..."),
saveItem = new JMenuItem("Save"),
saveAsItem = new JMenuItem("Save As ..."),
exitItem = new JMenuItem("Exit");
Listener listener = new Listener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
newItem.setActionCommand("Create a New Document");
openItem.setActionCommand("Open an Existing Document");
saveItem.setActionCommand("Save Document");
saveAsItem.setActionCommand("Save Document As ...");
exitItem.setActionCommand("Exit the applet");
newItem.addChangeListener(listener);
openItem.addChangeListener(listener);
saveItem.addChangeListener(listener);
saveAsItem.addChangeListener(listener);
exitItem.addChangeListener(listener);
mb.add(fileMenu);
setJMenuBar(mb);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
class Listener implements ChangeListener {
private JApplet applet;
public Listener(JApplet applet) {
this.applet = applet;
}
public void stateChanged(ChangeEvent e) {
JMenuItem b = (JMenuItem)e.getSource();
if(b.isArmed())
applet.showStatus(b.getActionCommand());
}
}
10.3.4 JMenuItem类总结
10.3.5 AWT兼容
10.4 JCheckBoxMenuItem
例10-6 实例化复选框菜单项
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu checkBoxMenu = new JMenu("Endangered Species");
ImageIcon crabIcon = new ImageIcon("crab.gif");
ImageIcon eagleIcon = new ImageIcon("eagle.gif");
JMenuItem
crabItem = new JCheckBoxMenuItem(crabIcon),
eagleItem = new JCheckBoxMenuItem("eagle",
eagleIcon, true),
ladybugItem = new JCheckBoxMenuItem("ladybug");
checkBoxMenu.add(crabItem);
checkBoxMenu.add(eagleItem);
checkBoxMenu.add(ladybugItem);
ladybugItem.setMnemonic('l');
mb.add(checkBoxMenu);
setJMenuBar(mb);
}
}
10.4.1 JCheckBoxMenuItem属性
10.4.2 JCheckBoxMenuItem事件
例10-7 访问JCheckBoxMenuItem状态
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
private ImageIcon crabIcon = new ImageIcon("crab.gif");
private ImageIcon eagleIcon = new ImageIcon("eagle.gif");
private JCheckBoxMenuItem
crabItem = new JCheckBoxMenuItem(crabIcon),
eagleItem = new JCheckBoxMenuItem("eagle", eagleIcon),
ladybugItem = new JCheckBoxMenuItem("ladybug");
public void init() {
Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu checkBoxMenu = new JMenu("Endangered Species");
Listener listener = new Listener();
checkBoxMenu.add(crabItem);
checkBoxMenu.add(eagleItem);
checkBoxMenu.add(ladybugItem);
crabItem.addActionListener(listener);
eagleItem.addActionListener(listener);
ladybugItem.addActionListener(listener);
mb.add(checkBoxMenu);
setJMenuBar(mb);
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
showStatus("crab: " + crabItem.getState() + ", " +
"eagle: " + eagleItem.getState() + ", " +
"ladybug: " + ladybugItem.getState());
}
}
}
10.4.3 JCheckBoxMenuItem类总结
10.4.4 AWT兼容
10.5 JRadioButtonMenuItem
例10-8 实例化单选按钮菜单项
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu radioMenu = new JMenu("Favorite Animal");
ImageIcon crabIcon = new ImageIcon("crab.gif");
ImageIcon eagleIcon = new ImageIcon("eagle.gif");
final JMenuItem
crabItem = new JRadioButtonMenuItem(crabIcon),
eagleItem = new JRadioButtonMenuItem("eagle",
eagleIcon),
ladybugItem = new JRadioButtonMenuItem("ladybug");
radioMenu.add(crabItem);
radioMenu.add(eagleItem);
radioMenu.add(ladybugItem);
mb.add(radioMenu);
setJMenuBar(mb);
}
}
10.5.1 JRadioButtonMenuItem属性
10.5.2 JRadioButtonMenuItem事件
例10-9 为JRadioButton的实例监视事件
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
private ImageIcon crabIcon = new ImageIcon("crab.gif",
"Crab");
private ImageIcon eagleIcon = new ImageIcon("eagle.gif",
"Eagle");
private JMenuItem
crabItem = new JRadioButtonMenuItem(crabIcon),
eagleItem = new JRadioButtonMenuItem("eagle",
eagleIcon),
ladybugItem = new JRadioButtonMenuItem("ladybug");
public void init() {
Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu radioMenu = new JMenu("Favorite Animal");
AnItemListener itemListener = new AnItemListener();
AnActionListener actionListener = new AnActionListener();
AChangeListener changeListener = new AChangeListener();
radioMenu.add(crabItem);
radioMenu.add(eagleItem);
radioMenu.add(ladybugItem);
ButtonGroup group = new ButtonGroup();
group.add(crabItem);
group.add(eagleItem);
group.add(ladybugItem);
mb.add(radioMenu);
setJMenuBar(mb);
crabItem.addItemListener(itemListener);
eagleItem.addItemListener(itemListener);
ladybugItem.addItemListener(itemListener);
crabItem.addActionListener(actionListener);
eagleItem.addActionListener(actionListener);
ladybugItem.addActionListener(actionListener);
crabItem.addChangeListener(changeListener);
eagleItem.addChangeListener(changeListener);
ladybugItem.addChangeListener(changeListener);
}
private String getItemDescription(JMenuItem item) {
String s;
ImageIcon icon = (ImageIcon)item.getIcon();
if(icon != null) return icon.getDescription();
else return item.getText();
}
// Inner class event handlers follow ...
class AnActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
String s = getItemDescription(item);
showStatus(s + " activated");
System.out.println("action event fired");
}
};
class AChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
Object[] selectedObjs =
ladybugItem.getSelectedObjects();
if(selectedObjs == null)
System.out.println("selected objs is null");
else
System.out.println(selectedObjs[0] + "selected");
JMenuItem item = (JMenuItem)e.getSource();
if(item.isArmed()) {
String s = getItemDescription(item);
showStatus(s + " armed");
}
System.out.println("change event fired");
}
};
class AnItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
String s = getItemDescription(item);
if(e.getStateChange() == ItemEvent.SELECTED)
s += " selected";
else
s += " deselected";
showStatus(s);
System.out.println("item event fired");
}
};
}
10.5.3 JRadioButtonMenuItem类总结
10.5.4 AWT兼容
10.6 JMenu
例10-10 JMenu的一个简单例子
10.6.1 动态修改菜单
例10-11 一个自修改菜单
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class Test extends JApplet {
private SelfModifyingMenu selfModifyingMenu;
public void init() {
JMenuBar menuBar = new JMenuBar();
createMenus(menuBar);
setJMenuBar(menuBar);
}
public void createMenus(JMenuBar mbar) {
mbar.add(createFileMenu());
mbar.add(selfModifyingMenu = new SelfModifyingMenu());
}
private JMenu createFileMenu() {
JMenu fileMenu = new JMenu("File");
JMenuItem quitItem = new JMenuItem("Quit");
fileMenu.add(quitItem);
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
return fileMenu;
}
}
class SelfModifyingMenu extends JMenu {
private Vector newItems = new Vector();
private Listener menuItemListener = new Listener();
private JMenuItem toggleItem, enablerItem,
addItem, removeItem;
public SelfModifyingMenu() {
super("Self Modifying Menu");
add(enablerItem = new JMenuItem("Disable Item Below"));
add(toggleItem = new JMenuItem("Enable/Disable Me"));
addSeparator();
add(addItem = new JMenuItem("Add a JMenuItem ..."));
add(removeItem = new JMenuItem(
"Remove last JMenuItem ..."));
addItem.setFont(new Font("Helvetica", Font.BOLD, 18));
addSeparator();
enablerItem.addActionListener(menuItemListener);
toggleItem.addActionListener(menuItemListener);
addItem.addActionListener(menuItemListener);
removeItem.addActionListener(menuItemListener);
}
public void addItem() {
JMenuItem newItem =
new JMenuItem("Extra Item #" + newItems.size());
add(newItem);
newItems.addElement(newItem);
}
public void removeLastItem() {
if(newItems.size() == 0)
System.out.println("Nothing to remove!");
else {
JMenuItem removeMe =
(JMenuItem)newItems.lastElement();
remove(removeMe);
newItems.removeElement(removeMe);
}
}
public void toggleItem() {
if(toggleItem.isEnabled()) toggleItem.setEnabled(false);
else toggleItem.setEnabled(true);
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JMenuItem item = (JMenuItem)event.getSource();
if(item == enablerItem) {
toggleItem();
if(toggleItem.isEnabled())
enablerItem.setText("Disable Item Below");
else
enablerItem.setText("Enable Item Below");
}
else if(item == addItem) addItem();
else if(item == removeItem) removeLastItem();
}
}
}
10.6.2 右拉式菜单
例10-12 右拉式菜单
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
JMenuBar menuBar = new JMenuBar();
createMenus(menuBar);
setJMenuBar(menuBar);
}
public void createMenus(JMenuBar mbar) {
mbar.add(createFileMenu());
mbar.add(createCascadingMenu());
}
private JMenu createFileMenu() {
JMenu fileMenu = new JMenu("File");
JMenuItem quitItem = new JMenuItem("Quit");
fileMenu.add(quitItem);
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
return fileMenu;
}
private JMenu createCascadingMenu() {
JMenu cascading = new JMenu("Cascading", true);
JMenu submenu = new JMenu("pull right menu 1", true);
JMenu subsubmenu = new JMenu("pull right menu 2", true);
submenu.add("submenu item 1");
submenu.add("submenu item 2");
submenu.add("submenu item 3");
submenu.add("submenu item 4");
submenu.add("submenu item 5");
subsubmenu.add("subsubmenu item 1");
subsubmenu.add("subsubmenu item 2");
subsubmenu.add("subsubmenu item 3");
subsubmenu.add("subsubmenu item 4");
subsubmenu.add("subsubmenu item 5");
submenu.add(subsubmenu);
cascading.add(submenu);
return cascading;
}
}
10.6.3 JMenu属性
10.6.4 JMenu事件
例10-13 监听菜单事件
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
public void init() {
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add("New ...");
fileMenu.add("Open ...");
fileMenu.add("Save");
fileMenu.add("Save As ..");
fileMenu.addSeparator();
fileMenu.add("Exit");
mb.add(fileMenu);
setJMenuBar(mb);
fileMenu.addMenuListener(new MenuListener() {
public void menuCanceled(MenuEvent e) {
System.out.println("menu canceled");
}
public void menuSelected(MenuEvent e) {
System.out.println("menu selected");
}
public void menuDeselected(MenuEvent e) {
System.out.println("menu deselected");
}
});
}
}