ActionLintsner都实现此接口,其它监听器可以监听的事件都可以被它捕获
public interface ActionListener extends EventListener
The listener interface for receiving action events.
The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
actionPerformed(ActionEvent e)
Invoked when an action occurs.
ItemListener用于捕获带有item的组件产生的事件,如ComboBox,CheckBox,RadioButton,ToggleButton,接口中定义的itemStateChanged(ItemEvent e)将执行需要在已选定(或已取消选定)项时发生的操作
public interface ItemListenerextends EventListenerThe listener interface for receiving item events.
The class that is interested in processing an item event implements this interface. The object created with that class is then registered with a component using the component's addItemListener method. When an item-selection event occurs, the listener object's itemStateChanged method is invoked.
void itemStateChanged(ItemEvent e)
Invoked when an item has been selected or deselected by the user. The code written for this method performs the operations that need to occur when an item is selected (or deselected).
packageswing.combox;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Rectangle;importjava.awt.event.ItemEvent;importjava.awt.event.ItemListener;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;public class YearMonthDay extendsJFrame {private static final long serialVersionUID = 1L;private final int STARTYEAR = 2000;//年份的开始值
private final int ENDYEAR = 2020;//年份的结束值
JPanel contentPane;//年月日的选择框
JComboBoxcboYear= new JComboBox(),
cboMonth= new JComboBox(),
cboDay= new JComboBox();//年月日标签
JLabel jLabel1 = newJLabel();
JLabel jLabel2= newJLabel();
JLabel jLabel3= newJLabel();publicYearMonthDay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbInit();
}private voidjbInit(){
contentPane=(JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("年月日下拉列表级联");//年的下拉选择框
cboYear.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
cboYear.setBounds(new Rectangle(0, 0, 55, 18));//月的下拉选择框
cboMonth.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
cboMonth.setBounds(new Rectangle(80, 0, 45, 18));
cboMonth.addItemListener(new DateItemAdapter(this));//cboMonth//日的下拉选择框
cboDay.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
cboDay.setBounds(new Rectangle(150, 0, 45, 18));//cboDay.setEditable(true);//年的label
jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
jLabel3.setText("年");
jLabel3.setBounds(new Rectangle(60, 0, 20, 20));//月的label
jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
jLabel2.setText("月");
jLabel2.setBounds(new Rectangle(130, 0, 20, 20));//日的label
jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
jLabel1.setText("日");
jLabel1.setBounds(new Rectangle(200, 0, 20, 20));
contentPane.add(cboYear);
contentPane.add(cboMonth);
contentPane.add(cboDay);
contentPane.add(jLabel3);
contentPane.add(jLabel2);
contentPane.add(jLabel1);//添加初始值
AddInfo();
}private voidAddInfo() {//年下拉选择框
for (int i = STARTYEAR; i < ENDYEAR; i++) {
cboYear.addItem("" +i);
}//月下拉选择框
for (int i = 0; i < 12; i++) {
cboMonth.addItem("" + (i + 1));
}//日下拉选择框
for (int j = 0; j < 31; j++) {
cboDay.addItem("" + (j + 1));
}
}public voidcboMonth_itemStateChanged(ItemEvent e) {
Object obj= cboMonth.getSelectedItem();//取得选中月份
if (obj != null) {
cboDay.removeAllItems();//清空日的下拉列表框
int month =Integer.valueOf(obj.toString());if (month==5) {//test JComboBox api combox method setEnabled()
this.cboDay.setEnabled(false);
}int days = 31;if (month == 4 || month == 6 || month == 9 || month == 11) {
days= 30;
}else if (month == 2) {//取得选中年份
int year =Integer.parseInt(cboYear.getSelectedItem()
.toString());if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {//是闰年
days = 29;
}else{//不是闰年
days = 28;
}
}//if
for (int j = 0; j < days; j++) {
cboDay.addItem("" + (j + 1));
}//for
}//if
}//if
}//end class//事件监听器
class DateItemAdapter implementsItemListener {privateYearMonthDay adaptee;
DateItemAdapter(YearMonthDay adaptee) {this.adaptee =adaptee;
}public voiditemStateChanged(ItemEvent e) {if (e.getStateChange()==ItemEvent.SELECTED) {
System.out.println("was invoked");
adaptee.cboMonth_itemStateChanged(e);
}
}
}
packageswing.combox;public classTest {public static voidmain(String[] args) {
YearMonthDay testDate= newYearMonthDay();
testDate.setVisible(true);
testDate.setBounds(200, 200, 250, 100);
}
}
JComboBox ItemListener监听器
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
class itemListener implements ItemListener {
public voiditemStateChanged(ItemEvent e){
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox jcb = (JComboBox) e.getSource();
System.out.println((String) (jcb.getSelectedItem()));
System.out.println(jcb.getSelectedIndex());
} else {
}
}
}
今天用到了JComboBox的事件监听类 ItemListener,程序在执行时发现事件总是被触发两次,上网看了些资料,原来和ItemListener类中的方法itemStateChanged()事件的itemState有关,itemState在这里的状态有两个,Selected 和 deSelected(即选中和未被选中),所以,当改变下拉列表中被选中的项的时候,其实是触发了两次事件,第一次是上次被选中的项的 State 由 Selected 变为 deSelected ,即取消选择, 第二次是本次被选中的项的 State 由 deSelected 变为 Selected ,即新选中,所以,必然的 ItemStateChanged 事件中的代码要被执行两次了。
解决方法 就是:
if(e.getStateChange() == ItemEvent.SELECTED){
//要执行的代码
}
即为当事件是选中某一项的时候,才执行该代码。