这个我的一个小程序,怎样才能实现在实现ItemListener事件中只执行SELECTED而不执行DESELECTED?
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;public class JComboBoxDemo extends JFrame implements ItemListener { private JComboBox cob = null; public JComboBoxDemo() {
this.setTitle("westfish");
this.setSize(400, 300);
this.setLocationRelativeTo(null); cob = new JComboBox();
cob.setBounds(100, 100, 100, 26);
cob.addItem("请选择");
cob.addItem("管理员");//复选框添加元素
cob.addItem("教师");
cob.addItem("学生");
cob.addItemListener(this); this.getContentPane().add(cob);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public void itemStateChanged(ItemEvent e) {
System.out.println("e.getStateChange=" + e.getStateChange());
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("choose");
}
if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("not choose");
}
} public static void main(String[] args) {
new JComboBoxDemo();
}
}
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;public class JComboBoxDemo extends JFrame implements ItemListener { private JComboBox cob = null; public JComboBoxDemo() {
this.setTitle("westfish");
this.setSize(400, 300);
this.setLocationRelativeTo(null); cob = new JComboBox();
cob.setBounds(100, 100, 100, 26);
cob.addItem("请选择");
cob.addItem("管理员");//复选框添加元素
cob.addItem("教师");
cob.addItem("学生");
cob.addItemListener(this); this.getContentPane().add(cob);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public void itemStateChanged(ItemEvent e) {
System.out.println("e.getStateChange=" + e.getStateChange());
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("choose");
}
if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("not choose");
}
} public static void main(String[] args) {
new JComboBoxDemo();
}
}
解决方案 »
- java的基础知识求教
- 听说jdk1.6对线程池有个什么优化来着,有人知道么?
- 正则表达式,很简单,答完速给分
- 再发一帖,有关class文件打包问题
- 问个JDBC的问题??
- 初学,写了一个关于打开数据库的小程序,请大家看看!
- 很简单的问题,关于JList
- struts的用法
- A class defined within a method can only access final fields of the enclosing method.怎么理解啊?请举个简单的例子.
- 何处有IBM visualage下载(中文版,指中文手册)
- java 串口读数据--不能监听事件-急!!各位帮帮忙
- 谁帮我看看这个程序错在那了?
if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("not choose");
}//或者改成如下
if (e.getStateChange() == ItemEvent.DESELECTED) {
//什么也不做
}