2

我有一个为它设置了图标的 JMenuItem。我只想为这个图标添加一个工具提示。我无法找到任何解决方案。这是我到目前为止所实施的。

JMenuItem mnuItem = new JMenuItem(Aaction a);
ImageIconn img = new ImageIcon(String filename);
mnuItem.setIcon(img);

我想为图标添加一个工具提示。

4

1 回答 1

2

基本上@MadProgrammer 是对的。但是一些 Swing hack 让我为你找到了解决方案。就这个:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

/**
 * <code>TestMenuItem</code>.
 */
public class TestMenuItem {

    private static final String ICON_RECT = "iconRect";

    private static class IconDelegate implements Icon {

        private Icon delegate;

        public IconDelegate(Icon delegate) {
            this.delegate = delegate;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            delegate.paintIcon(c, g, x, y);
            if (c instanceof JComponent) {
                // save last point where the icon was shown in the menu item
                ((JComponent) c).putClientProperty(ICON_RECT, new Point(x, y));
            }
        }

        @Override
        public int getIconWidth() {
            return delegate.getIconWidth();
        }

        @Override
        public int getIconHeight() {
            return delegate.getIconHeight();
        }
    }

    private static class IconTooltipItem extends JMenuItem {

        /**
         * 
         */
        public IconTooltipItem() {
            super();
        }

        /**
         * @param a
         */
        public IconTooltipItem(Action a) {
            super(a);
        }

        /**
         * @param text
         * @param icon
         */
        public IconTooltipItem(String text, Icon icon) {
            super(text, icon);
        }

        @Override
        public String getToolTipText(MouseEvent event) {
            // get the icon point
            Point p = (Point) getClientProperty(ICON_RECT);
            boolean shouldShow = true;
            if (p != null && getIcon() != null) {
                Rectangle rect = new Rectangle(p, new Dimension(getIcon().getIconWidth(), getIcon().getIconHeight()));
                // show only when mouse is in the icon rectangle.
                shouldShow = rect.contains(event.getPoint());
            }
            return shouldShow ? super.getToolTipText(event) : null;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestMenuItem::startUp);
    }

    private static void startUp() {
        Icon icn = UIManager.getIcon("OptionPane.informationIcon");
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem item = new IconTooltipItem("Information", new IconDelegate(icn));
        item.setToolTipText("Information item");
        bar.add(menu);
        menu.add(item);
        JFrame frm = new JFrame("IconItem");
        frm.setSize(500, 500);
        frm.setJMenuBar(bar);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}
于 2020-02-10T08:07:06.670 回答