JFrame f = new MnemoticTest();
JButton b=new JButton("bat");
b.setMnemonic(KeyEvent.VK_B);
f.add(b);
f.setSize(400, 700);
f.setVisible(true);
您好,我得到了上面的代码。加载时,“b”已经带有下划线。 我希望仅当我单击 alt 时“b”才带有下划线。
加载时它不应显示下划线,当我单击 alt 时,b 应显示为下划线
我们该怎么做?
最佳答案
据我所知,此行为仅受 Windows L&F 支持。因此您需要将 L&F 更改为 Windows L&F。
如果您使用的是 Windows 计算机,您可以执行以下操作:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
如果没有,您可以尝试以下方法(我不确定Windows L&F是否可以在非Windows计算机上运行):
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
重要:L&F 设置应在应用程序启动时执行,此时尚未初始化任何视觉元素。
关于java - JButton 助记符仅在 ALT 键上有下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39406516/