JMenuItem的工具提示 [英] Tooltip for a JMenuItem

查看:52
本文介绍了JMenuItem的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向这样的工具栏添加菜单:

I add a menu to a toolbar like this :

    JMenuBar menu = new JMenuBar();
    JMenu actions = new JMenu("Aktionen");
    Icon menuIcon = ImageUtilities.loadImageIcon("pathToIcon", true);
    actions.setIcon(menuIcon);

    // Add
    JMenuItem addItem = new JMenuItem("Add");       
    Icon addIcon = ImageUtilities.loadImageIcon("pathToIcon", true);
    addItem.setIcon(addIcon);
    addItem.setToolTipText("Add new Item");
    addItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            AddItemAction someAction = new AddItemAction();
            someAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null) {
                // Foo
            });
        }
    });        

    menu.add(actions);
    actions.add(addItem);
    toolbar.addSeparator();
    toolbar.add(menu);  

基本上,它可以正常工作.但是,它永远不会显示工具提示(添加新项").有任何提示吗?

Basically, it works fine. But, it never displays the tooltip ("Add new Item"). Any hints ?

以防万一遇到相同问题的人偶然发现:这是L& F,正如我从一开始就应该怀疑的那样.它具有用于显示JMenuItems的工具提示的属性;并且默认为false.

Just in case someone with the same problem stumbles upon this: it was the L&F, as I should have suspected from the beginning. It has a property for displaying tooltips of JMenuItems ; and it defaults to false.

推荐答案

下面的 sscce 可以正常工作.如果仍然有问题,请编辑问题,以包含显示您所描述问题的示例.

The sscce below works correctly. If you still have problems, please edit your question to include an example that exhibits the problem you describe.

附录:我在 JToolBar 中添加了菜单,它仍然可以工作,无论是停靠的还是自由浮动的.

Addendum: I added the menu to a JToolBar, and it still works, either docked or free-floating.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

/** @see http://stackoverflow.com/a/14630345/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menu = new JMenuBar();
        JMenu actions = new JMenu("Aktionen");
        JMenuItem addItem = new JMenuItem("Add");
        addItem.setToolTipText("Add new Item");
        menu.add(actions);
        actions.add(addItem);
        JToolBar toolbar = new JToolBar("Tools");
        toolbar.add(menu);
        f.add(toolbar);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

这篇关于JMenuItem的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆