Java捕获选项事件ItemEvent
[java]
package com.han;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
/**
* 捕获选项事件ItemEvent
* @author HAN
*
*/
public class ItemEvent_1 extends JFrame {
/**
*
*/
private static final long serialVersionUID = -9005090453541883935L;
public ItemEvent_1() {
// TODO Auto-generated constructor stub
Container container = getContentPane();
container.setLayout(new FlowLayout());
Vector<String> vector = new Vector<String>();
for (int i = 1; i<6; i++)
vector.add("选项" + i);
DefaultComboBoxModel<String> defaultComboBoxModel =
new DefaultComboBoxModel<String>(vector);
JComboBox<String> comboBox = new JComboBox<String>(defaultComboBoxModel);
container.add(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
String item = e.getItem().toString();
int stateChange = e.getStateChange();
if (stateChange == ItemEvent.SELECTED) {
System.out.println("此次事件由选中“" + item + "”触发!");
}else if (stateChange == ItemEvent.DESELECTED) {
System.out.println("此次事件由取消选中“" + item + "”触发!");
}else {
System.out.println("此次事件由其它原因触发!");
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ItemEvent_1 frame = new ItemEvent_1();
frame.setTitle("选项事件示例");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(0, 0, 300, 100);
}
}
package com.han;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
/**
* 捕获选项事件ItemEvent
* @author HAN
*
*/
public class ItemEvent_1 extends JFrame {
/**
*
*/
private static final long serialVersionUID = -9005090453541883935L;
public ItemEvent_1() {
// TODO Auto-generated constructor stub
Container container = getContentPane();
container.setLayout(new FlowLayout());
Vector<String> vector = new Vector<String>();
for (int i = 1; i<6; i++)
vector.add("选项" + i);
DefaultComboBoxModel<String> defaultComboBoxModel =
new DefaultComboBoxModel<String>(vector);
JComboBox<String> comboBox = new JComboBox<String>(defaultComboBoxModel);
container.add(comboBox);
comboBox.addItemListener(new ItemListener() {
@Override www.zzzyk.com
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
String item = e.getItem().toString();
int stateChange = e.getStateChange();
if (stateChange == ItemEvent.SELECTED) {
System.out.println("此次事件由选中“" + item + "”触发!");
}else if (stateChange == ItemEvent.DESELECTED) {
System.out.println("此次事件由取消选中“" + item + "”触发!");
}else {
System.out.println("此次事件由其它原因触发!");
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ItemEvent_1 frame = new ItemEvent_1();
frame.setTitle("选项事件示例");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(0, 0, 300, 100);
}
}
作者:Gaowen_HAN
补充:软件开发 , Java ,