英文:
Which event type does JVM generate when a user clicks a radio button? Is it an action event or an item event?
问题
我对Swing组件相当新。然而,我看过许多示例,其中单选按钮实现了actionListener和itemListener。在这种情况下,哪种处理事件的方式是正确的呢?
英文:
I am fairly new to swing components. However, I have seen many examples where a radio button implements actionListener as well as itemListener. Which one is the correct way of handling events in such a case?
答案1
得分: 2
当用户点击单选按钮时,JVM 会生成哪种事件类型?它是操作事件还是项事件?
这取决于已连接到单选按钮的监听器!例如,
AbstractButton.addActionListener(ActionListener)
和/或addItemListener(ItemListener)
中的一个或两个。另外: 对于这个情况,我建议使用
ActionListener
。ItemListener
会在选择ButtonGroup
中的按钮时产生两个事件。其中一个事件是刚刚被取消选择的项的事件。
英文:
> Which event type does JVM generate when a user clicks a radio button? Is it an action event or an item event?
It depends which listener(s) has(/have) been attached to the radio buttons! E.G. Either or both of AbstractButton.addActionListener(ActionListener)
/ addItemListener(ItemListener)
.
Aside: for this I'd recommend an ActionListener
.<br>The ItemListener
will produce two events when a button in a ButtonGroup
is selected. One of the events is for the item which just got deselected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论