【问题标题】:How To Disable Arrow Key Navigation For Java Swing JRadioButton?如何禁用 Java Swing JRadioButton 的箭头键导航?
【发布时间】:2020-06-30 17:16:21
【问题描述】:
我的 Java Swing 应用程序中有一些 JRadioButtons。选择一个按钮后,单击箭头键将浏览所有按钮,但我需要将箭头键用于其他目的,如何禁用默认行为以便按箭头键不会导航单选按钮?
JRadioButton Button1=new JRadioButton("Button1");
JRadioButton Button2=new JRadioButton("Button2");
JRadioButton Button3=new JRadioButton("Button3");
【问题讨论】:
标签:
java
swing
navigation
jradiobutton
arrow-keys
【解决方案1】:
将JRadioButton 添加到ButtonGroup 时,为左、右、上、下箭头键添加“键绑定”:
您可以使用以下代码删除绑定:
InputMap im = button1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("RIGHT"), "none");
im.put(KeyStroke.getKeyStroke("LEFT"), "none");
阅读 Key Bindings 上的 Swing 教程部分了解更多信息。