张世豪
23 小时以前 03b0fb0ba2de86bcfff277778826547c0e37a93f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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();
    }
}