ActionListener 和 ItemListener 都用于通过 JCheckBox 触发事件?
那么,它们之间有什么区别,在哪种情况下,它们中的一个比另一个更受欢迎?
ActionListener 和 ItemListener 都用于通过 JCheckBox 触发事件?
那么,它们之间有什么区别,在哪种情况下,它们中的一个比另一个更受欢迎?
两者ItemListener
以及ActionListener
, 在JCheckBox
具有相同行为的情况下。但是,主要区别是ItemListener
可以通过调用setSelected(true)
复选框来触发。作为一种编码实践,不要同时注册ItemListener
,ActionListener
以JCheckBox
避免不一致。
不同之处在于,ActionEvent
当对它执行操作时触发JCheckBox
它,即通过用鼠标单击它或用空格键或助记符来改变它的状态。无论选择还是取消选择,它都不会真正监听更改事件。JCheckBox
例如,如果JCheckBox c1
(say) 被添加到ButtonGroup
. 改变 other 的状态JCheckBoxes
不会ButtonGroup
触发ActionEvent
other JCheckBox
,而是ItemEvent
触发 an 。
最后的话:ItemEvent
即使用户通过选择另一个复选框JCheckBox
(在 a中时)取消选择复选框时也会触发An ButtonGroup
,但是ActionEvent
不会像那样生成,而是ActionEvent
仅侦听是否在JCheckBox
(ActionListener
仅注册了的)上执行了操作。它不知道ButtonGroup
和所有其他选择/取消选择的东西。
作为参考,这里有一个说明差异的sscce 。安慰:
已选择 ACTION_PERFORMED 取消选择 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();
}
});
}
}
我addActionListener
用于 JButtons 而addItemListener
对于JToggleButton
. 在后一种情况下if(event.getStateChange()==ItemEvent.SELECTED)
,我在检查/未选中时添加事件。
我自己一直在对此进行测试,并查看了这篇文章中的所有答案,我认为他们不能很好地回答这个问题。我自己试验以获得一个好的答案(下面的代码)。当单选按钮或复选框或我假设的任何其他类型的 Swing 项目中的状态发生更改时,您可以 100% 地使用 ActionListener 和 ItemListener 触发任一事件,因为它是 Object 类型。我可以告诉这两个侦听器之间的唯一区别是侦听器返回的事件对象的类型不同。并且您可以通过使用 ItemListener 而不是 ActionListener 的复选框获得更好的事件类型。
ActionEvent 和 ItemEvent 的返回类型将存储不同的方法,当事件类型被触发时可以使用这些方法。在下面的代码中,注释显示了每个 Class 返回的 Event 类型的 .get 方法的差异。
下面的代码设置了一个简单的 JPanel,其中包含 JRadioButtons、JCheckBoxes 和一个根据按钮配置更改的 JLabel 显示。我用一个动作监听器和一个项目监听器设置了所有的 RadioButtons 和 CheckBoxes。然后我写了下面的 Listener 类,其中 ActionListener 完全注释,因为我在这个实验中首先测试了它。您会注意到,如果您将此面板添加到框架并显示,则无论侦听器类型如何,所有单选按钮和复选框总是会触发,只需注释掉其中一个方法并尝试另一个,反之亦然。
实现方法的返回类型是两者之间的主要区别。两个侦听器都以相同的方式触发事件。在上面的评论中解释得更好一点是由于返回的事件类型,复选框应该使用 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);
}
}
}
}