动态JComboBox我有以下数据(字符串):Course1: A1Course1: A2Course2: B1Course2: B2Course2: B3Course2: B4Course3: C1Course3: C2我想创造两个JComboBox(JComboBox 1,JComboBox 2)使JComboBox 1包含Course 1,Course 2,Course 3等。如果我从JComboBox 1中选择Course 2,那么对应的B1、B2、B3、B4应该在JComboBox 2中填充。如何实现这一点?非常感谢。
2 回答
白板的微信
TA贡献1883条经验 获得超3个赞
// first comboBoxfinal JComboBox courseBox = new JComboBox(
new String[]{"Course 1", "Course 2", "Course 3"});final JComboBox box2 = new JComboBox();
// Now listen for changescourseBox.addActionListener(new ActionListener(){
void actionPerformed(ActionEvent e){
if(courseBox.getSelectedItem().equals("Course 1")){
// we know that the user picked "Course 1", now change box2 to match
// first clear everything
box2.removeAllItems();
// now add back relevant values
box2.addItem("A1");
box2.addItem("A2");
}else if(...){
// ...
}
}});添加回答
举报
0/150
提交
取消
