检测 JRadioButton 状态更改

发布于 2024-08-05 07:16:32 字数 191 浏览 2 评论 0原文

当用鼠标单击时,如何检测 JRadioButton 从“未选择”更改为“选择”?我尝试在按钮上使用 ActionListener,但每次单击单选按钮时都会触发该事件,而不仅仅是在将状态更改为“选定”时触发。

我想过维护一个记住按钮状态的布尔变量,并在 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'.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-08-12 07:16:32

查看 JRadioButton。addItemListener()

编辑:您不太可能想要使用changeListener,因为它每次点击都会触发多次。 itemListener 每次点击仅触发一次。
参见此处

编辑2:只是为了扩展这一点,每次用户单击 jradioButton 上的 actionListener 都会触发它,即使它已经被选中。如果这就是你想要的,那好吧,但我觉得这很烦人。我只想在选择或取消选择时收到通知。

ChangeListener 会针对各种情况触发,这意味着您的侦听器每次点击将收到 5 个或更多事件。不好。

仅当选定或取消选定状态发生变化时,项目侦听器才会触发。这意味着用户可以多次点击它,如果它不改变,它就不会触发。在您的处理程序方法中,您必须有一个 if 块检查 SELECTEDDESELECTED 状态并在那里执行任何操作:

@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.
    }
}

它效果更好,因为您知道如果您处于该方法中,则单选按钮刚刚被选择或取消选择,而不是用户只是出于某种未知原因敲击界面。

Look at JRadioButton.addItemListener()

EDIT: 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

EDIT2: 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.

A ChangeListener will fire for all sorts of things, meaning your listener will receive 5 or more events per click. Not good.

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.

遮云壑 2024-08-12 07:16:32

我相信您想添加一个 ChangeListener 实现。

I believe you want to add a ChangeListener implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文