快捷方式的JButton提示在其他主题不显示除了matalic
我们有一定的要求,以显示JButton的提示按键板的快捷方式,我们已经增加了行动的地图和提示,但快捷方式所需的所有代码只显示在金属制品的外观和感觉,如果我们改变了应用程序的外观和感觉,从金属到系统或任何其他外观和感觉,快捷方式不再显示在工具提示中。快捷方式的JButton提示在其他主题不显示除了matalic
下面是示例程序,它显示了带快捷键的2个J按钮,快捷方式正确显示具有金属外观,但如果我使用任何其他外观和感觉,如系统或Motif,快捷方式停止显示在J按钮工具小费。为了测试下面的示例中的行为在其他的外观和感觉请评论金属制品外观和实例感受,使系统或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中实现的东西(“only”指的是我尝试过的所有其他LAF)。此行为在MetalToolTipUI
类中定义,该类是负责设置和显示工具提示的类。
幸运的是UIManager
类允许您设置ToolTipUI
。您可以使用金属的任何其他LAF:
UIManager.getDefaults().put("ToolTipUI", "javax.swing.plaf.metal.MetalToolTipUI");
对于我检查,工具提示的视觉风格(背景颜色和字体)改编的LAF,但也有可能是在长相不匹配的情况。在这种情况下,您可能想要子类ToolTipUI
或其子类之一以满足您自己的需要。
此外,检查了LAF
UIManager.getDefaults().getBoolean("ToolTip.hideAccelerator");
回报false
。如果没有,并且所用的工具提示UI遵循此属性,则不会显示加速器。无论如何,您都可以将其设置为true
。
谢谢你的解决方案,配置ToolTipUI金属,该解决方案为我工作后,我也检查了其他LAF,发现其他LAF ToolTipUI设置为BasicToolTipUI不具有代码,以显示加速器的字符串。所以唯一的解决方案是将ToolTipUI类从BasicToolTipUI更改为MetalToolTipUI,更改属性ToolTip.hideAccelerator将无助于显示其他LAF的加速器。再次感谢您提供快速解决方案。 – Gaurav 2014-10-18 15:35:00
@Gaurav *“的为其他LAF ToolTipUI设置为BasicToolTipUI” *,*“改变属性ToolTip.hideAccelerator将不利于为其他LAF显示加速器*”的LAF可以有自己实施的工具提示UI,它可能尊重' hideAccelerator'属性,所以你不能做这个泛化。这也是我为什么在我的回答中解决它的原因。 – user1803551 2014-10-18 17:16:59
@Gaurav *“唯一的解决办法是从BasicToolTipUI的ToolTipUI类更改为MetalToolTipUI” *正如我所说的,你也可以创建自己的工具提示UI并实现所需的功能。 – user1803551 2014-10-18 17:17:38
请张贴的成分的短代码示例这样的提示,动作映射等,所以我们可以看到本作自己。 – user1803551 2014-10-17 13:36:14
示例中添加了问题,请测试具有不同外观和感觉的示例,快捷方式只显示Metalic外观但不显示其他 – Gaurav 2014-10-18 10:07:03
感谢您的示例。请注意,应该在方法的末尾调用'frame.setVisible(true)'。 – user1803551 2014-10-18 11:41:45