java中省份与城市组合框怎么编(也就是ComboBox用什么来监听)? 在线等
3个回答
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {
Vector<String> v1 = new Vector<String>();
v1.add("请选择");
v1.add("1");
v1.add("2");
final JComboBox c1 = new JComboBox(v1);
final JComboBox c2 = new JComboBox();
c1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String value = (String) c1.getSelectedItem();
c2.removeAllItems();
if (value.equals("1")) {
c2.addItem("111");
c2.addItem("1111");
}
if (value.equals("2")) {
c2.addItem("222");
c2.addItem("2222");
}
}
});
this.setLayout(new FlowLayout());
this.add(c1);
this.add(c2);
}
public static void main(String[] args) {
Test t = new Test();
t.setSize(200, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {
Vector<String> v1 = new Vector<String>();
v1.add("请选择");
v1.add("1");
v1.add("2");
final JComboBox c1 = new JComboBox(v1);
final JComboBox c2 = new JComboBox();
c1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String value = (String) c1.getSelectedItem();
c2.removeAllItems();
if (value.equals("1")) {
c2.addItem("111");
c2.addItem("1111");
}
if (value.equals("2")) {
c2.addItem("222");
c2.addItem("2222");
}
}
});
this.setLayout(new FlowLayout());
this.add(c1);
this.add(c2);
}
public static void main(String[] args) {
Test t = new Test();
t.setSize(200, 200);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询