Written in front of the words:
- Reference material: Java implements login and registration interface【Click here for details】
- Reference materials: Java Swing Graphical Interface Development【Click here for details】
- In this chapter: The Java Swing Login Screen
- IDE:IntelliJ IDEA 2021.2.1
- JDK:Java8
Table of contents
3. Relevant knowledge understanding
3.3 Set the position of the text in the label
1. Project display
Project display:
2. Project structure
Project structure:
3. Relevant knowledge understanding
3.1 Create a window
MyFrame.java role: create a window
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);
}
}
Effect screenshot:
3.2 Set font color
setForeground(new Color(0xFF0000));
Parameter Description:
- 0xff0000 means red, fill in the hexadecimal representation of the color here [online color picker]
- Color can also be a parameter or you can fill in a variety of
The basic usage of the above Color class: new Color(255,255,255,255);//Set color and transparency
for example:
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);
}
}
Screenshot of the result:
3.2 Set font size
setFont(new Font("黑体", Font.PLAIN,20));
Parameter Description:
- "黑体" indicates the font name
- Font.PLAIN means normal font (in addition, there are bold BOLD and italic ITALIC )
- 20 means font size
Font style (normal, bold, italic)
for example:
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);
}
}
Effect screenshot:
3.3 Set the position of the text in the label
setBounds(140,60,300,300);
Parameter Description:
- 140 represents the coordinates on the horizontal axis
- 60 represents the coordinates on the vertical axis
- 300 means width
- 300 means height
Screen coordinates (important!)
for example:
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);
}
}
Effect screenshot: (displayed in the center)
4. Actual login interface
Login.java login interface
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);
}
}
StudentManage.java student management system empty frame
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);
}
}
Demo part:
If the password is wrong, a prompt will be displayed.
After successfully logging in with the user name and password, enter the "Student Management System".