创建一个窗体,上面有两个下拉列表,一个为学院下拉列表,一个为专业下拉列表,
要求用户选择某个院系以后,专业下拉列表中的选项相应进行更新。
软件学院:软件开发,软件测试,网络工程
外语学院:英语,日语,法语
管理学院:工商管理,会计,市场营销
<span style="font-size:24px;">import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ContainerEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; public class zuoye2 extends JFrame{ private JComboBox xyJComboBox,zyJComboBox; private String xyName [] ={"软件学院","外语学院","管理学院 "} ; private String zyName [][] ={ {"软件开发","软件测试","网络工程"}, {"英语","日语","法语"}, {"工商管理","会计","市场营销"}}; public zuoye2(){ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); setBounds(dimension.width / 4, dimension.height / 4, dimension.width / 4, dimension.height / 4); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); xyJComboBox=new JComboBox(xyName); zyJComboBox=new JComboBox(zyName[0]); Container container = this.getContentPane(); container.setLayout(new FlowLayout()); container.add(new JLabel("学院:")); container.add(xyJComboBox); container.add(new JLabel("专业:")); container.add(zyJComboBox); xyJComboBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent arg0) { zyJComboBox.removeAllItems(); int index=xyJComboBox.getSelectedIndex(); for(int i=0;i<zyName[index].length;i++){ zyJComboBox.addItem(zyName[index][i]); } } } ); setVisible(true); } public static void main(String[] args) { zuoye2 demo = new zuoye2(); } } </span>
原文地址:http://blog.csdn.net/zzc1510334836/article/details/44751769