import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBox extends Applet{ String str[]={"计算机1班","计算机2班","计算机3班","计算机4班"}; Label l1=new Label("班级选项:"); Label l2=new Label("选中的班级为:"); Label l3=new Label("计算机1班"); JComboBox cb=new JComboBox(str); JPanel row1=new JPanel(); JPanel row2=new JPanel(); FlowLayout layout = new FlowLayout(); public void init(){ row1.setLayout(layout); row1.add(l1); row1.add(cb); this.add(row1); row2.setLayout(layout); row2.add(l2); row2.add(l3); this.add(row2); } public void start(){ cb.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent arg0) { l3.setText(arg0.getItem().toString()); } }); } }
时间: 2024-10-19 03:31:39