Java -- Swing ベースのログイン インターフェイス

操作結果:

コード:

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);
    }
}

 要約:

これは、学生情報管理システム用の単純な Java コードです。ユーザー名入力ボックス、パスワード入力ボックス、役割選択ラジオボタングループ、ログインボタン、登録ボタンを備えたフォームを作成します。ユーザーがログイン ボタンをクリックすると、選択したロールに従って、対応するログイン ロジックが実行されます。ユーザー名またはパスワードが空の場合、ユーザーは入力を求められます。役割が選択されていない場合は、役割を選択するよう求められます。管理者ログインと学生ログインの処理ロジックを、対応する条件分岐に追加できます。

おすすめ

転載: blog.csdn.net/m0_74293254/article/details/131524692