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