GUI

Graphical User Interface
图形用户接口:采用图形的方式,进行操作页面的展示

  • AWT
    Abstract Window Toolkit

    窗体 -> Frame
    面板 -> Panel
    按钮 -> Button
    标签 -> Label
    文本域 -> TextField
    多行文本 -> TextArea

  • Swing

    窗体 -> JFrame
    面板 -> JPanel
    按钮 -> JButton
    标签 -> JLabel
    文本域 -> JTextField
    多行文本 -> JTextArea

事件

ActionListener 动作/响应事件
KeyListener 键盘事件
MouseListener 鼠标事件

布局管理

边界式 BorderLayout(JFrame)
流式 FlowLayout(JPanel)
网格式 GridLayout 自定义式(null)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public class UserService {
public String Login(String account, String password){
if(account.equals("admin") && password.equals("123456")){
return "登录成功";
}
return "用户名或密码不正确";
}
}


import service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestGui {

public static void main(String[] args){
//创建窗体
JFrame jf = new JFrame("登录窗口");
//jf.setTitle("设置窗体标题的一种方式");
//设置窗体出现位置,宽高
jf.setBounds(200,300, 480,360);
//创建一个面板
JPanel panel = new JPanel();

//创建 title 标题
JLabel title = new JLabel("在 线 考 试 系 统");
//创建账户标签
JLabel userLabel = new JLabel("账 号:");
//创建密码标签
JLabel passLabel = new JLabel("密 码:");
//创建账户输入框
JTextField userText = new JTextField();
//创建密码输入框
JPasswordField passText = new JPasswordField();

//创建复选框
JCheckBox jcBox = new JCheckBox("记住密码");

//添加按钮
JButton logIn = new JButton("登 录");
JButton logOut = new JButton("退 出");

//布局
/**
* 设置panel布局管理为自定义
*/
panel.setLayout(null);
title.setBounds(100,10,300,50);
title.setFont(new Font("黑体",Font.BOLD,26));

userLabel.setBounds(60,80,100,30);
userLabel.setFont(new Font("黑体",Font.BOLD,18));

passLabel.setBounds(60,130,100,30);
passLabel.setFont(new Font("黑体",Font.BOLD,18));

userText.setBounds(135,80,220,30);
userText.setFont(new Font("黑体",Font.BOLD,18));

passText.setBounds(135,130,220,30);
passText.setFont(new Font("黑体",Font.BOLD,18));

jcBox.setBounds(130,180,120,20);

logIn.setBounds(135,220,80,30);
logIn.setFont(new Font("黑体",Font.BOLD,16));

logOut.setBounds(255,220,80,30);
logOut.setFont(new Font("黑体",Font.BOLD,16));

panel.add(title);
panel.add(userLabel);
panel.add(userText);
panel.add(passLabel);
panel.add(passText);
panel.add(jcBox);
panel.add(logIn);
panel.add(logOut);
jf.add(panel);

//添加事件
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account = userText.getText();
String password = new String(passText.getPassword());
UserService uservice = new UserService();
String res = uservice.Login(account,password);

if(res.equals("登陆成功")){
System.out.println(res);
}else{
JOptionPane.showMessageDialog(jf,res);
userText.setText("");
passText.setText("");
}
}
};
logIn.addActionListener(listener);

//设置窗体状态,显示
jf.setVisible(true);

//设计点击关闭按钮,关闭窗口
jf.setDefaultCloseOperation(3);
}
}

菜单

/*JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("文件");
JMenuItem newItem = new JMenuItem("新建");
menu.add(newItem);
bar.add(menu);
jf.setJMenuBar(bar);*/

示例代码

  • BaseFrame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package frames;

import javax.swing.*;

public abstract class BaseFrame extends JFrame {
//模板模式
protected abstract void setComponentConfig();
protected abstract void addElements();
protected abstract void addListener();
protected abstract void setFrameSelf();

public BaseFrame(){};

public BaseFrame(String title){
super(title);
}

protected void init(){
setComponentConfig();
addElements();
addListener();
setFrameSelf();
}
}
  • UserService
1
2
3
4
5
6
7
8
9
10
package service;

public class UserService {
public String Login(String account, String password){
if(account.equals("admin") && password.equals("123456")){
return "登录成功";
}
return "用户名或密码不正确";
}
}
  • LoginFrame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package frames;

import service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends BaseFrame {
//布局管理
private JPanel panel = new JPanel();
//标题
private JLabel title = new JLabel("在 线 考 试 系 统");
//账户标签
private JLabel userLabel = new JLabel("账 号:");
//密码标签
private JLabel passLabel = new JLabel("密 码:");
//账户输入框
private JTextField userText = new JTextField();
//密码输入框
private JPasswordField passText = new JPasswordField();
//复选框
private JCheckBox jcBox = new JCheckBox("记住密码");
//按钮
private JButton logIn = new JButton("登 录");
private JButton logOut = new JButton("退 出");

//设置组件位置
protected void setComponentConfig(){
/**
* 设置panel布局管理为自定义
*/
panel.setLayout(null);
title.setBounds(100,10,300,50);
title.setFont(new Font("黑体",Font.BOLD,26));

userLabel.setBounds(60,80,100,30);
userLabel.setFont(new Font("黑体",Font.BOLD,18));

passLabel.setBounds(60,130,100,30);
passLabel.setFont(new Font("黑体",Font.BOLD,18));

userText.setBounds(135,80,220,30);
userText.setFont(new Font("黑体",Font.BOLD,18));

passText.setBounds(135,130,220,30);
passText.setFont(new Font("黑体",Font.BOLD,18));

jcBox.setBounds(130,180,120,20);

logIn.setBounds(135,220,80,30);
logIn.setFont(new Font("黑体",Font.BOLD,16));

logOut.setBounds(255,220,80,30);
logOut.setFont(new Font("黑体",Font.BOLD,16));
}
//窗体添加元素
protected void addElements(){
panel.add(title);
panel.add(userLabel);
panel.add(userText);
panel.add(passLabel);
panel.add(passText);
panel.add(jcBox);
panel.add(logIn);
panel.add(logOut);
this.add(panel);
}
//添加事件
protected void addListener(){
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account = userText.getText();
String password = new String(passText.getPassword());
UserService uservice = new UserService();
String res = uservice.Login(account,password);

if(res.equals("登陆成功")){
//new ExaminaSystem();
}else{
JOptionPane.showMessageDialog(LoginFrame.this,res);
userText.setText("");
passText.setText("");
}
}
};
logIn.addActionListener(listener);
}
protected void setFrameSelf(){
this.setBounds(200,300, 480,360);
//设置窗体状态,显示
this.setVisible(true);
//点击关闭按钮,关闭窗口
this.setDefaultCloseOperation(3);
}

public LoginFrame(){
init();
}
public LoginFrame(String title){
super(title);
init();
}
}
  • UserService
1
2
3
4
5
6
7
8
9
10
package service;

public class UserService {
public String Login(String account, String password){
if(account.equals("admin") && password.equals("123456")){
return "登录成功";
}
return "用户名或密码不正确";
}
}