Swing中美化右键菜单JPopupMenu

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
 
public class JImagedPopupMenu extends JPopupMenu {
    private Font font = new Font("Dialog", Font.BOLD, 13);
    private ImageIcon imageIcon = null;
 
    public JImagedPopupMenu(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
    }
 
    public JImagedPopupMenu(String text) {
        this.imageIcon = createImage(text);
    }
 
    private ImageIcon createImage(String text) {
    	FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
    	int height = fm.stringWidth(text)+20;
        BufferedImage bi = new BufferedImage(30, height, BufferedImage.TYPE_INT_ARGB);
        ImageIcon image = new ImageIcon(bi);
        Graphics2D g2d = bi.createGraphics();
 
        GradientPaint paint = new GradientPaint(0, 0, Color.YELLOW, 30, 0, 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, -height+10, 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();
            // 每一副图像的位置坐标
    		int x = insets.left - imageIcon.getIconWidth();
    		int y = insets.top-2;
 
    		Image image = imageIcon.getImage();
    		// 平铺背景图片
    		while (true) {
    			// 绘制图片
    			g.drawImage(image, x, y, this);
    			// 如果绘制完毕,退出循环
    			if ( y > getSize().height)
    				break;
    			y += imageIcon.getIconHeight();
    		}
        }
    }
 
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setSize(600, 500);
        frame.setTitle("ImageMenu");
        final JImagedPopupMenu menu = new JImagedPopupMenu("www.voland.com.cn");
        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..."));
 
        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();
    }
}
 

你可能感兴趣的:(jpopupmenu)