在windows的“开始”菜单上,会在菜单左侧显示一幅图像。很多基于windows的软件也有类似效果的菜单。用java swing可以制作出类似效果的菜单吗?答案当然是肯定的,而且非常简单。
我们首先从swing的jpopupmenu组件进行扩展,让其接受一个图片,显示在左侧;或者接受一个字符串,动态生成图片后,在显示在左侧。为了避免准备图片的麻烦,我们就以动态生成内存图片为例,编写一个jimagedpopupmenu类。
jimagedpopupmenu在创建时可以接受一个字符串,生成一副内存图片bufferedimage。然后,我们需要覆盖jcomponent的getinsets方法,重新计算inset的left值,将其在原数值基础上加上图片的宽度,然后返回:
?public insets getinsets() {
??????? insets insets = (insets)super.getinsets().clone();
??????? insets.left += imageicon.geticonwidth();
??????? return insets;
??? }
最后,覆盖paintcomponent方法,在原基础上增加图片的绘制:
??? public void paintcomponent(graphics g) {
??????? super.paintcomponent(g);
??????? if (imageicon != null) {
??????????? insets insets = getinsets();
??????????? g.drawimage(imageicon.getimage(),
??????????????????????? insets.left – imageicon.geticonwidth(),
??????????????????????? insets.top,
??????????????????????? null);
??????? }
??? }
完整代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class imagemenu extends jpopupmenu {
??? private font font = new font("dialog", font.bold, 13);
??? private imageicon imageicon = null;
??? public imagemenu(imageicon imageicon) {
??????? this.imageicon = imageicon;
??? }
??? public imagemenu(string text) {
??????? this.imageicon = createimage(text);
??? }
??? private imageicon createimage(string text) {
??????? bufferedimage bi = new bufferedimage(30, 1000, bufferedimage.type_int_argb);
??????? imageicon image = new imageicon(bi);
??????? graphics2d g2d = bi.creategraphics();
??????? gradientpaint paint = new gradientpaint(0, 0, color.yellow, 30, 10, color.red, true);
??????? g2d.setpaint(paint);
??????? g2d.fillrect(0, 0, bi.getwidth(), bi.getheight());
??????? affinetransform at = new affinetransform();
??????? at.rotate( -math.pi / 2);
??????? g2d.settransform(at);
??????? g2d.setcolor(color.white);
??????? g2d.setfont(font);
??????? g2d.drawstring(text, -180, bi.getwidth() / 2);
??????? return image;
??? }
??? public insets getinsets() {
??????? insets insets = (insets)super.getinsets().clone();
??????? insets.left += imageicon.geticonwidth();
??????? return insets;
??? }
??? public void paint(graphics g) {
??????? super.paint(g);
??????? if (imageicon != null) {
??????????? insets insets = getinsets();
??????????? g.drawimage(imageicon.getimage(),
??????????????????????? insets.left – imageicon.geticonwidth(),
??????????????????????? insets.top,
??????????????????????? null);
??????? }
??? }
??? public static void main(string[] args) {
??????? final jframe frame = new jframe();
??????? frame.setsize(600, 500);
??????? frame.settitle("imagemenu");
??????? final imagemenu menu = new imagemenu("windows xp perfessional");
??????? menu.add(new jmenuitem("winzip 8.0"));
??????? menu.addseparator();
??????? menu.add(new jmenuitem("programs"));
??????? menu.add(new jmenuitem("document"));
??????? menu.add(new jmenuitem("settings"));
??????? menu.add(new jmenuitem("search"));
??????? menu.add(new jmenuitem("help and support"));
??????? menu.add(new jmenuitem("run…"));
??????? menu.addseparator();
??????? menu.add(new jmenuitem("shut down…"));
??????? jlabel label = new jlabel("right click me to show image popup menu.");
??????? label.addmouselistener(new java.awt.event.mouseadapter() {
??????????? public void mousereleased(mouseevent e) {
??????????????? if (e.ispopuptrigger()) {
??????????????????? menu.show(frame, e.getpoint().x, e.getpoint().y);
??????????????? }
??????????? }
??????? });
??????? frame.getcontentpane().add(label, borderlayout.center);
??????? frame.show();
??? }
}
同样,采用这种方法,也可以将jmenu扩展出同样的效果。