JAVA GUI图形设计

今天教大家一个简单的GUI图形设计,我们来做一个登录窗口

首先使用IDEA创建一个项目

 src目录下创建窗口运行文件Login.java

1
2
3
4
5
public class Login {
    public static void main(String[] args) {
        new LoginWin();
    }
}

接着新建窗口类LoginWin();

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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
public class LoginWin extends JFrame{
    private static final long serialVersionUID = -1314520L;
    public LoginWin(){
        setTitle("系统登录");   //窗口标题
        setSize(400,300);   //窗口大小
        setLocation(660,320);   //窗口位置
        setResizable(false);    //窗口大小不可改变
        setLocationRelativeTo(null);    //窗口居中
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);            //设置关闭方式
 
        Container container = getContentPane();                             //添加一个容器
        container.setLayout(null);                                          //设置布局管理器为null
        JLabel username = new JLabel("用户名:");                           //生成 name 标签
        JLabel pw = new JLabel("密   码:");                                   //生成 password 标签
        JTextField name = new JTextField(30);                               //name 输入框
        JPasswordField password = new JPasswordField(30);                   //password 输入框
        JButton login = new JButton("登陆");                                  //登陆 按钮
        JButton reset = new JButton("重置");                                  //重置 按钮
 
        /*
         * 设置模块的位置和大小
         */
        username.setBounds(60, 50, 120, 30);
        name.setBounds(120, 50, 180, 30);
        pw.setBounds(60, 100, 120, 30);
        password.setBounds(120, 100, 180, 30);
        login.setBounds(100, 180, 70, 30);
        reset.setBounds(220, 180, 70, 30);
 
        container.add(username);
        container.add(name);
        container.add(pw);
        container.add(password);
        container.add(login);
        container.add(reset);
 
        /*
         * 给 登陆 按钮添加 动作事件
         */
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(name.getText().trim().length() == 0 || new String(password.getPassword()).trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "用户名,密码不能为空");              //显示对话框
                    return;
                }
                if(name.getText().equals("admin") && new String(password.getPassword()).equals("lvhongming")) {
                    JOptionPane.showMessageDialog(null, "登陆成功");
                    return;
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码不正确");
                    return;
                }
            }
        });
        /*
         * 给 重置 按钮添加动作事件
         */
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                name.setText("");
                password.setText("");
            }
        });
        //输出窗口
        this.setVisible(true);
    }
 
}

运行测试:

 

 

posted @   智昕  阅读(128)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示