首先是下拉框
public class JComboBoxDemo extends JFrame {
public JComboBoxDemo(){
Container container = this.getContentPane();
JComboBox box = new JComboBox();//首先是记住名字
box.addItem(null);//添加下拉选项的属性是addItem
box.addItem("1");
box.addItem("2");
box.addItem("3");
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(box);
container.add(box);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,400,600);
}
public static void main(String[] args) {
new JComboBoxDemo();
}
}
然后是关于列表框的使用
public class JComboBoxDemo extends JFrame {
public JComboBoxDemo(){
Container container = this.getContentPane();
Vector vector = new Vector<>();//创建一个vector容器;
JList jList = new JList(vector);//JList中可以放容器(动态),也可以放一些静态的数组啥的.
vector.add("1");
vector.add(2);
vector.add(3);
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,400,600);
}
public static void main(String[] args) {
new JComboBoxDemo();
}
}
下面是密码框的使用.
public class JComboBoxDemo extends JFrame {
public JComboBoxDemo(){
Container container = this.getContentPane();
Vector vector = new Vector<>();
JList jList = new JList(vector);
vector.add("1");
vector.add(2);
vector.add(3);
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,400,600);
}
public static void main(String[] args) {
new JComboBoxDemo();
}
}
还要区别textField和textArea的区别