4个combobox,对每个combobox添加Listener,并用一个静态变量记录,Listener被出发的次数,为什么没点击一次combobox会出发两次Listener呢?
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class testcombobox extends Applet {
private boolean isStandalone = false; int ComboBoxNum =4;
int SubPanelNum = 4;
JComboBox[] ComboBox = new JComboBox[4];
JLabel label = new JLabel();// for test
static int ii; //for test
int AvaliableNum = 0; //Number of avaliable subpanels //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public testcombobox() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null); for (int i = 0; i < ComboBoxNum; i++)
{
ComboBox[i]=new JComboBox();
ComboBox[i].addItem("");
for (int j = 0; j < 4; j++)
ComboBox[i].addItem(Integer.toString(j));
ComboBox[i].setBounds(new Rectangle(10,50*i,100,20));
this.add(ComboBox[i]); label.setBounds(150,0,50,20);
this.add(label);
label.setText("hello");
ComboBox[i].addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
ii++;
label.setText(Integer.toString(ii));
}
}
);
ComboBox[0].setSelectedIndex(1);
} }
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
}
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class testcombobox extends Applet {
private boolean isStandalone = false; int ComboBoxNum =4;
int SubPanelNum = 4;
JComboBox[] ComboBox = new JComboBox[4];
JLabel label = new JLabel();// for test
static int ii; //for test
int AvaliableNum = 0; //Number of avaliable subpanels //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public testcombobox() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null); for (int i = 0; i < ComboBoxNum; i++)
{
ComboBox[i]=new JComboBox();
ComboBox[i].addItem("");
for (int j = 0; j < 4; j++)
ComboBox[i].addItem(Integer.toString(j));
ComboBox[i].setBounds(new Rectangle(10,50*i,100,20));
this.add(ComboBox[i]); label.setBounds(150,0,50,20);
this.add(label);
label.setText("hello");
ComboBox[i].addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
ii++;
label.setText(Integer.toString(ii));
}
}
);
ComboBox[0].setSelectedIndex(1);
} }
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
}
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class testcombobox extends Applet {
private boolean isStandalone = false; int ComboBoxNum =4;
int SubPanelNum = 4;
JComboBox[] ComboBox = new JComboBox[4];
JLabel label = new JLabel();// for test
static int ii=0; //for test
int AvaliableNum = 0; //Number of avaliable subpanels //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public testcombobox() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(null); for (int i = 0; i < ComboBoxNum; i++)
{
ComboBox[i]=new JComboBox();
ComboBox[i].addItem("");
for (int j = 0; j < 4; j++)
ComboBox[i].addItem(Integer.toString(j));
ComboBox[i].setBounds(new Rectangle(10,50*i,100,20));
this.add(ComboBox[i]); label.setBounds(150,0,50,20);
this.add(label);
label.setText("hello");
ComboBox[i].addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
if(ie.getStateChange()==ie.DESELECTED){
return;
}
ii++;
label.setText(Integer.toString(ii));
}
}
);
ComboBox[0].setSelectedIndex(1);
} }
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
}这样就可以了