package xitongshezhi; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.FlowLayout; // 添加这行导入 import java.awt.Font; import java.awt.Frame; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import home.CardMachineUI; /** * 管理员登录对话框 * 与主页面风格保持一致的不透明设计 */ @SuppressWarnings("serial") public class AdminLoginDialog extends JDialog { private JPasswordField passwordField; private boolean loginSuccess = false; public AdminLoginDialog(Frame parent) { super(parent, "", true); initializeUI(); } private void initializeUI() { // 设置对话框属性 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setResizable(false); // 创建主面板 - 使用不透明背景 JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); mainPanel.setBackground(new Color(15, 28, 48)); // 与主页面相同的深蓝色背景 mainPanel.setBorder(new EmptyBorder(25, 30, 25, 30)); mainPanel.setOpaque(true); // 创建标题区域 JLabel titleLabel = new JLabel("管理员身份验证"); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20)); titleLabel.setForeground(new Color(52, 152, 219)); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); titleLabel.setBorder(new EmptyBorder(0, 0, 15, 0)); // 创建输入区域面板 JPanel inputPanel = new JPanel(new BorderLayout(10, 10)); inputPanel.setBackground(new Color(15, 28, 48)); inputPanel.setOpaque(true); inputPanel.setBorder(new EmptyBorder(10, 0, 10, 0)); // 创建标签 JLabel passwordLabel = new JLabel("请输入管理员密码:"); passwordLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); passwordLabel.setForeground(Color.WHITE); passwordLabel.setHorizontalAlignment(SwingConstants.LEFT); // 创建密码输入框 - 设置为不透明 passwordField = new JPasswordField(18); passwordField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); passwordField.setBackground(Color.WHITE); // 白色不透明背景 passwordField.setForeground(Color.BLACK); // 黑色文字 passwordField.setOpaque(true); // 确保不透明 passwordField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219), 2), BorderFactory.createEmptyBorder(8, 12, 8, 12) )); passwordField.setCaretColor(new Color(52, 152, 219)); // 添加输入框焦点效果 passwordField.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { passwordField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(46, 204, 113), 2), BorderFactory.createEmptyBorder(8, 12, 8, 12) )); } public void focusLost(java.awt.event.FocusEvent evt) { passwordField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219), 2), BorderFactory.createEmptyBorder(8, 12, 8, 12) )); } }); // 组装输入区域 inputPanel.add(passwordLabel, BorderLayout.NORTH); inputPanel.add(passwordField, BorderLayout.CENTER); // 创建按钮面板 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0)); buttonPanel.setBackground(new Color(15, 28, 48)); buttonPanel.setOpaque(true); buttonPanel.setBorder(new EmptyBorder(15, 0, 0, 0)); // 创建样式化按钮 JButton confirmButton = createStyledButton("确认", new Color(46, 204, 113)); JButton cancelButton = createStyledButton("取消", new Color(231, 76, 60)); buttonPanel.add(confirmButton); buttonPanel.add(cancelButton); // 组装主面板 mainPanel.add(titleLabel, BorderLayout.NORTH); mainPanel.add(inputPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); // 设置对话框内容 getContentPane().setBackground(new Color(15, 28, 48)); getContentPane().add(mainPanel); pack(); setLocationRelativeTo(getParent()); // 按钮事件处理 confirmButton.addActionListener(e -> handleLogin()); cancelButton.addActionListener(e -> dispose()); // 回车键确认 passwordField.addActionListener(e -> handleLogin()); } /** * 处理登录验证 */ private void handleLogin() { String password = new String(passwordField.getPassword()); // 通过父窗口验证密码 if (getParent() instanceof CardMachineUI) { CardMachineUI parentUI = (CardMachineUI) getParent(); if (parentUI.validateAdminPassword(password)) { loginSuccess = true; dispose(); } else { JOptionPane.showMessageDialog(this, "密码错误,请重新输入!", "验证失败", JOptionPane.WARNING_MESSAGE); passwordField.setText(""); passwordField.requestFocus(); } } } /** * 创建样式化按钮 */ 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(brighterColor(color)); } public void mouseExited(java.awt.event.MouseEvent evt) { button.setBackground(color); } }); return button; } private Color brighterColor(Color color) { int r = Math.min(255, color.getRed() + 30); int g = Math.min(255, color.getGreen() + 30); int b = Math.min(255, color.getBlue() + 30); return new Color(r, g, b); } /** * 显示对话框并返回登录结果 */ public boolean showDialog() { setVisible(true); return loginSuccess; } /** * 静态方法:显示管理员登录对话框 */ public static boolean showAdminLogin(Frame parent) { AdminLoginDialog dialog = new AdminLoginDialog(parent); return dialog.showDialog(); } }