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(); } }