itemStateChanged()的语句为何会被执行两遍?
下面是我编写的一个测试类,可是每当我点击jbReadyButton,jbAcwButton和jbBusyButton任意一个按钮时,itemStateChanged()中的System.out.println("")语句会被执行两遍,结果输出如下:
Busy
Busy
Acw
Acw
Ready
Ready
......
请问这是为什么?
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
public class testItemList extends Applet implements ActionListener,ItemListener
{
private JComboBox jcbStatus;
private JButton jbReadyButton;
private JButton jbAcwButton;
private JButton jbBusyButton;
private JButton jbStatusButton;
private boolean bOkButtonPressed;
String response="";
public testItemList()
{
setSize(350,220);
jbReadyButton = new JButton("Ready");
jbAcwButton = new JButton("Acw");
jbBusyButton = new JButton("Busy");
jbStatusButton = new JButton("qqqq");
jcbStatus = new JComboBox();
jcbStatus.addItem(new String("Ready"));
jcbStatus.addItem(new String("Acw"));
jcbStatus.addItem(new String("Busy"));
jcbStatus.addItem(new String("Other"));
this.add(jbReadyButton);
this.add(jbAcwButton);
this.add(jbBusyButton);
this.add(jbStatusButton);
this.add(jcbStatus);
jcbStatus.setVisible(true);
jbReadyButton.addActionListener(this);
jbAcwButton.addActionListener(this);
jbStatusButton.addActionListener(this);
jbBusyButton.addActionListener(this);
jcbStatus.addItemListener(this);
}
public void init()
{
}
public synchronized void actionPerformed(ActionEvent event)
{
if (event.getSource() == jbReadyButton)
{
jcbStatus.setSelectedIndex(0);
return;
}
if (event.getSource() == jbAcwButton)
{
jcbStatus.setSelectedIndex(1);
return;
}
if (event.getSource() == jbBusyButton)
{
jcbStatus.setSelectedIndex(2);
return;
}
}
public synchronized void itemStateChanged(ItemEvent e)
{
if(e.getSource() == jcbStatus)
{
if(jcbStatus.getSelectedIndex() == 0)
{
jbStatusButton.setText("Ready");
System.out.println("Ready");
return;
}
if(jcbStatus.getSelectedIndex() == 1)
{
jbStatusButton.setText("Acw");
System.out.println("Acw");
return;
}
if(jcbStatus.getSelectedIndex() == 2)
{
jbStatusButton.setText("Busy");
System.out.println("Busy");
return;
}
if(jcbStatus.getSelectedIndex() == 3)
{
jbStatusButton.setText("Other");
System.out.println("Other");
return;
}
}
return;
}
}