现在的位置: 首页 > 综合 > 正文

Swing 带标题栏的右键菜单(三)增强版

2018年01月30日 ⁄ 综合 ⁄ 共 2582字 ⁄ 字号 评论关闭

     这里主要通过PopupFactory来实现对菜单或右键弹出菜单添加标题,而不用从写JPopupMenu 或JMenu 。

效果如下图:

右键菜单:

 

JMenu:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.*;


public class TitlePopupMenuFactory extends PopupFactory {
    private static final int POPUP_TITLE_WIDTH = 30;
    private Font             font              = new Font("Dialog", Font.BOLD,
                                                       13);

    @Override
    public Popup getPopup(Component owner, Component contents, int x, int y)
            throws IllegalArgumentException {
        if (contents instanceof JPopupMenu || contents instanceof JMenu) {
            Dimension dim = contents.getPreferredSize();
            dim.width += POPUP_TITLE_WIDTH;
            ((JComponent) contents).setBorder(null);
            JComponent comp = new JPanel();
            comp.setBorder(UIManager.getBorder("PopupMenu.border"));
            comp.setPreferredSize(dim);
            comp.setLayout(new BorderLayout());
            comp.add(contents, BorderLayout.CENTER);
            comp.add(new JLabel(createImage("Menu Title", dim.height)),
                    BorderLayout.WEST);
            return super.getPopup(owner, comp, x, y);
        }
        return super.getPopup(owner, contents, x, y);
    }

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

        GradientPaint paint = new GradientPaint(0, 0, Color.yellow,
                POPUP_TITLE_WIDTH, 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.black);
        g2d.setFont(font);
        
        int stringWidth = g2d.getFontMetrics().stringWidth(text);
        g2d.drawString(text, -(height + stringWidth) / 2, bi.getWidth() / 2);

        return image;
    }

    //test
    public static void main(String[] args) {
        PopupFactory.setSharedInstance(new TitlePopupMenuFactory());
        final JFrame frame = new JFrame();
        frame.setSize(600, 500);
        frame.setTitle("ImageMenu");
        JMenu jmenu = new JMenu("Menu");
        jmenu.add(new JMenuItem("MenuItem1"));
        jmenu.add(new JMenuItem("MenuItem2"));
        jmenu.add(new JMenuItem("MenuItem3"));
        jmenu.add(new JMenuItem("MenuItem4"));
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(jmenu);
        frame.setJMenuBar(menuBar);
        final JPopupMenu menu = new JPopupMenu("Windows XP Perfessional");
        menu.add(new JMenuItem("Winzip 8.0"));
        menu.addSeparator();
        menu.add(new JMenuItem("Programs"));
        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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

抱歉!评论已关闭.