package Window;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJComboBoxTest extends JFrame {
JComboBox jcb; //下拉框
JPanel jp; //面板
//构造函数
public MyJComboBoxTest(){
jp = new JPanel();
String str1[] = {"1", "2", "3", "4"};
jcb = new JComboBox(str1);
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String itemSize = (String) jcb.getSelectedItem();
System.out.println(itemSize);
}
});
jp.add(jcb);
this.setLayout(new GridLayout(1, 1));
this.add(jp);
this.setSize(200,200);
this.setTitle("下拉框");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyJComboBoxTest();
}
}