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;
}
}
解决方案 »
- 请教组合求和的算法
- 为什么使用EL表达式却没有输出结果
- 紧急求救:java程序没有错误,但是在NetBeans中运行出现下面的错误?
- Clipboard类中setContents函数参数
- 什么是容器的公平性
- 问一个数据库连接的问题,请高手解答。。。
- 寻人启示:有哪位是在哈工大珠海分校读在职研究生的吗?有事请教
- 小调查----大家都用什么JAVA编译器?(来者有分)
- RMI配置问题!还是不行啊,一天了,急死了!高手们帮帮我吧
- 如何使多层应用系统开发适应于易变的用户需求?因为在多层应用系统中,当用户需求要求改变时,系统中的每一层可能都要改变,这种费用是很
- 异常处理中何时抛异常???
- 中午吃饭听技术人员说什么“拉夫瑞”这样的技术,请问是什么?
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;
}
}这样就可以了