Posted on 2010-08-19 15:24
TWaver 阅读(1183)
评论(2) 编辑 收藏
在Windows的“开始”菜单上,会在菜单左侧显示一幅图像。很多基于Windows的软件也有类似效果的菜单。用Java Swing可以制作出类似效果的菜单吗?答案当然是肯定的,而且非常简单。
我们首先从Swing的JPopupMenu组件进行扩展,让其接受一个图片,显示在左侧;或者接受一个字符串,动态生成图片后,在显示在左侧。为了避免准备图片的麻烦,我们就以动态生成内存图片为例,编写一个JImagedPopupMenu类。
JImagedPopupMenu在创建时可以接受一个字符串,生成一副内存图片BufferedImage。然后,我们需要覆盖JComponent的getInsets方法,重新计算Inset的left值,将其在原数值基础上加上图片的宽度,然后返回:
1public Insets getInsets() {
2 Insets insets = (Insets)super.getInsets().clone();
3 insets.left += imageIcon.getIconWidth();
4 return insets;
5}
最后,覆盖paintComponent方法,在原基础上增加图片的绘制:
1public void paintComponent(Graphics g) {
2 super.paintComponent(g);
3 if (imageIcon != null) {
4 Insets insets = getInsets();
5 g.drawImage(imageIcon.getImage(),
6 insets.left - imageIcon.getIconWidth(),
7 insets.top,
8 null);
9 }
10}
完整代码如下:
1import java.awt.*;
2import java.awt.event.*;
3import java.awt.geom.*;
4import java.awt.image.*;
5import javax.swing.*;
6
7public class JImagedPopupMenu extends JPopupMenu {
8
9 private Font font = new Font("微软雅黑", Font.BOLD, 16);
10 private ImageIcon imageIcon = null;
11
12 public JImagedPopupMenu(ImageIcon imageIcon) {
13 this.imageIcon = imageIcon;
14 }
15
16 public JImagedPopupMenu(String text) {
17 this.imageIcon = createImage(text);
18 }
19
20 private ImageIcon createImage(String text) {
21 BufferedImage bi = new BufferedImage(30, 1000, BufferedImage.TYPE_INT_ARGB);
22 ImageIcon image = new ImageIcon(bi);
23 Graphics2D g2d = bi.createGraphics();
24
25 GradientPaint paint = new GradientPaint(0, 0, Color.green.darker(), 30, 10, Color.yellow.brighter(), true);
26 g2d.setPaint(paint);
27
28 g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight());
29
30 AffineTransform at = new AffineTransform();
31 at.rotate(-Math.PI / 2);
32
33 g2d.setTransform(at);
34 g2d.setColor(Color.darkGray);
35 g2d.setFont(font);
36 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
37 g2d.drawString(text, -150, bi.getWidth() / 2 + 5);
38
39 return image;
40 }
41
42 @Override
43 public Insets getInsets() {
44 Insets insets = (Insets) super.getInsets().clone();
45 insets.left += imageIcon.getIconWidth();
46 return insets;
47 }
48
49 @Override
50 public void paint(Graphics g) {
51 super.paint(g);
52 if (imageIcon != null) {
53 Insets insets = getInsets();
54 g.drawImage(imageIcon.getImage(),
55 insets.left - imageIcon.getIconWidth(),
56 insets.top,
57 null);
58 }
59 }
60
61 public static void main(String[] args) {
62 final JFrame frame = new JFrame();
63 frame.setSize(600, 400);
64 frame.setTitle("TWaver中文社区之Swing探秘");
65 final JImagedPopupMenu menu = new JImagedPopupMenu("TWaver中文社区");
66 menu.add(new JMenuItem("Winzip 8.0"));
67 menu.addSeparator();
68 menu.add(new JMenuItem("Programs"));
69 menu.add(new JMenuItem("Document"));
70 menu.add(new JMenuItem("Settings"));
71 menu.add(new JMenuItem("Search"));
72 menu.add(new JMenuItem("Help and Support"));
73 menu.add(new JMenuItem("Run"));
74 menu.addSeparator();
75 menu.add(new JMenuItem("Shut Down"));
76 JLabel label = new JLabel("Right click me to show image popup menu.");
77 label.addMouseListener(new java.awt.event.MouseAdapter() {
78
79 public void mouseReleased(MouseEvent e) {
80 if (e.isPopupTrigger()) {
81 menu.show(frame, e.getPoint().x, e.getPoint().y);
82 }
83 }
84 });
85 frame.getContentPane().add(label, BorderLayout.CENTER);
86 frame.show();
87 }
88}
运行效果如下: