1个回答
展开全部
Java程序:
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Main extends JFrame implements ItemListener {
JComboBox cmbProvince, cmbCity;
JTextField txtProvince, txtCity;
String[] provinces = {"请选择省份", "北京市", "上海市", "河南省"};
String[][] cities = {{"东城区", "西城区", "海淀区", "丰台区"}, {"浦东区", "徐汇区", "崇明县"}, {"郑州市", "洛阳市", "开封市"}};
public Main() {
super("请选择省份/城市");
this.setSize(350, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
init();
}
public void init() {
cmbProvince = new JComboBox(provinces);
cmbCity = new JComboBox(new String[]{"请选择城市"});
txtProvince = new JTextField(15);
txtCity = new JTextField(15);
this.add(cmbProvince);
this.add(txtProvince);
this.add(cmbCity);
this.add(txtCity);
cmbProvince.addItemListener(this);
cmbCity.addItemListener(this);
}
public static void main(String[] args) {
new Main();
}
@Override
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange() != ItemEvent.SELECTED){
return;
}
JComboBox cmb = (JComboBox) arg0.getSource();
int i;
int index;
if(cmb == cmbProvince) {
index = cmbProvince.getSelectedIndex();
if(index == 0) {
return;
}
cmbCity.removeAllItems();
cmbCity.addItem("请选择城市");
for(i=0; i<cities[index-1].length; i++) {
cmbCity.addItem(cities[index-1][i]);
}
txtProvince.setText("您选择的省份是:" + cmbProvince.getSelectedItem().toString());
}
else if(cmb == cmbCity) {
index = cmbCity.getSelectedIndex();
if(index == 0) {
return;
}
txtCity.setText("您选择的城市是:" + cmb.getSelectedItem().toString());
}
}
}
运行测试:
更多追问追答
追问
里面都用到那些技术,比如文本框按钮监听器组件什么的??
能不能解释一个这个程序
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询