package com.mathlearning.view; import com.mathlearning.controller.AuthController; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame { private AuthController authController; private JTextField emailField; private JPasswordField passwordField; public LoginFrame() { this.authController = new AuthController(); initializeUI(); } private void initializeUI() { setTitle("数学学习软件 - 登录"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 400); setLocationRelativeTo(null); setResizable(false); // 使用更灵活的布局 JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40)); // 标题 JLabel titleLabel = new JLabel("数学学习软件", JLabel.CENTER); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 28)); titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 30, 0)); mainPanel.add(titleLabel); // 添加一些间距 mainPanel.add(Box.createRigidArea(new Dimension(0, 20))); // 邮箱输入区域 JPanel emailPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); emailPanel.setMaximumSize(new Dimension(400, 60)); JLabel emailLabel = new JLabel("邮箱:"); emailLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16)); emailLabel.setPreferredSize(new Dimension(80, 30)); emailField = new JTextField(); emailField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); emailField.setPreferredSize(new Dimension(250, 35)); emailPanel.add(emailLabel); emailPanel.add(emailField); mainPanel.add(emailPanel); // 添加间距 mainPanel.add(Box.createRigidArea(new Dimension(0, 15))); // 密码输入区域 JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); passwordPanel.setMaximumSize(new Dimension(400, 60)); JLabel passwordLabel = new JLabel("密码:"); passwordLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16)); passwordLabel.setPreferredSize(new Dimension(80, 30)); passwordField = new JPasswordField(); passwordField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); passwordField.setPreferredSize(new Dimension(250, 35)); passwordPanel.add(passwordLabel); passwordPanel.add(passwordField); mainPanel.add(passwordPanel); // 添加间距 mainPanel.add(Box.createRigidArea(new Dimension(0, 30))); // 按钮区域 - 只有登录和注册按钮 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0)); buttonPanel.setMaximumSize(new Dimension(400, 50)); JButton loginButton = createStyledButton("登录", new Color(70, 130, 180)); JButton registerButton = createStyledButton("注册", new Color(60, 179, 113)); buttonPanel.add(loginButton); buttonPanel.add(registerButton); mainPanel.add(buttonPanel); // 添加底部间距 mainPanel.add(Box.createVerticalGlue()); // 添加操作监听器 loginButton.addActionListener(new LoginAction()); registerButton.addActionListener(e -> openRegisterFrame()); add(mainPanel); } private JButton createStyledButton(String text, Color color) { JButton button = new JButton(text); button.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); button.setBackground(color); button.setForeground(Color.WHITE); button.setFocusPainted(false); button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); button.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 添加鼠标悬停效果 button.addMouseListener( new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { button.setBackground(color.darker()); } public void mouseExited(java.awt.event.MouseEvent evt) { button.setBackground(color); } }); return button; } private class LoginAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String email = emailField.getText().trim(); String password = new String(passwordField.getPassword()); if (email.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(LoginFrame.this, "请输入邮箱和密码", "错误", JOptionPane.ERROR_MESSAGE); return; } if (authController.login(email, password)) { JOptionPane.showMessageDialog( LoginFrame.this, "登录成功!", "成功", JOptionPane.INFORMATION_MESSAGE); openLevelSelectionFrame(); } else { JOptionPane.showMessageDialog(LoginFrame.this, "邮箱或密码错误", "错误", JOptionPane.ERROR_MESSAGE); } } } private void openRegisterFrame() { RegisterFrame registerFrame = new RegisterFrame(); registerFrame.setVisible(true); this.dispose(); } private void openLevelSelectionFrame() { LevelSelectionFrame levelFrame = new LevelSelectionFrame(authController); levelFrame.setVisible(true); this.dispose(); } }