JCheckbox – ActionListener和ItemListener?

ActionListener和ItemListener都用于使用JCheckBox触发一个事件?

那么,它们之间有什么区别呢?在这种情况下,其中一个比另一个更受欢迎?

JCheckBox情况下, ItemListenerActionListener都具有相同的行为。 但是,主要区别是可以通过调用checkbox上的setSelected(true)来触发ItemListener 。 作为一种编码实践,不要将ItemListenerItemListener注册,以避免不一致。

不同之处在于,在JCheckBox上执行操作时触发ActionEvent ,即通过用鼠标或空格键或助记符单击鼠标来更改状态。 它没有真正地听取更改事件,不pipeJCheckBox是选中还是取消选中。

例如,如果JCheckBox c1 (说)被添加到一个ButtonGroup 。 更改ButtonGroup中其他JCheckBoxes的状态不会触发其他JCheckBox上的ActionEvent ,而是触发ItemEvent

最后的话:即使当用户通过select另一个JCheckBox (当在ButtonGroup )时取消selectcheckbox时,也会触发一个ItemEvent ,但是ActionEvent不是像这样生成的,而是ActionEvent只监听是否在JCheckBox上执行操作( ActionListener只是注册)或不。 它不知道ButtonGroup和所有其他select/取消select的东西。

作为参考,这里是一个sscce ,说明不同之处。 安慰:

 SELECTED
 ACTION_PERFORMED
 DESELECTED
 ACTION_PERFORMED

码:

 import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; /** @see http://stackoverflow.com/q/9882845/230513 */ public class Listeners { private void display() { JFrame f = new JFrame("Listeners"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox b = new JCheckBox("JCheckBox"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(e.getID() == ActionEvent.ACTION_PERFORMED ? "ACTION_PERFORMED" : e.getID()); } }); b.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println(e.getStateChange() == ItemEvent.SELECTED ? "SELECTED" : "DESELECTED"); } }); JPanel p = new JPanel(); p.add(b); f.add(p); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Listeners().display(); } }); } } 

对于JButton,我使用addActionListener ,而对于JToggleButtonaddItemListener更方便。 与if(event.getStateChange()==ItemEvent.SELECTED) ,在后一种情况下,每当JToggleButton被选中/取消选中时,我都会添加事件。

我一直在testing这个,看看这个post的所有答案,我不认为他们回答这个问题很好。 为了得到一个很好的答案,我尝试了自己(代码如下)。 当状态在单选button或checkbox中被更改时,或者由于它是Objecttypes而被假设的任何其他types的Swing项目,您都可以使用ActionListener和ItemListener触发任一事件。 我可以告诉这两个侦听器的唯一区别是与侦听器返回的Event对象的types是不同的。 而且使用ItemListener而不是ActionListener可以获得更好的事件types。

ActionEvent和ItemEvent的返回types将存储不同的方法,这些方法可能在事件types被触发时使用。 在下面的代码中,注释显示每个Class返回的Eventtypes的.get方法的区别。

下面的代码用JRadioButtons,JCheckBoxes和一个基于buttonconfiguration改变的JLabel显示来设置一个简单的JPanel。 我用Action Listener和Item Listener同时设置了所有的RadioButton和CheckBox。 然后我用ActionListener完全评论了下面的Listener类,因为我在这个实验中首先testing了它。 你会注意到,如果你将这个面板添加到一个框架并显示,所有的单选button和checkbox总是会触发,无论是什么types的监听器,只需注释掉其中的方法,然后尝试另一个,反之亦然。

返回types到实现的方法是两者之间的主要区别。 两个听众都以同样的方式触发事件。 在上面的注释中解释得更好一点是由于返回的事件types,checkbox应该使用ItemListener而不是ActionListener。

 package EventHandledClasses; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RadioButtonsAndCheckBoxesTest extends JPanel{ JLabel display; String funny, serious, political; JCheckBox bold,italic; JRadioButton funnyQuote, seriousQuote, politicalQuote; ButtonGroup quotes; public RadioButtonsAndCheckBoxesTest(){ funny = "You are not ugly, you were just born... different"; serious = "Recommend powdered soap in prison!"; political = "Trump can eat a little Bernie, but will choke on his Birdie"; display = new JLabel(funny); Font defaultFont = new Font("Ariel",Font.PLAIN,20); display.setFont(defaultFont); bold = new JCheckBox("Bold",false); bold.setOpaque(false); italic = new JCheckBox("Italic",false); italic.setOpaque(false); //Color itemBackground = funnyQuote = new JRadioButton("Funny",true); funnyQuote.setOpaque(false); seriousQuote = new JRadioButton("Serious"); seriousQuote.setOpaque(false); politicalQuote = new JRadioButton("Political"); politicalQuote.setOpaque(false); quotes = new ButtonGroup(); quotes.add(funnyQuote); quotes.add(seriousQuote); quotes.add(politicalQuote); JPanel primary = new JPanel(); primary.setPreferredSize(new Dimension(550, 100)); Dimension standard = new Dimension(500, 30); JPanel radioButtonsPanel = new JPanel(); radioButtonsPanel.setPreferredSize(standard); radioButtonsPanel.setBackground(Color.green); radioButtonsPanel.add(funnyQuote); radioButtonsPanel.add(seriousQuote); radioButtonsPanel.add(politicalQuote); JPanel checkBoxPanel = new JPanel(); checkBoxPanel.setPreferredSize(standard); checkBoxPanel.setBackground(Color.green); checkBoxPanel.add(bold); checkBoxPanel.add(italic); primary.add(display); primary.add(radioButtonsPanel); primary.add(checkBoxPanel); //Add Action Listener To test Radio Buttons funnyQuote.addActionListener(new ActionListen()); seriousQuote.addActionListener(new ActionListen()); politicalQuote.addActionListener(new ActionListen()); //Add Item Listener to test Radio Buttons funnyQuote.addItemListener(new ItemListen()); seriousQuote.addItemListener(new ItemListen()); politicalQuote.addItemListener(new ItemListen()); //Add Action Listener to test Check Boxes bold.addActionListener(new ActionListen()); italic.addActionListener(new ActionListen()); //Add Item Listener to test Check Boxes bold.addItemListener(new ItemListen()); italic.addItemListener(new ItemListen()); //adds primary JPanel to this JPanel Object add(primary); } private class ActionListen implements ActionListener{ public void actionPerformed(ActionEvent e) { /* Different Get Methods from ItemEvent e.getWhen() e.getModifiers() e.getActionCommand()*/ /*int font=Font.PLAIN; if(bold.isSelected()){ font += Font.BOLD; } if(italic.isSelected()){ font += Font.ITALIC; } display.setFont(new Font("Ariel",font,20)); if(funnyQuote.isSelected()){ display.setText(funny); } if(seriousQuote.isSelected()){ display.setText(serious); } if(politicalQuote.isSelected()){ display.setText(political); }*/ } } private class ItemListen implements ItemListener { public void itemStateChanged(ItemEvent arg0) { /* Different Get Methods from ActionEvent arg0.getItemSelectable() arg0.getStateChange() arg0.getItem()*/ int font=Font.PLAIN; if(bold.isSelected()){ font += Font.BOLD; } if(italic.isSelected()){ font += Font.ITALIC; } display.setFont(new Font("Ariel",font,20)); if(funnyQuote.isSelected()){ display.setText(funny); } if(seriousQuote.isSelected()){ display.setText(serious); } if(politicalQuote.isSelected()){ display.setText(political); } } } }