【例 11-14】一个说明组合框用法的应用程序。程序中声明的组合框子类实现ItemLister接口和ActionListener接口。组合框子类的窗口中设置了一个文本框和一个组合框,组合框中有三个选择。实现接口的监视方法将组合框的选择结果在文本框中显示。 public class Example6_4{ public static void main(String args[]){ ComboBoxDemo mycomboBoxGUI = new ComboBoxDemo(); } } class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{ public static final int Width = 350; public static final int Height = 150; String proList[] = { "踢足球","打篮球","打排球" }; JTextField text; JComboBox comboBox; public ComboBoxDemo(){ setSize(Width,Height); setTitle("组合框使用示意程序"); Container conPane = getContentPane(); conPane.setBackground(Color.BLUE); conPane.setLayout(new FlowLayout()); comboBox = new JComboBox(proList); comboBox.addActionListener(this); combobox.addItemListener(this); comboBox.setEditable(true);//响应键盘输入 conPane.add(comboBox); text = new JTextField(10); conPane.add(text); this.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==comboBox) text.setText(comboBox.getSelectedItem().toString()); } public void itemStateChanged(ItemEvent e){ if(e.getSource()==comboBox){ text.setText(comboBox.getSelectedItem().toString()); } } }