当前位置:   article > 正文

Java登录界面_登录界面java代码

登录界面java代码

1.项目展示
项目展示:

2.项目结构
项目结构:

3.相关知识了解
3.1建立一个窗口
MyFrame.java 作用:建立一个窗口

import javax.swing.*;

public class MyFrame {

public static void main(String[] args) {

    //1.创建一个主要框架
    JFrame jFrame = new JFrame("测试");

    //2.设置窗口大小
    jFrame.setSize(500,500);

    //3.启动窗口关闭功能
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //4.设置窗口是否禁止放大功能
    /*
        true  -> 窗口可以进行缩放
        false -> 窗口不可以进行放大
     */
    jFrame.setResizable(false);

    //5.设置窗口初始位置为屏幕中央
    jFrame.setLocationRelativeTo(null);

    //6.设置窗口可见
    jFrame.setVisible(true);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

}

效果截图:

3.2 设置字体颜色
setForeground(new Color(0xFF0000));
参数说明:

0xff0000 表示红色,在这里填入表示颜色的十六进制[在线颜色选择器]
Color也可以参数也可以填多种

上述Color类的基本用法: new Color(255,255,255,255);//设置颜色以及透明度

举例子:

import javax.swing.;
import java.awt.
;

public class MyFrame {

public static void main(String[] args) {

    JFrame jFrame = new JFrame("测试");
    jFrame.setSize(500,500);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setResizable(false);
    jFrame.setLocationRelativeTo(null);

    /*
        创建一个标签
     */
    //1.创建一个标签,并给标签指定参数"测试文本"
    JLabel jLabel = new JLabel("测试文本");
    //2.设置标签的字体颜色,如:红色
    jLabel.setForeground(new Color(0xff0000));
    //3.将标签添加进框架中
    jFrame.add(jLabel);

    jFrame.setVisible(true);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

}

结果截图:

3.2 设置字体大小
setFont(new Font(“黑体”, Font.PLAIN,20));
参数说明:

”黑体“ 表示字体名称
Font.PLAIN 表示正常字体(除此之外,还有粗体BOLD和斜体ITALIC)
20 表示字体大小
字体样式(正常、粗体、斜体)

举例子:

import javax.swing.;
import java.awt.
;

public class Frame01 {

public static void main(String[] args) {

    JFrame jFrame = new JFrame("测试");
    jFrame.setSize(500,500);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setResizable(false);
    jFrame.setLocationRelativeTo(null);

    /*
        创建一个标签
     */
    //1.创建一个标签,并给标签指定参数"测试文本"
    JLabel jLabel = new JLabel("测试文本");
    //2.设置标签的字体颜色,如:红色
    jLabel.setForeground(new Color(0xff0000));
    //3.设置标签的字体大小
    jLabel.setFont(new Font("黑体", Font.PLAIN,50));
    //4.将标签添加进框架中
    jFrame.add(jLabel);

    jFrame.setVisible(true);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

}

效果截图:

3.3 设置文本在标签的位置
setBounds(140,60,300,300);
参数说明:

140 表示横轴上的坐标
60 表示纵轴上的坐标
300 表示宽度
300 表示高度
屏幕坐标(重点!)

举例子:

import javax.swing.;
import java.awt.
;

public class Frame01 {

public static void main(String[] args) {

    JFrame jFrame = new JFrame("测试");
    jFrame.setSize(500,500);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setResizable(false);
    jFrame.setLocationRelativeTo(null);

    //不使用布局管理器,采取绝对定位
    jFrame.setLayout(null);

    /*
        创建一个标签
     */
    //1.创建一个标签,并给标签指定参数"测试文本"
    JLabel jLabel = new JLabel("测试文本");
    //2.设置标签的字体颜色,如:红色
    jLabel.setForeground(new Color(0xff0000));
    //3.设置标签的字体大小
    jLabel.setFont(new Font("黑体", Font.PLAIN,50));
    //4.设置文本在标签的位置
    jLabel.setBounds(140,60,300,300);
    //5.将标签添加进框架中
    jFrame.add(jLabel);

    jFrame.setVisible(true);
}
  • 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

}

效果截图:(显示在中央)

4.实战登录界面
Login.java 登录界面

package login;

import student_manage.StudentManage;

import javax.swing.;
import java.awt.
;

public class Login{

public static void main(String[] args) {

    //创建一个主要框架,将其命名为”登录"
    JFrame jFrame = new JFrame("登录");

    //设置窗口大小
    jFrame.setSize(900,507);

    //先将布局管理器置为null
    jFrame.setLayout(null);

    //添加标签【学生管理系统】
    JLabel textStudentManage = new JLabel("学生管理系统");//创建一个标签,并命名为“学生管理系统“
    textStudentManage.setForeground(new Color(0x0010FF));//设置字体颜色
    textStudentManage.setFont(new Font("黑体", Font.PLAIN,50));//设置字体大小
    textStudentManage.setBounds(280,50,800,100);//设置标签的绝对位置
    jFrame.add(textStudentManage);//向框架中添加组件【标签(学生管理系统)】

    //添加标签【用户名】
    JLabel textUser = new JLabel("用户名:");
    textUser.setForeground(new Color(0xFF0000));
    textUser.setFont(new Font("黑体", Font.PLAIN,30));
    textUser.setBounds(200,140,200,100);
    jFrame.add(textUser);

    //添加输入框【用户名输入框】
    JTextField user = new JTextField(20);
    user.setFont(new Font("黑体", Font.PLAIN,18));
    user.setSelectedTextColor(new Color(0xFF0000));
    user.setBounds(330,170,280,40);
    jFrame.add(user);

    //添加标签【密码】
    JLabel textPassword = new JLabel("密码  :");
    textPassword.setForeground(new Color(0xFF0000));
    textPassword.setFont(new Font("黑体", Font.PLAIN,30));
    textPassword.setBounds(200,200,200,100);
    jFrame.add(textPassword);

    //添加密码输入框【密码】
    JPasswordField password = new JPasswordField(20);
    password.setBounds(330,230,280,40);
    jFrame.add(password);

    //添加按钮【登录】
    JButton jButton = new JButton("登录");
    jButton.setForeground(new Color(0x023BF6));
    jButton.setBackground(new Color(0x38FF00));
    jButton.setFont(new Font("黑体", Font.PLAIN,20));
    jButton.setBorderPainted(false);
    jButton.setBounds(300,330,100,50);
    jFrame.add(jButton);

    //添加按钮【注册】
    JButton register = new JButton("注册");
    register.setForeground(new Color(0x0029FF));
    register.setBackground(new Color(0xECA871));
    register.setFont(new Font("黑体", Font.PLAIN,20));
    register.setBorderPainted(false);
    register.setBounds(500,330,100,50);
    jFrame.add(register);

    //对按钮事件进行处理
    jButton.addActionListener((e -> {

        /*
            账号:admin
            密码:123456
         */

        //设定条件
        String pwd = new String(password.getPassword());
        if(user.getText().equals("admin")){
            if(pwd.equals("123456")){
                //密码账号正确,进入学生管理系统
                //进入学生管理系统
                jFrame.setVisible(false);//将登录界面设定为不可见
                new StudentManage().StudentMainInterface();
            }else{
                //密码不正确
                JOptionPane.showMessageDialog(jFrame,"密码错误","提示",JOptionPane.INFORMATION_MESSAGE);
                //将密码框置空
                password.setText("");
            }
        }else{
            //用户名错误
            JOptionPane.showMessageDialog(jFrame,"用户名错误","提示",JOptionPane.INFORMATION_MESSAGE);
            //将用户名和密码置空
            user.setText("");
            password.setText("");
        }
    }));

    //设置相对位置:屏幕中间
    jFrame.setLocationRelativeTo(null);

    //确保使用窗口关闭按钮,能够正常退出,结束进程!
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //禁止对窗口大小进行缩放处理
    jFrame.setResizable(false);

    //设置可见
    jFrame.setVisible(true);
}
  • 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

}

StudentManage.java 学生管理系统空框架

package student_manage;

/*
登录之后进入的页面【学生管理系统】
*/

import javax.swing.*;

public class StudentManage {

public void StudentMainInterface(){

    //创建一个窗口,并设置窗口名称为”登录”
    JFrame jFrame = new JFrame("学生管理系统");

    //设置窗口大小
    jFrame.setSize(1400,900);

    //设置相对位置:屏幕中间
    jFrame.setLocationRelativeTo(null);

    //确保使用窗口关闭按钮,能够正常退出,结束进程!
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    //禁止对窗口大小进行缩放处理
    jFrame.setResizable(false);

    //设置可见
    jFrame.setVisible(true);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

}

演示部分:

密码错误,会提示。

用户名和密码登录成功后,进入”学生管理系统“。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/298261
推荐阅读
相关标签
  

闽ICP备14008679号