JAVA swing常用组件

在图形用户界面编程中,我们常常会提供用户登陆界面,比如登陆到会员管理系统、登陆到工资管理系统、仓库管理系统...

常用组件

Ⅰ.文本框(JTextField)

Ⅱ.密码框(JPasswordField)

Ⅲ.标签(JLable)

Ⅳ.复选框(JCheckBox)

Ⅴ.单选框(JRadioButton)

Ⅵ.组合单选框(ButtonGroup)

Ⅶ.下拉框(JComboBox)

Ⅷ.列表(JList)

Ⅸ.滚动窗格组件(JScrollPane)

Ⅹ.多行文本框(JTextArea)

ⅩⅠ.选项卡(JTabbedPane)

实例

简单会员管理系统登录界面

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月05日
 4  * 内容:gui窗体
 5  * 步骤:
 6  *  1.继承JFrame容器
 7  *  2.定义需要的组件
 8  *  3.创建组件
 9  *  4.设置布局管理器
10  *  5.添加组件
11  *  6.对窗体设置
12  *  7.显示窗体
13  */
14 
15 package com.beekc.www;
16 import java.awt.*;
17 import javax.swing.*;
18 
19 //继承JFrame
20 public class Beekc extends JFrame{
21 
22     //定义组件
23     JPanel jPanel1,jPanel2,jPanel3;
24     JLabel jlb1,jlb2;
25     JTextField jt;
26     JPasswordField jpssswd;
27     JButton jb1,jb2;
28 
29     public static void main(String[] args){
30         Beekc beekc = new Beekc();
31     }
32 
33     public Beekc(){
34         //创建组件
35         jPanel1 = new JPanel();
36         jPanel2 = new JPanel();
37         jPanel3 = new JPanel();
38 
39         jlb1 = new JLabel("用户");
40         jlb2 = new JLabel("密码");
41         jt = new JTextField(10);
42         jpssswd = new JPasswordField(10);
43         jb1 = new JButton("登录");
44         jb2 = new JButton("注册");
45 
46         //设置布局管理器
47         this.setLayout(new GridLayout(3,1));
48         //添加JPanel
49         jPanel1.add(jlb1);
50         jPanel1.add(jt);
51         jPanel2.add(jlb2);
52         jPanel2.add(jpssswd);
53         jPanel3.add(jb1);
54         jPanel3.add(jb2);
55         //JPanel添加到JFanel
56         this.add(jPanel1);
57         this.add(jPanel2);
58         this.add(jPanel3);
59 
60         //窗体设置
61         this.setTitle("会员管理系统");
62         this.setSize(270,150);
63         this.setResizable(false);
64         this.setLocation(200,200);
65         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 
67         //显示
68         this.setVisible(true);
69 
70     }
71 
72 }

用户注册界面

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月06日
 4  * 内容:gui窗体
 5  * 步骤:
 6  *  1.继承JFrame容器
 7  *  2.定义需要的组件
 8  *  3.创建组件
 9  *  4.设置布局管理器
10  *  5.添加组件
11  *  6.对窗体设置
12  *  7.显示窗体
13  */
14 
15 package com.beekc.www;
16 
17 
18 import javax.swing.*;
19 import java.awt.*;
20 
21 //继承JFrame
22 public class Beekc extends JFrame {
23 
24     //定义组件
25     JPanel jPanel1,jPanel2,jPanel3;
26     JLabel jlb1,jlb2;
27     JCheckBox jc1,jc2,jc3;
28     JRadioButton jrb1,jrb2;
29     ButtonGroup bg;
30     JButton jb1,jb2;
31 
32     public static void main(String[] args){
33         Beekc beekc = new Beekc();
34     }
35 
36     //构造函数
37     public Beekc(){
38         //创建组件
39         jPanel1 =new JPanel();
40         jPanel2 =new JPanel();
41         jPanel3 =new JPanel();
42         bg = new ButtonGroup();
43 
44         jlb1 = new JLabel("你喜欢的运动");
45         jlb2 = new JLabel("性别");
46         jc1 =new JCheckBox("足球");
47         jc2 =new JCheckBox("篮球");
48         jc3 =new JCheckBox("排球");
49         jrb1 = new JRadioButton("男");
50         jrb2 = new JRadioButton("女");
51         jb1 = new JButton("注册");
52         jb2 = new JButton("取消");
53 
54         //设置布局管理器
55 
56         //JRadioButton添加ButtonGroup为一组
57         bg.add(jrb1);
58         bg.add(jrb2);
59 
60         //组件加到JPanel
61         jPanel1.add(jlb1);
62         jPanel1.add(jc1);
63         jPanel1.add(jc2);
64         jPanel1.add(jc3);
65         jPanel2.add(jlb2);
66         jPanel2.add(jrb1);
67         jPanel2.add(jrb2);
68         jPanel3.add(jb1);
69         jPanel3.add(jb2);
70         //布局
71         this.setLayout(new GridLayout(3,1));
72 
73         //添加组件
74         this.add(jPanel1);
75         this.add(jPanel2);
76         this.add(jPanel3);
77 
78         //对窗体设置
79         this.setTitle("用户注册");
80         this.setSize(270,150);
81         this.setResizable(false);
82         this.setLocation(200,200);
83         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84 
85         //显示
86         this.setVisible(true);
87 
88 
89     }
90 
91 }

用户调查界面

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月06日
 4  * 内容:gui窗体
 5  * 步骤:
 6  *  1.继承JFrame容器
 7  *  2.定义需要的组件
 8  *  3.创建组件
 9  *  4.设置布局管理器
10  *  5.添加组件
11  *  6.对窗体设置
12  *  7.显示窗体
13  */
14 
15 package com.beekc.www;
16 import java.awt.*;
17 import javax.swing.*;
18 
19 //继承JFrame
20 public class Beekc extends JFrame{
21 
22     //定义组件
23     JPanel jPanel1,jPanel2;
24     JLabel jlb1,jlb2;
25     JComboBox jcb;
26     JList jl;
27     JScrollPane jsp;
28 
29     public static void main(String[] args){
30         Beekc beekc = new Beekc();
31     }
32 
33     //构造函数
34     public Beekc(){
35         //创建组件
36         jPanel1 = new JPanel();
37         jPanel2 = new JPanel();
38         jlb1 = new JLabel("籍贯");
39         jlb2 = new JLabel("旅游地方");
40 
41         String[] strJComboBox = {"北京","天津","广东"};
42         jcb = new JComboBox(strJComboBox);
43 
44         String[] strJLIst = {"故宫","天安门","长城","颐和园"};
45         jl = new JList(strJLIst);
46         //设置显示多少个
47         jl.setVisibleRowCount(3);
48         jsp = new JScrollPane(jl);
49 
50         //设置布局管理器
51         //添加到JPanel
52         jPanel1.add(jlb1);
53         jPanel1.add(jcb);
54         jPanel2.add(jlb2);
55         jPanel2.add(jsp);
56 
57         this.setLayout(new GridLayout(1,2));
58 
59         //添加组件
60         this.add(jPanel1);
61         this.add(jPanel2);
62 
63         //对窗体设置
64         this.setTitle("用户调查");
65         this.setSize(270,110);
66         this.setResizable(false);
67         this.setLocation(200,200);
68         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
69 
70         //显示
71         this.setVisible(true);
72     }
73 
74 }

翻译软件界面

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月06日
 4  * 内容:gui窗体
 5  * 步骤:
 6  *  1.继承JFrame容器
 7  *  2.定义需要的组件
 8  *  3.创建组件
 9  *  4.设置布局管理器
10  *  5.添加组件
11  *  6.对窗体设置
12  *  7.显示窗体
13  */
14 
15 package com.beekc.www;
16 import java.awt.*;
17 import javax.swing.*;
18 
19 //继承JFrame
20 public class Beekc extends JFrame{
21 
22     //定义组件
23     JSplitPane jsp;
24     JList jList;
25     JLabel jl1;
26 
27     public static void main(String[] args){
28         Beekc beekc = new Beekc();
29     }
30 
31     //构造函数
32     public Beekc(){
33         //创建组件
34         String[] words = {"bird","boy","girl"};
35         jList =new JList(words);
36         //new ImageIcon,存放图片
37         jl1 = new JLabel(new ImageIcon("images/jinshanciba.png"));
38         //拆分窗体
39         jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jList,jl1);
40         //可以收缩
41         jsp.setOneTouchExpandable(true);
42 
43         //设置布局管理器
44 
45         //添加组件
46         this.add(jsp);
47 
48         //窗体设置
49         this.setTitle("谷歌翻译");
50         this.setSize(760,480);
51         this.setLocation(200,200);
52         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
53 
54         //显示
55         this.setVisible(true);
56     }
57 }

聊天界面

 1 /*
 2  * 作者:白客C
 3  * 时间:2020年03月06日
 4  * 内容:gui窗体
 5  * 步骤:
 6  *  1.继承JFrame容器
 7  *  2.定义需要的组件
 8  *  3.创建组件
 9  *  4.设置布局管理器
10  *  5.添加组件
11  *  6.对窗体设置
12  *  7.显示窗体
13  */
14 
15 package com.beekc.www;
16 import java.awt.*;
17 import javax.swing.*;
18 
19 //继承JFrame
20 public class Beekc extends JFrame{
21 
22     //定义组件
23     JTextArea jTextArea;
24     JScrollPane jScrollPane;
25     JPanel jPanel;
26     JTextField jTextField;
27     JButton jButton;
28 
29     public static void main(String[] args){
30         Beekc beekc = new Beekc();
31     }
32 
33     //构造函数
34     public Beekc(){
35         //创建组件
36         jTextArea = new JTextArea();
37         jScrollPane = new JScrollPane(jTextArea);
38 
39         jPanel = new JPanel();
40         jTextField = new JTextField(20);
41         jButton = new JButton("发送");
42 
43         //设置布局管理器
44         //添加到JPanel
45         jPanel.add(jTextField);
46         jPanel.add(jButton);
47 
48         //添加组件
49         this.add(jScrollPane);
50         this.add(jPanel,BorderLayout.SOUTH);
51 
52         //窗体设置
53         this.setTitle("聊天界面");
54         this.setIconImage((new ImageIcon("images\\qq.png")).getImage());
55         this.setSize(320,270);
56         this.setLocation(200,200);
57         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58 
59         //显示
60         this.setVisible(true);
61     }
62 
63 }

聊天登录界面

猜你喜欢

转载自www.cnblogs.com/beekc/p/12423161.html
今日推荐