import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Exec65 extends JFrame implements ItemListener{ JComboBox box; JTextField text; static String[] lessons = {"语文","数学","化学","物理"}; public static void main(String[] args) { new Exec65(); } public Exec65(){ this.setTitle("Test"); this.setSize(500,500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); box = new JComboBox(lessons); box.setEditable(true); box.addItemListener(this); text = new JTextField(10); add(box,BorderLayout.NORTH); add(text,BorderLayout.CENTER); this.setVisible(true); pack(); } @Override public void itemStateChanged(ItemEvent e) { text.setText(((JComboBox)(e.getSource())).getSelectedItem().toString()); } }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)