张世豪
4 小时以前 100f4dcea20a32663a07e91525de111f7515eb79
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package xitongshezhi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
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 CardPickupDialog extends JDialog {
    private JPasswordField passwordField;
    private boolean pickupSuccess = false;
    private int slotId;
    private CardMachineUI.SlotStatus slotStatus;
    private CardMachineUI parentUI;
    
    public CardPickupDialog(Frame parent, int slotId, CardMachineUI.SlotStatus status) {
        super(parent, "", true);
        this.slotId = slotId;
        this.slotStatus = status;
        this.parentUI = (CardMachineUI) parent;
        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);
 
        // 创建标题区域
        JPanel headerPanel = createHeaderPanel();
        
        // 创建输入区域面板
        JPanel inputPanel = createInputPanel();
        
        // 创建按钮面板
        JPanel buttonPanel = createButtonPanel();
 
        // 组装主面板
        mainPanel.add(headerPanel, 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());
        
        // 回车键确认
        passwordField.addActionListener(e -> handlePickup());
    }
    
    /**
     * 创建标题区域面板
     */
    private JPanel createHeaderPanel() {
        JPanel headerPanel = new JPanel(new BorderLayout(10, 10));
        headerPanel.setBackground(new Color(15, 28, 48));
        headerPanel.setOpaque(true);
        headerPanel.setBorder(new EmptyBorder(0, 0, 15, 0));
 
        // 状态图标
        JLabel statusIcon = new JLabel();
        statusIcon.setHorizontalAlignment(SwingConstants.CENTER);
        statusIcon.setPreferredSize(new Dimension(48, 48));
 
        // 根据状态设置不同的图标和颜色
        Color statusColor;
        switch (slotStatus) {
            case CHARGING:
                statusIcon.setText("⚡");
                statusColor = new Color(231, 76, 60);
                break;
            case FULL:
                statusIcon.setText("🔋");
                statusColor = new Color(46, 204, 113);
                break;
            case CHARGE_FAULT:
                statusIcon.setText("⚠️");
                statusColor = new Color(243, 156, 18);
                break;
            case COMM_FAULT:
                statusIcon.setText("📡");
                statusColor = new Color(155, 89, 182);
                break;
            default:
                statusIcon.setText("💳");
                statusColor = new Color(93, 109, 126);
        }
 
        statusIcon.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 32));
        statusIcon.setForeground(statusColor);
 
        // 标题和状态信息
        JLabel titleLabel = new JLabel("取卡操作", SwingConstants.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
        titleLabel.setForeground(new Color(52, 152, 219));
 
        JLabel infoLabel = new JLabel("卡槽 " + slotId + " • " + slotStatus.getDisplayName(), SwingConstants.CENTER);
        infoLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        infoLabel.setForeground(new Color(160, 200, 255));
 
        JPanel textPanel = new JPanel(new GridLayout(2, 1, 0, 5));
        textPanel.setBackground(new Color(15, 28, 48));
        textPanel.add(titleLabel);
        textPanel.add(infoLabel);
 
        headerPanel.add(statusIcon, BorderLayout.WEST);
        headerPanel.add(textPanel, BorderLayout.CENTER);
 
        return headerPanel;
    }
    
    /**
     * 创建输入区域面板
     */
    private JPanel createInputPanel() {
        JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
        inputPanel.setBackground(new Color(15, 28, 48));
        inputPanel.setOpaque(true);
        inputPanel.setBorder(new EmptyBorder(15, 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)
                ));
            }
        });
 
        // 提示信息
        JLabel hintLabel = new JLabel("请输入取卡密码进行身份验证", SwingConstants.CENTER);
        hintLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
        hintLabel.setForeground(new Color(149, 165, 166));
        hintLabel.setBorder(new EmptyBorder(8, 0, 0, 0));
 
        // 组装输入区域
        JPanel fieldPanel = new JPanel(new BorderLayout());
        fieldPanel.setBackground(new Color(15, 28, 48));
        fieldPanel.add(passwordLabel, BorderLayout.NORTH);
        fieldPanel.add(passwordField, BorderLayout.CENTER);
        fieldPanel.add(hintLabel, BorderLayout.SOUTH);
 
        inputPanel.add(fieldPanel, BorderLayout.CENTER);
 
        return inputPanel;
    }
    
    /**
     * 创建按钮面板
     */
    private JPanel createButtonPanel() {
        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));
 
        // 按钮事件处理
        confirmButton.addActionListener(e -> handlePickup());
        cancelButton.addActionListener(e -> dispose());
 
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
 
        return buttonPanel;
    }
    
    /**
     * 处理取卡操作
     */
    private void handlePickup() {
        String password = new String(passwordField.getPassword());
        
        // 验证取卡密码
        if (parentUI.validatePickupPassword(password)) {
            // 密码正确,执行取卡操作
            if (parentUI.performCardPickup(slotId)) {
                pickupSuccess = true;
                
                // 更新卡槽状态为未使用
                parentUI.getSlotManager().setSlotHasCard(slotId, "0");
                
                // 通知父窗口更新显示
                parentUI.updateCardSlotsDisplay();
                parentUI.updateStatistics();
                
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, 
                        "取卡操作失败,请检查设备连接后重试", 
                        "操作失败", 
                        JOptionPane.ERROR_MESSAGE);
            }
        } 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 pickupSuccess;
    }
    
    /**
     * 静态方法:显示取卡操作对话框
     */
    public static boolean showCardPickup(Frame parent, int slotId, CardMachineUI.SlotStatus status) {
        CardPickupDialog dialog = new CardPickupDialog(parent, slotId, status);
        return dialog.showDialog();
    }
}