Java 虚拟键盘与Nimbus冲突
我想知道为什么会和你有冲突。查看我在下面制作的示例:Java 虚拟键盘与Nimbus冲突,java,action,nimbus,virtual-keyboard,Java,Action,Nimbus,Virtual Keyboard,我想知道为什么会和你有冲突。查看我在下面制作的示例: public class buttontest implements ActionListener { JMenuItem close =new JMenuItem("Close"); public static void main (String[] args){ try { for (LookAndFeelInfo info : UIManager.getInstalledLookAn
public class buttontest implements ActionListener {
JMenuItem close =new JMenuItem("Close");
public static void main (String[] args){
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
}
public buttontest(){
JFrame test = new JFrame();
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("File");
close.setMnemonic(KeyEvent.VK_C);
file.setMnemonic(KeyEvent.VK_F);
test.setExtendedState(test.getExtendedState() | test.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize())
test.setDefaultCloseOperation(1);
bar.add(file);
file.add(close);
test.setJMenuBar(bar);
test.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
}
}
按照它的编写方式,您可以尝试使用虚拟密钥。您将看到Alt F可以打开文件菜单,但Alt C不能关闭应用程序。换句话说,如果您对Nimbus代码进行注释,两个虚拟键都可以工作
我做了一项关于这个“错误”的研究(或者我做了一些我不知道的错误),但直到现在我什么也没发现。有人通过过这个吗?对于
JMenuItem
,您必须使用setAccelerator()
方法:
close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK ));
来自Javadoc:JMenuItem#设置加速器(击键)
设置所需的键组合
在不导航菜单的情况下调用菜单项的操作侦听器
等级制度用户界面负责安装正确的
行动。请注意,当键入键盘加速器时,它将工作
当前是否显示菜单
附加说明:
LookAndFeelInfo
替换为UIManager.LookAndFeelInfo
,因为它是UIManager
中的一个内部类setDefaultCloseOperation(1)
的参数更改为3作为3=JFrame。在关闭时退出,但1=JFrame。在关闭时隐藏它,我个人讨厌它,因为关闭按钮是为关闭帧而创建的,而不是像Skype那样隐藏它
close.addActionListener(此按钮)代码>
给你一个词伙计:天才!