Java jbutton工具提示中的快捷方式不显示在除matalic之外的其他主题中
我们需要在Jbutton工具提示中显示键盘快捷键,我们添加了动作图和工具提示所需的所有代码,但快捷键仅显示在Metalic外观中,如果我们将应用程序外观从Metalic更改为system或任何其他外观,shortcut将不再显示在工具提示中 下面是示例程序,它显示了2个带有快捷键的J按钮,快捷键正确地显示为metalic外观,但如果我使用任何其他类似系统或Motif的外观,快捷键将停止显示在J按钮工具提示中。要在其他外观中测试以下示例的行为,请在示例中注释Metalic外观,并启用系统或Motif外观,目前已注释:Java jbutton工具提示中的快捷方式不显示在除matalic之外的其他主题中,java,swing,Java,Swing,我们需要在Jbutton工具提示中显示键盘快捷键,我们添加了动作图和工具提示所需的所有代码,但快捷键仅显示在Metalic外观中,如果我们将应用程序外观从Metalic更改为system或任何其他外观,shortcut将不再显示在工具提示中 下面是示例程序,它显示了2个带有快捷键的J按钮,快捷键正确地显示为metalic外观,但如果我使用任何其他类似系统或Motif的外观,快捷键将停止显示在J按钮工具提示中。要在其他外观中测试以下示例的行为,请在示例中注释Metalic外观,并启用系统或Moti
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);
}
}
在工具提示中显示加速器字符串是默认情况下仅在金属中实现的(“only”指我尝试过的所有其他LAF)。此行为在
MetalToolTipUI
类中定义,该类负责设置和显示工具提示
幸运的是,UIManager
类允许您设置ToolTipUI
。您可以将金属用于任何其他LAF:
UIManager.getDefaults().put("ToolTipUI", "javax.swing.plaf.metal.MetalToolTipUI");
对于我检查过的LAF,工具提示的视觉样式(背景颜色和字体)进行了调整,但可能存在外观不匹配的情况。在这种情况下,您可能需要为ToolTipUI
或其子类之一创建子类,以满足您自己的需要
还有,检查一下
UIManager.getDefaults().getBoolean("ToolTip.hideAccelerator");
返回LAF的
false
。如果没有,并且使用的工具提示UI尊重此属性,则不会显示加速器。在任何情况下,您都可以将其设置为true
。请发布一个带有此类工具提示、动作图等的组件的简短代码示例。这样我们就可以自己看到了。示例中添加了问题,请使用不同的外观测试示例,快捷方式仅使用Metalic外观显示,而不使用其他快捷方式(例如)。请注意,frame.setVisible(true)
应在方法末尾调用。感谢您提供的解决方案,在将ToolTipUI配置为Metal后,该解决方案对我有效,我还检查了其他LAF,发现对于其他LAF,ToolTipUI设置为BasicToolTipUI,它没有显示加速器字符串的代码。因此,唯一的解决方案是将ToolTipUI类从BasicToolTipUI更改为MetalToolTipUI,更改属性ToolTip.hideAccelerator将无助于为其他LAF显示加速器。再次感谢您提供快速解决方案。@Gaurav“对于其他LAF ToolTipUI设置为BasicToolTipUI”,“更改属性ToolTip.HideAcelerator将不会帮助显示其他LAF的加速器”LAF可以有自己的工具提示UI实现,它可能尊重HideAcelerator
属性,所以你不能这样概括。这也是我在回答中提到它的原因。@Gaurav“唯一的解决方案是将ToolTipUI类从BasicToolTipUI更改为MetalToolTipUI”。正如我所说,您还可以创建自己的ToolTipUI并实现所需的功能。@Gaurav它不是Oracle API的一部分,我在这方面帮不了您。您必须找到负责呈现工具提示的类。UIManager
为ToolTipUI
属性返回了什么?@Gaurav这正是我在回答和评论hideacelerator
属性时所说的:)