Java--Swing-based login interface

operation result:

code:

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

public class ljt2 {
    public static void main(String[] args) {
        // 创建窗体
        JFrame frame = new JFrame("学生信息管理系统");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(4, 2));

        // 创建用户名、密码输入框和角色选择按钮组
        JTextField usernameField = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        ButtonGroup roleGroup = new ButtonGroup();
        JRadioButton adminRadioButton = new JRadioButton("管理员");
        JRadioButton studentRadioButton = new JRadioButton("学生");

        // 将单选按钮添加到按钮组中
        roleGroup.add(adminRadioButton);
        roleGroup.add(studentRadioButton);

        // 创建登录和注册按钮
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");

        // 将组件添加到窗体中
        frame.add(new JLabel("       用户名:"));
        frame.add(usernameField);
        frame.add(new JLabel("       密码:"));
        frame.add(passwordField);
        frame.add(adminRadioButton);
        frame.add(studentRadioButton);
        frame.add(loginButton);
        frame.add(registerButton);

        // 设置登录按钮点击事件的处理逻辑
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                boolean isAdmin = adminRadioButton.isSelected();
                boolean isStudent = studentRadioButton.isSelected();

                // 如果用户名和密码为空,则提示并返回
                if (username.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "请输入用户名和密码", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 如果角色为空,则提示并返回
                if (!isAdmin && !isStudent) {
                    JOptionPane.showMessageDialog(frame, "请选择一种角色", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                // 执行管理员登录逻辑
                if (isAdmin) {

                    JOptionPane.showMessageDialog(frame, "管理员登录");
                }
                // 执行学生登录逻辑
                else if (isStudent) {
                    JOptionPane.showMessageDialog(frame, "学生登录");
                }

                // 其他操作...
            }
        // 设置注册按钮点击事件的处理逻辑

        });

        // 显示窗体
        frame.setVisible(true);
    }
}

 Summarize:

This is a simple Java code for a student information management system. It creates a form with username input box, password input box, role selection radio button group, login button and register button. When the user clicks the login button, the corresponding login logic is executed according to the selected role. If the user name or password is empty, the user is prompted for input; if no role is selected, the user is also prompted to select a role. The processing logic of administrator login and student login can be added in the corresponding conditional branch.

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/131524692