Java 如何为JMenuItem设置工具提示?

Java 如何为JMenuItem设置工具提示?,java,swing,tooltip,imageicon,jtooltip,Java,Swing,Tooltip,Imageicon,Jtooltip,我有一个设置了图标的JMenuItem。我只想为这个图标添加一个工具提示。我找不到任何解决办法。这就是我到目前为止所实现的 JMenuItem mnuItem = new JMenuItem(Aaction a); ImageIconn img = new ImageIcon(String filename); mnuItem.setIcon(img); 我只想为图标添加一个工具提示。基本上@MadProgrammer是正确的。但是一些Swing黑客让我为你找到了一个解决方案。这是: impo

我有一个设置了图标的JMenuItem。我只想为这个图标添加一个工具提示。我找不到任何解决办法。这就是我到目前为止所实现的

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

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

基本上@MadProgrammer是正确的。但是一些Swing黑客让我为你找到了一个解决方案。这是:

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);
    }
}

@我的问题与上面的链接问题不同。我想向菜单项中的图标添加工具提示。我不想让整个JMenuItem都有工具提示。好吧,很抱歉让您感到困惑,但我想您会发现,除非您想构建外观委托,否则您可能会发现不可能实现我们自己的方法来显示图标的工具提示吗?如果是的话,你能告诉我哪里有导游吗begin@MadProgrammer这是可能的,但有一些肮脏的挥杆技巧。我可能会陷入“外观和感觉”的委托中personally@MadProgrammer你是对的,也许L&F的变化在这里会更好。但在本例中,我需要扩展L&F类,该类由主题启动程序使用。另外,
BasicMenuItemUI
中的方法
paintIcon
是私有的,并且使用一些非公共API。