Detecting a JRadioButton state change(检测 JRadioButton 状态更改)
问题描述
当使用鼠标单击时,如何检测 JRadioButton 何时从未选择"更改为已选择"?我尝试在按钮上使用 ActionListener,但每次单击单选按钮时都会触发它,而不仅仅是在将状态更改为已选择"时.
How can I detect when a JRadioButton is changed from 'unselected' to 'selected' when clicked with the mouse? I've tried using an ActionListener on the button, but that gets fired every time the radiobutton is clicked, not just when it's changing state to 'selected'.
我曾考虑维护一个布尔变量来记住按钮的状态,并在 ActionListener 中对其进行测试以查看是否更改其状态,但我想知道是否有更好或更清洁的解决方案.
I've thought of maintaining a boolean variable that remembers the state of the button, and test it inside the ActionListener to see whether to change its state but I'm wondering if there's a much better or cleaner solution.
推荐答案
看看JRadioButton.addItemListener()
Look at JRadioButton.addItemListener()
您不太可能想要使用 changeListener,因为它每次点击都会触发多次.itemListener 每次点击只触发一次.看这里
It is unlikely you want to use a changeListener as it fires multiple times per click. An itemListener fires only once per click. See here
只是为了扩展这一点,每次用户点击它时,jradioButton 上的 actionListener 都会触发,即使它已经被选中.如果那是你想要的,很好,但我觉得这很烦人.我只想收到它被选中或取消选中的通知.
Just to expand on this, an actionListener on a jradioButton will fire every time a user clicks on it, even if it is already selected. if that's what you want, fine, but I find it annoying. I only want to be notified it it is selected or deselected.
ChangeListener 会针对各种情况触发,这意味着您的侦听器每次点击都会收到 5 个或更多事件.不好.
A ChangeListener will fire for all sorts of things, meaning your listener will receive 5 or more events per click. Not good.
itemlistener 仅在选中或取消选中状态发生变化时触发.这意味着用户可以多次单击它,如果它不改变它就不会触发.在您的处理程序方法中,您必须有一个 if
块检查 SELECTED
或 DESELECTED
状态并在那里执行任何操作:
An itemlistener will fire only if the selected or deselected state changes. This means that a user can click on it multiple times and it will not fire if it doesn't change. In your handler method you will have to have an if
block checking for SELECTED
or DESELECTED
status and do whatever there:
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// Your selected code here.
}
else if (e.getStateChange() == ItemEvent.DESELECTED) {
// Your deselected code here.
}
}
它只是工作得更好,因为你知道如果你在方法中,那么单选按钮要么刚刚被选中或取消选中,而不是用户只是出于某种未知原因在界面上敲打.
It just works better because you know that if you are in the method then the radio button has either just been selected or deselected, not that the user is just banging on the interface for some unknown reason.
这篇关于检测 JRadioButton 状态更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测 JRadioButton 状态更改
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01