LOADING

正在加载喵~

week82

2025/4/19 未来与梦 未标记

java import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginInterface extends JFrame { private JTextField…

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

public class LoginInterface extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginInterface() {
        // 设置窗口标题
        setTitle("登录界面");
        // 设置窗口大小
        setSize(300, 200);
        // 设置窗口关闭时的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口布局
        setLayout(new GridLayout(3, 2));

        // 用户名标签
        JLabel usernameLabel = new JLabel("用户名:");
        add(usernameLabel);

        // 用户名输入框
        usernameField = new JTextField();
        add(usernameField);

        // 密码标签
        JLabel passwordLabel = new JLabel("密码:");
        add(passwordLabel);

        // 密码输入框
        passwordField = new JPasswordField();
        add(passwordField);

        // 登录按钮
        JButton loginButton = new JButton("登录");
        add(loginButton);

        // 为登录按钮添加点击事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                // 这里简单假设用户名是 "admin",密码是 "123456"
                if ("admin".equals(username) && "123456".equals(password)) {
                    JOptionPane.showMessageDialog(null, "登录成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    public static void main(String[] args) {
        // 在事件调度线程中创建和显示 GUI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                LoginInterface loginFrame = new LoginInterface();
                // 使窗口可见
                loginFrame.setVisible(true);
            }
        });
    }
}