Java学习-GUI编程-下拉框、列表框
下拉框
public class TestConboboxDemo extends JFrame {
public TestConboboxDemo(){
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestConboboxDemo();
}
}
运行程序,点击下拉,可以选择:
列表框
public class TestConboboxDemo extends JFrame {
public TestConboboxDemo(){
Container container = this.getContentPane();
String[] contents = {"1","2","3"};
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestConboboxDemo();
}
}
运行程序,列表可选:
列表也可以动态增减
public class TestConboboxDemo extends JFrame {
public TestConboboxDemo(){
Container container = this.getContentPane();
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("张三");
vector.add("李四");
vector.add("王五");
vector.add("赵六");
container.add(jList);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestConboboxDemo();
}
}
运行程序: