简体   繁体   English

Java:JRadioButton上的ActionEvent和ItemEvent有什么区别?

[英]Java: What's the difference between ActionEvent and ItemEvent on a JRadioButton?

They're both raised once after the mouse button is released and both can have all the information available on the JRadioButton right? 释放鼠标按钮后,它们都会被提升一次,并且两者都可以在JRadioButton上获得所有可用的信息吗? Is there any difference? 有什么区别吗?

An ItemListener s are notified when ever the state of the button is changed, whether through a user interacting with the button or programmatically (via the setSelected method). 无论是通过用户与按钮交互还是通过编程方式(通过setSelected方法)更改按钮的状态,都会通知ItemListener ActionListener s on the other hand will be called when a user interacts with the button (but can be simulated programmatically via the onClick method). 另一方面,当用户与按钮交互时,将调用ActionListener (但可以通过onClick方法以编程方式进行模拟)。

Note that a user interacting with the button such as clicking or hitting the space bar will also change the state of the button and raise an item event as well as an action event. 请注意,与按钮交互的用户(例如单击或按空格键)也将更改按钮的状态并引发项目事件以及动作事件。 Generally, you will want to define either one or the other, don't listen for both action events and item events on the button. 通常,您需要定义其中一个,不要同时监听按钮上的动作事件和项目事件。

ItemEvent is specific event that indicates that the state has changed in a component specified by the itemStateChanged in the ItemListener. ItemEvent是特定事件,指示ItemListener中itemStateChanged指定的组件中的状态已更改。 JRadioButton has two states on and off. JRadioButton开启和关闭两个州。 ItemEvent is also useful when a radio button is part of a button group . 当单选按钮是按钮组的一部分时,ItemEvent也很有用。

I think you should use the item listener when you are interested in state changes in the radio button and an action listener when you want to do something when the radio button is clicked. 当你想要在单击单选按钮时执行某些操作时,如果您对单选按钮和动作侦听器中的状态更改感兴趣,我认为您应该使用项侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM