5.2.1 把图标与组件相关联
例5-9 菜单项中的图标
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet {
ColorIcon redIcon = new ColorIcon(Color.red, 40, 15),
blueIcon = new ColorIcon(Color.blue, 40, 15),
yellowIcon = new ColorIcon(Color.yellow, 40, 15);
public void init() {
JMenuBar mb = new JMenuBar();
JMenu colors = new JMenu("Colors");
colors.add(new JMenuItem(redIcon));
colors.add(new JMenuItem(blueIcon));
colors.add(new JMenuItem(yellowIcon));
mb.add(colors);
setJMenuBar(mb);
}
}
5.2.2 在组件中共享图标
例5-10 修改后的ColorIcon类清单
import java.awt.*;
import javax.swing.*;
class ColorIcon implements Icon {
private Color fillColor;
private int w, h;
public ColorIcon(Color fillColor, int w, int h) {
this.fillColor = fillColor;
this.w = w;
this.h = h;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
g.setColor(fillColor);
g.fillRect(x+1, y+1, w-2, h-2);
}
public int getIconWidth() {
return w;
}
public int getIconHeight() {
return h;
}
}
例5-11 在许多组件中共享单图标的小应用程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
private ColorIcon colorIcon = new ColorIcon(40, 15);
private JPopupMenu popup = new JPopupMenu();
private JButton button = new JButton("select a color ...",
colorIcon);
public void init() {
addPopupMenuItems();
button.putClientProperty("fill color", Color.red);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Dimension buttonsz = button.getSize();
popup.show(button,buttonsz.width,
buttonsz.height);
}
});
}
private void addPopupMenuItems() {
JMenuItem redItem = new JMenuItem(colorIcon),
blueItem = new JMenuItem(colorIcon),
grayItem = new JMenuItem(colorIcon),
yellowItem = new JMenuItem(colorIcon),
blackItem = new JMenuItem(colorIcon),
whiteItem = new JMenuItem(colorIcon),
orangeItem = new JMenuItem(colorIcon);
MenuItemListener listener = new MenuItemListener();
redItem.putClientProperty("fill color", Color.red);
redItem.addActionListener(listener);
popup.add(redItem);
blueItem.putClientProperty("fill color", Color.blue);
blueItem.addActionListener(listener);
popup.add(blueItem);
grayItem.putClientProperty("fill color", Color.gray);
grayItem.addActionListener(listener);
popup.add(grayItem);
yellowItem.putClientProperty("fill color", Color.yellow);
yellowItem.addActionListener(listener);
popup.add(yellowItem);
blackItem.putClientProperty("fill color", Color.black);
blackItem.addActionListener(listener);
popup.add(blackItem);
whiteItem.putClientProperty("fill color", Color.white);
whiteItem.addActionListener(listener);
popup.add(whiteItem);
orangeItem.putClientProperty("fill color", Color.orange);
orangeItem.addActionListener(listener);
popup.add(orangeItem);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JComponent jc = (JComponent)e.getSource();
button.putClientProperty("fill color",
jc.getClientProperty("fill color"));
button.repaint();
}
}
}
5.2.3 图像图标
例5-12 有一个ImageIcon的小应用程序
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet {
ImageIcon icon = new ImageIcon(this.getClass().getResource("coffeeCup.jpg"));
public void paint(Graphics g) {
icon.paintIcon(this, g, 20, 15);
}
}
5.2.4 动画的图像图标
例5-13 带一个动画的图标的小应用程序
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
JPanel panel = new MyJPanel();
getContentPane().add(panel, "Center");
}
}
class MyJPanel extends JPanel {
ImageIcon animatedIcon = new ImageIcon(getClass().getResource("globe.gif"));
public void paintComponent(Graphics g) {
super.paintComponent(g);
animatedIcon.paintIcon(this, g, 20, 20);
}
}
5.3 动作
例5-14 带一个菜单条的小应用程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("exit");
exitItem.addActionListener(new ExitListener());
fileMenu.add(exitItem);
mb.add(fileMenu);
setJMenuBar(mb);
}
}
class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
例5-15 用一个动作创建一个菜单项
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new ExitAction());
mb.add(fileMenu);
setJMenuBar(mb);
}
}
class ExitAction extends AbstractAction {
public ExitAction() {
super("exit");
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
5.3.1 作为控制中心点的动作
例5-16 与一个工具条按钮和一个菜单项相关联的动作
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
public class Test extends JApplet
implements PropertyChangeListener {
JPanel jp = new JPanel();
JPanel cp = new JPanel(); // cp = checkbox panel
JCheckBox jc = new JCheckBox("action enabled");
JMenuBar mb = new JMenuBar();
JToolBar tb = new JToolBar();
Action saveAction = new SaveAction();
Action exitAction = new ExitAction();
public void init() {
JMenu fileMenu = new JMenu("File");
fileMenu.add(saveAction);
fileMenu.add(exitAction);
tb.add(saveAction);
JCheckBoxMenuItem checkBoxItem =
new JCheckBoxMenuItem("saved");
associateActionAndCheckBoxItem(saveAction, checkBoxItem);
fileMenu.add(checkBoxItem);
mb.add(fileMenu);
saveAction.addPropertyChangeListener(this);
jp.setLayout(new BorderLayout(2,2));
jp.add(tb, "North"); // toolbar
jp.add(cp, "Center"); // checkbox panel
cp.setLayout(new FlowLayout());
cp.add(jc);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
getRootPane().setJMenuBar(mb);
contentPane.add(jp, "Center");
jc.setSelected(saveAction.isEnabled());
jc.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
saveAction.setEnabled(!saveAction.isEnabled());
}
});
}
public void propertyChange(PropertyChangeEvent e) {
boolean b = ((Boolean)e.getNewValue()).booleanValue();
showStatus("save action " + (b ? "enabled" : "disabled"));
}
private void associateActionAndCheckBoxItem(
final Action action,
final JCheckBoxMenuItem item) {
item.setHorizontalTextPosition(JButton.LEFT);
item.setVerticalTextPosition(JButton.CENTER);
item.setEnabled(action.isEnabled());
item.addActionListener(action);
action.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if(name.equals(Action.NAME)) {
item.setText((String)e.getNewValue());
item.revalidate();
}
else if(name.equals("enabled")) {
item.setEnabled(
((Boolean)e.getNewValue()).booleanValue());
item.repaint();
}
else if(name.equals(Action.SMALL_ICON)) {
item.setIcon((Icon)e.getNewValue());
item.revalidate();
}
}
});
}
}
class SaveAction extends AbstractAction {
public SaveAction() {
//super("save", new ImageIcon( this.getClass().getResource("save.gif")));
//("save.gif")));
super("save",new ImageIcon("save.gif"));
setEnabled(false);
}
public void actionPerformed(ActionEvent event) {
String s = new String();
Object o = event.getSource();
if(o instanceof JButton) s += "ToolBar: ";
else if(o instanceof JMenuItem) s += "MenuBar: ";
System.out.println(s + " save");
}
}
class ExitAction extends AbstractAction {
public ExitAction() {
super("exit");
}
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
5.3.2 动作常量
例5-17 在一个定制组件中使用Action.SHORT_DESCRIPTION
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
Container contentPane = getContentPane();
CustomAction action = new CustomAction();
CustomButton button = new CustomButton(action);
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
}
}
class CustomButton extends JButton {
public CustomButton(Action action) {
super((String)action.getValue(Action.NAME),
(Icon)action.getValue(Action.SMALL_ICON));
String shortDescription = (String)action.getValue(
Action.SHORT_DESCRIPTION);
setToolTipText(shortDescription);
addActionListener(action);
}
}
class CustomAction extends AbstractAction {
public CustomAction() {
super("doit", new ImageIcon("skelly.gif"));
putValue(Action.SHORT_DESCRIPTION, "a short description");
}
public void actionPerformed(ActionEvent e) {
System.out.println("Custom action performed");
}
}
5.4 本章回顾