GUI-Swing-下拉列表

下拉列表

代码:

 1 package com.luckylu.gui;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestComboboxDemo extends JFrame{
 7     public TestComboboxDemo(){
 8         Container container = this.getContentPane();
 9 
10         JComboBox status = new JComboBox();
11 
12         status.addItem(null);
13         status.addItem("张三");
14         status.addItem("李四");
15         status.addItem("王五");
16         status.addItem("赵六");
17 
18         container.add(status);
19 
20         this.setVisible(true);
21         this.setTitle("下拉列表案例");
22         this.setBounds(200,200,200,80);
23         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
24 
25     }
26 
27     public static void main(String[] args) {
28         new TestComboboxDemo();
29     }
30 }

结果:

 

列表框

方法一:静态数组

 1 package com.luckylu.gui;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class JListDemo extends JFrame{
 7     public JListDemo(){
 8         Container container = this.getContentPane();
 9 
10         //生成数组内容
11         String[] contents = {"辽宁","吉林","黑龙江","内蒙古","北京","天津","上海"};
12         //将数组装入列表
13         JList list = new JList(contents);
14         //将列表装入容器
15         container.add(list);
16 
17         this.setVisible(true);
18         this.setTitle("列表框案例");
19         this.setBounds(200,200,100,200);
20         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
21 
22     }
23 
24     public static void main(String[] args) {
25         new JListDemo();
26     }
27 }

结果:

 

 方法一:动态数组

 

 1 package com.luckylu.gui;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.util.Vector;
 6 
 7 public class JListDemo02 extends JFrame{
 8     public JListDemo02(){
 9         Container container = this.getContentPane();
10 
11         //生成动态数组内容
12         Vector contents = new Vector();
13         //先建立列表
14         JList list = new JList(contents);
15         //后添加数组内容,可动态增减
16         contents.add("辽宁");
17         contents.add("吉林");
18         contents.add("黑龙江");
19         contents.add("浙江");
20         contents.add("江苏");
21         contents.add("上海");
22 
23         container.add(list);
24 
25         this.setVisible(true);
26         this.setTitle("列表框案例");
27         this.setBounds(200,200,200,300);
28         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
29 
30     }
31 
32     public static void main(String[] args) {
33         new JListDemo02();
34     }
35 }

结果:

 

posted @ 2022-04-19 22:42  luckylu1983  阅读(79)  评论(0)    收藏  举报