itemStateChanged()的语句为何会被执行两遍?

fengkuangzhijian 2002-09-22 05:05:44
下面是我编写的一个测试类,可是每当我点击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;
}


}
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧