ActionLintsner是所有监听器的父类,其它监听器可以监听的事件都可以被它捕获
ItemListener用于捕获带有item的组件产生的事件,如ComboBox,CheckBox,RadioButton,ToggleButton,接口中定义的itemStateChanged(ItemEvent e)将执行需要在已选定(或已取消选定)项时发生的操作
import java.awt.Dimension; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; public class YearMonthDay extends JPanel { private int year; private int month; private int day; private final int STARTYEAR = 2000;// 年份的开始值 private final int ENDYEAR = 2020;// 年份的结束值 JPanel contentPane; // 年月日的选择框 JComboBox cboYear = new JComboBox(); JComboBox cboMonth = new JComboBox(); JComboBox cboDay = new JComboBox(); // 年月日标签 JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); public YearMonthDay() { jbInit(); } private void jbInit() { this.setLayout(null); setSize(new Dimension(200, 80)); // 年的下拉选择框 cboYear.setFont(new java.awt.Font("Dialog", Font.BOLD, 13)); cboYear.setBounds(new Rectangle(0, 0, 55, 18)); // cboYear.addItemListener(new DateItemAdapter(this)); // 月的下拉选择框 cboMonth.setFont(new java.awt.Font("Dialog", Font.BOLD, 13)); cboMonth.setBounds(new Rectangle(80, 0, 45, 18)); cboMonth.addItemListener(new DateItemAdapter(this)); // 日的下拉选择框 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)); this.add(cboYear); this.add(cboMonth); this.add(cboDay); this.add(jLabel3); this.add(jLabel2); this.add(jLabel1); // 添加初始值 AddInfo(); //设置默认的时间 setYear(Integer.valueOf(cboYear.getSelectedItem().toString().trim())); setMonth(Integer.valueOf(cboMonth.getSelectedItem().toString().trim())); setDay(Integer.valueOf(cboDay.getSelectedItem().toString().trim())); } private void AddInfo() { // 年下拉选择框 for (int i = STARTYEAR; i < ENDYEAR; i++) { cboYear.addItem("" + i); } //年的默认时间是2009年 cboYear.setSelectedIndex(9); // 月下拉选择框 for (int i = 0; i < 12; i++) { cboMonth.addItem("" + (i + 1)); } // 日下拉选择框 for (int j = 0; j < 31; j++) { cboDay.addItem("" + (j + 1)); } } private int getYear() { return year; } private void setYear(int year) { this.year = year; } private int getMonth() { return month; } private void setMonth(int month) { this.month = month; } private int getDay() { return day; } private void setDay(int day) { this.day = day; } public String getDate(){ return year+"-"+month+"-"+day; } @Override public String toString() { return this.getDate(); } public void cboMonth_itemStateChanged(ItemEvent e) { Object yearObject = cboYear.getSelectedItem(); Object monthObject = cboMonth.getSelectedItem(); Object dayObject = cboDay.getSelectedItem(); if (yearObject != null && monthObject != null && dayObject != null) { this.setYear(Integer.valueOf(yearObject.toString().trim())); int month = Integer.valueOf(cboMonth.getSelectedItem().toString());// 取得选 中月份 this.setDay(Integer.valueOf(cboDay.getSelectedItem().toString() .trim())); cboDay.removeAllItems();// 清空日的下拉列表框 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 } }// end class // 事件监听器 class DateItemAdapter implements ItemListener { private YearMonthDay adaptee; DateItemAdapter(YearMonthDay adaptee) { this.adaptee = adaptee; } public void itemStateChanged(ItemEvent e) { adaptee.cboMonth_itemStateChanged(e); } }
http://blog.sina.com.cn/s/blog_5f20c4740100e47e.html
JComboBox ItemListener监听器
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
class itemListener implements ItemListener {
public void itemStateChanged(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){
//要执行的代码
}
即为当事件是选中某一项的时候,才执行该代码。
http://ribbonchen.blog.163.com/blog/static/118316505200988105942563/
(总结)Swing组件的使用---下拉式菜单(menu),弹出式菜单(JPopupMenu),选项卡窗体(JTabbedPane)
http://blog.csdn.net/lyxaiclr/article/details/7391318