java - 除了金属之外,jbutton 工具提示中的快捷方式不显示在其他主题中

标签 java swing

我们要求在 Jbutton 工具提示中显示键盘快捷方式,我们已经添加了操作映射和工具提示所需的所有代码,但如果我们将应用程序外观从金属更改为系统或任何,快捷方式仅以金属外观显示其他外观,工具提示中不再显示快捷方式。

下面是示例程序,它显示了 2 个带快捷方式的 J 按钮,快捷方式以金属外观正确显示,但如果我使用任何其他外观,如系统或 Motif,快捷方式将停止在 J 按钮工具提示中显示.要在其他外观中测试以下示例的行为,请在示例中评论 Metalic 外观,并启用现在评论的 System 或 Motif 外观:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        JFrame frame = new JFrame("KeyStroke Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JButton buttonA = new JButton("a Button");
        buttonA.setMnemonic('a');
        frame.setVisible(true);
        buttonA.setToolTipText("a Button");
        final JButton buttonB = new JButton("another Button");
        buttonB.setMnemonic('b');
        frame.add(buttonA);
        frame.add(buttonB);
        buttonB.setToolTipText("another Button");
        Action action = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource().equals(buttonA)) {
                    System.out.println("buttonA");
                }
                if (e.getSource().equals(buttonB)) {
                    System.out.println("buttonB");
                }
            }
        };
        buttonA.addActionListener(action);
        buttonB.addActionListener(action);

        // buttonA ("ALT+A");
        buttonA.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.ALT_MASK),
                "left_button_pressed");
        buttonA.getActionMap().put("left_button_pressed", action);

        // buttonB ("CTRL+Shift+A");
        buttonB.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK
                        | InputEvent.SHIFT_MASK), "right_button_pressed");
        buttonB.getActionMap().put("right_button_pressed", action);
        frame.setLayout(new GridLayout(2, 2));

        frame.setSize(400, 200);

    }
}

最佳答案

在工具提示中显示加速器字符串是默认情况下仅在 Metal 中实现的(“仅”指的是我尝试过的所有其他 LAF)。此行为在 MetalToolTipUI 类中定义,该类负责设置和显示工具提示。

幸运的是,UIManager 类允许您设置一个ToolTipUI。您可以将 Metal 用于任何其他 LAF:

UIManager.getDefaults().put("ToolTipUI", "javax.swing.plaf.metal.MetalToolTipUI");

对于我检查过的 LAF,工具提示的视觉样式(背景颜色和字体)进行了调整,但可能存在外观不匹配的情况。在这种情况下,您可能想要子类化 ToolTipUI 或其子类之一以满足您自己的需要。

另外,检查一下

UIManager.getDefaults().getBoolean("ToolTip.hideAccelerator");

为 LAF 返回 false。如果不是,并且使用的工具提示 UI 遵循此属性,则不会显示加速器。在任何情况下,您都可以将其设置为 true

关于java - 除了金属之外,jbutton 工具提示中的快捷方式不显示在其他主题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26425676/

相关文章:

java - 如何检查 gson 的 int 字段是否为空? [安卓]

java - 如何使用通用枚举实现 Action 监听器?

java - 如何从jtable插入数据库?

java - UIManager,如何获取不同LookAndFeels的值?

java - 自定义 TableCellRenderer 不工作(表行呈现)

java - 如何更改 JTable 中的插入符号颜色

java - 什么时候单例类比只有静态方法的类更受欢迎?

java - Gradle 。如何从 collect{} 打印所有文件

java - 如何从 Web URL 以及带有附加参数的 URL 获取文件名和扩展名

java - Swing JTabbedPane如何设置滚动宽度?