swing创建带图像的右键弹出菜单

在Windows的“开始”菜单上,会在菜单左侧显示一幅图像。很多基于Windows的软件也有类似效果的菜单。用Java Swing可以制作出类似效果的菜单吗?答案当然是肯定的,而且非常简单。

JImagedPopupMenu在创建时可以接受一个字符串,生成一副内存图片BufferedImage。然后,我们需要覆盖JComponent的getInsets方法,重新计算Inset的left值,将其在原数值基础上加上图片的宽度,然后返回

JPopupMenu类扩展了javax.swing.JComponent,是Swing提供的一种菜单。 与其他形式菜单不同的是,JPopupMenu并不固定在菜单栏中,而是能够自由浮动。JPopupMenu具有很好的环境相关(context- sensitive)特性,每一个JPopupMenu都与相应的控件相关联,该控件被称做调用者(invoker)。

Insets 对象是容器边界的表示形式。它指定容器必须在其各个边缘留出的空间。这个空间可以是边界、空白空间或标题。



package com.menu;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;

public class JImagedPopupMenu extends JPopupMenu {

    private Font font = new Font("΢utf-8", Font.BOLD, 16);
    private ImageIcon imageIcon = null;

    public JImagedPopupMenu(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
    }
    public JImagedPopupMenu(String text) {
        this.imageIcon = createImage(text);
    }

    private ImageIcon createImage(String text) {
        BufferedImage bi = new BufferedImage(30, 100, BufferedImage.TYPE_INT_ARGB);
        ImageIcon image = new ImageIcon(bi);
        Graphics2D g2d = bi.createGraphics();

        GradientPaint paint = new GradientPaint(0, 0, Color.green.darker(), 
        		30, 10, Color.yellow.brighter(), 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.darkGray);
        g2d.setFont(font);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawString(text, -150, bi.getWidth() / 2 + 5);

        return image;
    }

    @Override
    public Insets getInsets() {
        Insets insets = (Insets) super.getInsets().clone();
        insets.left += imageIcon.getIconWidth();
        return insets;
    }

    @Override
    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, 400);
        frame.setTitle("swing");
        final JImagedPopupMenu menu = new JImagedPopupMenu("swing");
        menu.add(new JMenuItem("Winzip 8.0"));
        menu.addSeparator();
        menu.add(new JMenuItem("Programs"));
        menu.addSeparator();
        menu.add(new JMenuItem("Document"));
        menu.addSeparator();
        menu.add(new JMenuItem("Run"));
        menu.addSeparator();
        menu.add(new JMenuItem("Shut Down"));
        JLabel label = new JLabel(" click ");
        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();
    }
 }


你可能感兴趣的:(swing创建带图像的右键弹出菜单)