| | |
| | | package xitongshezhi; |
| | | import javax.swing.*; |
| | | import java.awt.*; |
| | | import java.awt.event.*; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.io.*; |
| | | import java.net.*; |
| | | |
| | | public class banbenguanli extends JFrame { |
| | | private JPanel mainPanel; |
| | | private JLabel titleLabel; |
| | | private JButton closeButton; |
| | | |
| | | // 版本信息组件 |
| | | private JLabel currentVersionLabel; |
| | | private JLabel versionDateLabel; |
| | | private JLabel statusIconLabel; |
| | | private JLabel statusTitleLabel; |
| | | private JLabel statusMessageLabel; |
| | | private JPanel latestVersionPanel; |
| | | private JLabel latestVersionLabel; |
| | | |
| | | // 按钮组件 |
| | | private JButton checkUpdateButton; |
| | | private JButton updateButton; |
| | | |
| | | // 进度条组件 |
| | | private JProgressBar downloadProgressBar; |
| | | private JLabel progressPercentLabel; |
| | | |
| | | // 版本数据 |
| | | private String currentVersion = "V2.3.5"; |
| | | private String currentDate = "2023-10-15"; |
| | | private String latestVersion = "V2.4.0"; |
| | | private boolean updateAvailable = false; |
| | | |
| | | // 颜色定义 |
| | | private final Color BACKGROUND_COLOR = new Color(15, 28, 48); |
| | | private final Color CARD_COLOR = new Color(26, 43, 68); |
| | | private final Color PRIMARY_COLOR = new Color(52, 152, 219); |
| | | private final Color SECONDARY_COLOR = new Color(46, 204, 113); |
| | | private final Color WARNING_COLOR = new Color(243, 156, 18); |
| | | private final Color TEXT_COLOR = new Color(224, 224, 224); |
| | | private final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255); |
| | | @SuppressWarnings("serial") |
| | | public class banbenguanli extends JDialog { |
| | | private JPanel mainPanel; |
| | | private JButton closeButton; |
| | | |
| | | public banbenguanli() { |
| | | initializeUI(); |
| | | setupEventListeners(); |
| | | updateVersionInfo(); |
| | | } |
| | | // 版本信息组件 |
| | | private JLabel currentVersionLabel; |
| | | private JLabel versionDateLabel; |
| | | private JLabel statusTitleLabel; |
| | | private JLabel statusMessageLabel; |
| | | private JPanel latestVersionPanel; |
| | | private JLabel latestVersionLabel; |
| | | |
| | | private void initializeUI() { |
| | | // 设置窗口属性 - 7寸竖屏 (480x800) |
| | | setTitle("版本管理 - UWB人员定位卡发卡机管理系统"); |
| | | setSize(480, 800); |
| | | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| | | setLocationRelativeTo(null); |
| | | setResizable(false); |
| | | |
| | | // 设置不透明背景 |
| | | getContentPane().setBackground(BACKGROUND_COLOR); |
| | | |
| | | // 主面板 |
| | | mainPanel = new JPanel(); |
| | | mainPanel.setLayout(new BorderLayout()); |
| | | mainPanel.setBackground(BACKGROUND_COLOR); |
| | | mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
| | | |
| | | // 创建版本管理弹窗内容 |
| | | JPanel versionPanel = createVersionPanel(); |
| | | mainPanel.add(versionPanel, BorderLayout.CENTER); |
| | | |
| | | add(mainPanel); |
| | | } |
| | | |
| | | private JPanel createVersionPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(CARD_COLOR); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(PRIMARY_COLOR, 1), |
| | | BorderFactory.createEmptyBorder(25, 25, 25, 25) |
| | | )); |
| | | |
| | | // 顶部渐变条 |
| | | JPanel gradientBar = new JPanel(); |
| | | gradientBar.setBackground(PRIMARY_COLOR); |
| | | gradientBar.setPreferredSize(new Dimension(400, 5)); |
| | | gradientBar.setMaximumSize(new Dimension(400, 5)); |
| | | gradientBar.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 标题区域 |
| | | JPanel headerPanel = new JPanel(); |
| | | headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.Y_AXIS)); |
| | | headerPanel.setBackground(CARD_COLOR); |
| | | headerPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 图标 |
| | | JLabel iconLabel = new JLabel("📋"); |
| | | iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 42)); |
| | | iconLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | iconLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0)); |
| | | |
| | | // 标题 |
| | | JLabel titleLabel = new JLabel("版本管理"); |
| | | titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 副标题 |
| | | JLabel subtitleLabel = new JLabel("UWB人员定位卡发卡机管理系统"); |
| | | subtitleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | subtitleLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | subtitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | subtitleLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 25, 0)); |
| | | |
| | | headerPanel.add(iconLabel); |
| | | headerPanel.add(titleLabel); |
| | | headerPanel.add(subtitleLabel); |
| | | |
| | | // 当前版本卡片 |
| | | JPanel currentVersionPanel = createCurrentVersionPanel(); |
| | | |
| | | // 更新状态区域 |
| | | JPanel statusPanel = createStatusPanel(); |
| | | |
| | | // 下载进度 |
| | | JPanel progressPanel = createProgressPanel(); |
| | | |
| | | // 按钮区域 |
| | | JPanel buttonPanel = createButtonPanel(); |
| | | |
| | | // 关闭按钮 |
| | | JButton closeBtn = new JButton("关闭"); |
| | | closeBtn.setFont(new Font("Microsoft YaHei", Font.BOLD, 15)); |
| | | closeBtn.setBackground(new Color(255, 255, 255, 20)); |
| | | closeBtn.setForeground(TEXT_COLOR); |
| | | closeBtn.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(255, 255, 255, 50), 1), |
| | | BorderFactory.createEmptyBorder(12, 20, 12, 20) |
| | | )); |
| | | closeBtn.setFocusPainted(false); |
| | | closeBtn.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | closeBtn.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | closeBtn.setMaximumSize(new Dimension(400, 45)); |
| | | |
| | | closeButton = closeBtn; |
| | | |
| | | // 组装所有组件 |
| | | panel.add(gradientBar); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(headerPanel); |
| | | panel.add(currentVersionPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(statusPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 15))); |
| | | panel.add(progressPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(buttonPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 15))); |
| | | panel.add(closeBtn); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createCurrentVersionPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(new Color(15, 28, 48, 150)); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(255, 255, 255, 12), 1), |
| | | BorderFactory.createEmptyBorder(20, 20, 20, 20) |
| | | )); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 150)); |
| | | |
| | | // 版本号标签 |
| | | currentVersionLabel = new JLabel(currentVersion); |
| | | currentVersionLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22)); |
| | | currentVersionLabel.setForeground(Color.WHITE); |
| | | currentVersionLabel.setBackground(PRIMARY_COLOR); |
| | | currentVersionLabel.setOpaque(true); |
| | | currentVersionLabel.setBorder(BorderFactory.createEmptyBorder(10, 25, 10, 25)); |
| | | currentVersionLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 版本描述 |
| | | JLabel versionDescLabel = new JLabel("当前版本"); |
| | | versionDescLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | versionDescLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | versionDescLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | versionDescLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0)); |
| | | |
| | | // 更新日期 |
| | | versionDateLabel = new JLabel("最后更新: " + currentDate); |
| | | versionDateLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | versionDateLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | versionDateLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | panel.add(currentVersionLabel); |
| | | panel.add(versionDescLabel); |
| | | panel.add(versionDateLabel); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createStatusPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(new Color(15, 28, 48, 150)); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(255, 255, 255, 12), 1), |
| | | BorderFactory.createEmptyBorder(20, 20, 20, 20) |
| | | )); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 200)); |
| | | |
| | | JPanel contentPanel = new JPanel(); |
| | | contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); |
| | | contentPanel.setBackground(new Color(15, 28, 48, 0)); |
| | | contentPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 状态图标 |
| | | statusIconLabel = new JLabel("✅"); |
| | | statusIconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 48)); |
| | | statusIconLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | statusIconLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); |
| | | |
| | | // 状态标题 |
| | | statusTitleLabel = new JLabel("系统已是最新版本"); |
| | | statusTitleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18)); |
| | | statusTitleLabel.setForeground(TEXT_COLOR); |
| | | statusTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 状态消息 |
| | | statusMessageLabel = new JLabel("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | statusMessageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | statusMessageLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | statusMessageLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | statusMessageLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 15, 0)); |
| | | |
| | | // 最新版本信息面板 |
| | | latestVersionPanel = new JPanel(); |
| | | latestVersionPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
| | | latestVersionPanel.setBackground(new Color(243, 156, 18, 38)); |
| | | latestVersionPanel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(243, 156, 18, 75), 1), |
| | | BorderFactory.createEmptyBorder(10, 15, 10, 15) |
| | | )); |
| | | latestVersionPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | latestVersionPanel.setMaximumSize(new Dimension(350, 50)); |
| | | |
| | | JLabel arrowLabel = new JLabel("⬆️"); |
| | | arrowLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 14)); |
| | | |
| | | JLabel latestTextLabel = new JLabel("最新版本: "); |
| | | latestTextLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | latestTextLabel.setForeground(WARNING_COLOR); |
| | | |
| | | latestVersionLabel = new JLabel(latestVersion); |
| | | latestVersionLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 15)); |
| | | latestVersionLabel.setForeground(WARNING_COLOR); |
| | | |
| | | latestVersionPanel.add(arrowLabel); |
| | | latestVersionPanel.add(latestTextLabel); |
| | | latestVersionPanel.add(latestVersionLabel); |
| | | latestVersionPanel.setVisible(false); |
| | | |
| | | contentPanel.add(statusIconLabel); |
| | | contentPanel.add(statusTitleLabel); |
| | | contentPanel.add(statusMessageLabel); |
| | | contentPanel.add(latestVersionPanel); |
| | | |
| | | panel.add(contentPanel); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createProgressPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(CARD_COLOR); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 60)); |
| | | panel.setVisible(false); |
| | | |
| | | // 进度信息 |
| | | JPanel infoPanel = new JPanel(new BorderLayout()); |
| | | infoPanel.setBackground(CARD_COLOR); |
| | | infoPanel.setMaximumSize(new Dimension(400, 25)); |
| | | |
| | | JLabel downloadingLabel = new JLabel("下载更新..."); |
| | | downloadingLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | downloadingLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | |
| | | progressPercentLabel = new JLabel("0%"); |
| | | progressPercentLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); |
| | | progressPercentLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | |
| | | infoPanel.add(downloadingLabel, BorderLayout.WEST); |
| | | infoPanel.add(progressPercentLabel, BorderLayout.EAST); |
| | | |
| | | // 进度条 |
| | | downloadProgressBar = new JProgressBar(0, 100); |
| | | downloadProgressBar.setValue(0); |
| | | downloadProgressBar.setBackground(new Color(255, 255, 255, 25)); |
| | | downloadProgressBar.setForeground(SECONDARY_COLOR); |
| | | downloadProgressBar.setBorder(BorderFactory.createEmptyBorder()); |
| | | downloadProgressBar.setMaximumSize(new Dimension(400, 8)); |
| | | |
| | | panel.add(infoPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 8))); |
| | | panel.add(downloadProgressBar); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createButtonPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new GridLayout(1, 2, 15, 0)); |
| | | panel.setBackground(CARD_COLOR); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 50)); |
| | | |
| | | // 检查更新按钮 |
| | | checkUpdateButton = new JButton("检查更新"); |
| | | checkUpdateButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | checkUpdateButton.setBackground(PRIMARY_COLOR); |
| | | checkUpdateButton.setForeground(Color.WHITE); |
| | | checkUpdateButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0)); |
| | | checkUpdateButton.setFocusPainted(false); |
| | | checkUpdateButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | |
| | | // 立即更新按钮 |
| | | updateButton = new JButton("立即更新"); |
| | | updateButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | updateButton.setBackground(new Color(128, 128, 128)); |
| | | updateButton.setForeground(Color.WHITE); |
| | | updateButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0)); |
| | | updateButton.setFocusPainted(false); |
| | | updateButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | updateButton.setEnabled(false); |
| | | |
| | | panel.add(checkUpdateButton); |
| | | panel.add(updateButton); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private void setupEventListeners() { |
| | | // 关闭按钮 |
| | | closeButton.addActionListener(e -> { |
| | | dispose(); |
| | | }); |
| | | |
| | | // 检查更新按钮 |
| | | checkUpdateButton.addActionListener(e -> { |
| | | checkForUpdates(); |
| | | }); |
| | | |
| | | // 立即更新按钮 |
| | | updateButton.addActionListener(e -> { |
| | | startUpdate(); |
| | | }); |
| | | } |
| | | |
| | | private void updateVersionInfo() { |
| | | currentVersionLabel.setText(currentVersion); |
| | | versionDateLabel.setText("最后更新: " + currentDate); |
| | | latestVersionLabel.setText(latestVersion); |
| | | |
| | | if (updateAvailable) { |
| | | showUpdateAvailable(); |
| | | } else { |
| | | showUpToDate(); |
| | | } |
| | | } |
| | | |
| | | private void showUpToDate() { |
| | | statusIconLabel.setText("✅"); |
| | | statusTitleLabel.setText("系统已是最新版本"); |
| | | statusMessageLabel.setText("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | latestVersionPanel.setVisible(false); |
| | | updateButton.setEnabled(false); |
| | | updateButton.setBackground(new Color(128, 128, 128)); |
| | | } |
| | | |
| | | private void showUpdateAvailable() { |
| | | statusIconLabel.setText("⚠️"); |
| | | statusTitleLabel.setText("发现新版本"); |
| | | statusMessageLabel.setText("有新版本可用,建议更新以获得更好的体验和功能。"); |
| | | latestVersionPanel.setVisible(true); |
| | | updateButton.setEnabled(true); |
| | | updateButton.setBackground(SECONDARY_COLOR); |
| | | } |
| | | |
| | | private void checkForUpdates() { |
| | | // 保存原始按钮状态 |
| | | String originalText = checkUpdateButton.getText(); |
| | | |
| | | // 显示检查中状态 |
| | | checkUpdateButton.setText("检查中..."); |
| | | checkUpdateButton.setEnabled(false); |
| | | |
| | | // 模拟网络请求延迟 |
| | | Timer timer = new Timer(2000, e -> { |
| | | // 随机决定是否有更新 |
| | | updateAvailable = Math.random() > 0.5; |
| | | |
| | | if (updateAvailable) { |
| | | showUpdateAvailable(); |
| | | } else { |
| | | showUpToDate(); |
| | | } |
| | | |
| | | // 恢复按钮状态 |
| | | checkUpdateButton.setText(originalText); |
| | | checkUpdateButton.setEnabled(true); |
| | | |
| | | // 显示检查完成提示 |
| | | String originalMessage = statusMessageLabel.getText(); |
| | | statusMessageLabel.setText(updateAvailable ? |
| | | "✓ 检查完成,发现新版本可用" : |
| | | "✓ 检查完成,系统已是最新版本"); |
| | | |
| | | // 3秒后恢复原消息 |
| | | Timer restoreTimer = new Timer(3000, e2 -> { |
| | | if (updateAvailable) { |
| | | statusMessageLabel.setText("有新版本可用,建议更新以获得更好的体验和功能。"); |
| | | } else { |
| | | statusMessageLabel.setText("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | } |
| | | }); |
| | | restoreTimer.setRepeats(false); |
| | | restoreTimer.start(); |
| | | }); |
| | | timer.setRepeats(false); |
| | | timer.start(); |
| | | } |
| | | |
| | | private void startUpdate() { |
| | | // 禁用按钮 |
| | | checkUpdateButton.setEnabled(false); |
| | | updateButton.setEnabled(false); |
| | | updateButton.setText("更新中..."); |
| | | |
| | | // 显示下载进度 |
| | | JPanel progressPanel = (JPanel) mainPanel.getComponent(0).getComponentAt(275, 500); |
| | | progressPanel.setVisible(true); |
| | | |
| | | // 模拟下载进度 |
| | | Timer progressTimer = new Timer(100, null); |
| | | progressTimer.addActionListener(new ActionListener() { |
| | | int progress = 0; |
| | | |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | progress += (int) (Math.random() * 8); |
| | | if (progress >= 100) { |
| | | progress = 100; |
| | | progressTimer.stop(); |
| | | |
| | | // 更新完成 |
| | | Timer completionTimer = new Timer(500, e2 -> { |
| | | progressPanel.setVisible(false); |
| | | updateButton.setText("更新完成"); |
| | | updateButton.setBackground(new Color(149, 165, 166)); |
| | | |
| | | // 更新版本信息 |
| | | currentVersion = latestVersion; |
| | | currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | updateAvailable = false; |
| | | updateVersionInfo(); |
| | | |
| | | // 显示成功消息 |
| | | statusIconLabel.setText("✅"); |
| | | statusTitleLabel.setText("更新完成"); |
| | | statusMessageLabel.setText("系统已成功更新到最新版本,所有功能已优化。"); |
| | | }); |
| | | completionTimer.setRepeats(false); |
| | | completionTimer.start(); |
| | | } |
| | | |
| | | downloadProgressBar.setValue(progress); |
| | | progressPercentLabel.setText(progress + "%"); |
| | | } |
| | | }); |
| | | progressTimer.start(); |
| | | } |
| | | |
| | | // 主方法测试 |
| | | public static void main(String[] args) { |
| | | // 设置系统外观 |
| | | try { |
| | | UIManager.setLookAndFeel(UIManager.getLookAndFeel()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | SwingUtilities.invokeLater(() -> { |
| | | banbenguanli frame = new banbenguanli(); |
| | | frame.setVisible(true); |
| | | }); |
| | | } |
| | | // 按钮组件 |
| | | private JButton checkUpdateButton; |
| | | private JButton updateButton; |
| | | |
| | | // 进度条组件 |
| | | private JProgressBar downloadProgressBar; |
| | | private JLabel progressPercentLabel; |
| | | |
| | | // 版本数据 |
| | | private String currentVersion = "V1.0.0"; |
| | | private String currentDate = "2025-11-20"; |
| | | private String latestVersion = "V1.0.1"; |
| | | private boolean updateAvailable = false; |
| | | |
| | | // API配置 |
| | | private final String UPDATE_CHECK_URL = "http://39.106.210.13:8090/api/wx/findTbUpapp"; |
| | | private final String LOGIN_URL = "http://39.106.210.13:8090/api/wx/login"; |
| | | private final String APP_NAME = "发卡app"; |
| | | private final String LOGIN_USERNAME = "王飞"; |
| | | private final String LOGIN_PASSWORD = "admin"; |
| | | |
| | | private String downloadUrl = ""; |
| | | private String releaseNotes = ""; |
| | | private String loginToken = ""; |
| | | |
| | | // 颜色定义 |
| | | private final Color BACKGROUND_COLOR = new Color(15, 28, 48); |
| | | private final Color CARD_COLOR = new Color(26, 43, 68); |
| | | private final Color PRIMARY_COLOR = new Color(52, 152, 219); |
| | | private final Color SECONDARY_COLOR = new Color(46, 204, 113); |
| | | private final Color WARNING_COLOR = new Color(243, 156, 18); |
| | | private final Color TEXT_COLOR = new Color(224, 224, 224); |
| | | private final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255); |
| | | |
| | | public banbenguanli(JFrame parent) { |
| | | super(parent, "版本管理", true); |
| | | initializeUI(); |
| | | setupEventListeners(); |
| | | updateVersionInfo(); |
| | | } |
| | | |
| | | private void initializeUI() { |
| | | // 设置弹窗属性 - 7寸竖屏 (480x800) |
| | | setTitle("版本管理"); |
| | | setSize(480, 800); |
| | | setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| | | setLocationRelativeTo(getParent()); |
| | | setResizable(false); |
| | | |
| | | // 设置不透明背景 |
| | | getContentPane().setBackground(BACKGROUND_COLOR); |
| | | |
| | | // 主面板 |
| | | mainPanel = new JPanel(); |
| | | mainPanel.setLayout(new BorderLayout()); |
| | | mainPanel.setBackground(BACKGROUND_COLOR); |
| | | mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
| | | |
| | | // 创建版本管理弹窗内容 |
| | | JPanel versionPanel = createVersionPanel(); |
| | | mainPanel.add(versionPanel, BorderLayout.CENTER); |
| | | |
| | | add(mainPanel); |
| | | } |
| | | |
| | | private JPanel createVersionPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(CARD_COLOR); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(PRIMARY_COLOR, 1), |
| | | BorderFactory.createEmptyBorder(25, 25, 25, 25) |
| | | )); |
| | | |
| | | // 顶部渐变条 |
| | | JPanel gradientBar = new JPanel(); |
| | | gradientBar.setBackground(PRIMARY_COLOR); |
| | | gradientBar.setPreferredSize(new Dimension(400, 5)); |
| | | gradientBar.setMaximumSize(new Dimension(400, 5)); |
| | | gradientBar.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 标题区域 |
| | | JPanel headerPanel = new JPanel(); |
| | | headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.Y_AXIS)); |
| | | headerPanel.setBackground(CARD_COLOR); |
| | | headerPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 标题 |
| | | JLabel titleLabel = new JLabel("版本管理"); |
| | | titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 副标题 |
| | | JLabel subtitleLabel = new JLabel("UWB人员定位卡发卡机管理系统"); |
| | | subtitleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | subtitleLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | subtitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | subtitleLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 25, 0)); |
| | | |
| | | headerPanel.add(titleLabel); |
| | | headerPanel.add(subtitleLabel); |
| | | |
| | | // 当前版本卡片 |
| | | JPanel currentVersionPanel = createCurrentVersionPanel(); |
| | | |
| | | // 更新状态区域 |
| | | JPanel statusPanel = createStatusPanel(); |
| | | |
| | | // 下载进度 |
| | | JPanel progressPanel = createProgressPanel(); |
| | | |
| | | // 按钮区域 |
| | | JPanel buttonPanel = createButtonPanel(); |
| | | |
| | | // 关闭按钮 |
| | | JButton closeBtn = new JButton("关闭"); |
| | | closeBtn.setFont(new Font("Microsoft YaHei", Font.BOLD, 15)); |
| | | closeBtn.setBackground(new Color(60, 60, 60)); |
| | | closeBtn.setForeground(TEXT_COLOR); |
| | | closeBtn.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(100, 100, 100), 1), |
| | | BorderFactory.createEmptyBorder(12, 20, 12, 20) |
| | | )); |
| | | closeBtn.setFocusPainted(false); |
| | | closeBtn.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | closeBtn.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | closeBtn.setMaximumSize(new Dimension(400, 45)); |
| | | closeBtn.setOpaque(true); |
| | | |
| | | closeButton = closeBtn; |
| | | |
| | | // 组装所有组件 |
| | | panel.add(gradientBar); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(headerPanel); |
| | | panel.add(currentVersionPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(statusPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 15))); |
| | | panel.add(progressPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 20))); |
| | | panel.add(buttonPanel); |
| | | panel.add(Box.createRigidArea(new Dimension(0, 15))); |
| | | panel.add(closeBtn); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createCurrentVersionPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(new Color(20, 35, 55)); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(80, 80, 80), 1), |
| | | BorderFactory.createEmptyBorder(20, 20, 20, 20) |
| | | )); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 150)); |
| | | panel.setOpaque(true); |
| | | |
| | | // 版本号标签 |
| | | currentVersionLabel = new JLabel(currentVersion); |
| | | currentVersionLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22)); |
| | | currentVersionLabel.setForeground(Color.WHITE); |
| | | currentVersionLabel.setBackground(PRIMARY_COLOR); |
| | | currentVersionLabel.setOpaque(true); |
| | | currentVersionLabel.setBorder(BorderFactory.createEmptyBorder(10, 25, 10, 25)); |
| | | currentVersionLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | |
| | | // 版本描述 |
| | | JLabel versionDescLabel = new JLabel("当前版本"); |
| | | versionDescLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | versionDescLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | versionDescLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | versionDescLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0)); |
| | | versionDescLabel.setOpaque(true); |
| | | versionDescLabel.setBackground(new Color(20, 35, 55)); |
| | | |
| | | // 更新日期 |
| | | versionDateLabel = new JLabel("最后更新: " + currentDate); |
| | | versionDateLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | versionDateLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | versionDateLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | versionDateLabel.setOpaque(true); |
| | | versionDateLabel.setBackground(new Color(20, 35, 55)); |
| | | |
| | | panel.add(currentVersionLabel); |
| | | panel.add(versionDescLabel); |
| | | panel.add(versionDateLabel); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createStatusPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| | | panel.setBackground(new Color(20, 35, 55)); |
| | | panel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(new Color(80, 80, 80), 1), |
| | | BorderFactory.createEmptyBorder(20, 20, 20, 20) |
| | | )); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 150)); |
| | | panel.setOpaque(true); |
| | | |
| | | JPanel contentPanel = new JPanel(); |
| | | contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); |
| | | contentPanel.setBackground(new Color(20, 35, 55)); |
| | | contentPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | contentPanel.setOpaque(true); |
| | | |
| | | // 状态标题 |
| | | statusTitleLabel = new JLabel("系统已是最新版本"); |
| | | statusTitleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18)); |
| | | statusTitleLabel.setForeground(TEXT_COLOR); |
| | | statusTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | statusTitleLabel.setOpaque(true); |
| | | statusTitleLabel.setBackground(new Color(20, 35, 55)); |
| | | |
| | | // 状态消息 |
| | | statusMessageLabel = new JLabel("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | statusMessageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | statusMessageLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | statusMessageLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | statusMessageLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 15, 0)); |
| | | statusMessageLabel.setOpaque(true); |
| | | statusMessageLabel.setBackground(new Color(20, 35, 55)); |
| | | |
| | | // 最新版本信息面板 |
| | | latestVersionPanel = new JPanel(); |
| | | latestVersionPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); |
| | | latestVersionPanel.setBackground(new Color(60, 50, 30)); |
| | | latestVersionPanel.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createLineBorder(WARNING_COLOR, 1), |
| | | BorderFactory.createEmptyBorder(10, 15, 10, 15) |
| | | )); |
| | | latestVersionPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | latestVersionPanel.setMaximumSize(new Dimension(350, 40)); |
| | | latestVersionPanel.setOpaque(true); |
| | | |
| | | JLabel latestTextLabel = new JLabel("最新版本: "); |
| | | latestTextLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | latestTextLabel.setForeground(WARNING_COLOR); |
| | | latestTextLabel.setOpaque(true); |
| | | latestTextLabel.setBackground(new Color(60, 50, 30)); |
| | | |
| | | latestVersionLabel = new JLabel(latestVersion); |
| | | latestVersionLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 15)); |
| | | latestVersionLabel.setForeground(WARNING_COLOR); |
| | | latestVersionLabel.setOpaque(true); |
| | | latestVersionLabel.setBackground(new Color(60, 50, 30)); |
| | | |
| | | latestVersionPanel.add(latestTextLabel); |
| | | latestVersionPanel.add(latestVersionLabel); |
| | | latestVersionPanel.setVisible(false); |
| | | |
| | | contentPanel.add(statusTitleLabel); |
| | | contentPanel.add(statusMessageLabel); |
| | | contentPanel.add(latestVersionPanel); |
| | | |
| | | panel.add(contentPanel); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private JPanel createProgressPanel() { |
| | | // 将创建的面板赋值给成员变量 progressPanel |
| | | progressPanel = new JPanel(); |
| | | progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.Y_AXIS)); |
| | | progressPanel.setBackground(CARD_COLOR); |
| | | progressPanel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | progressPanel.setMaximumSize(new Dimension(400, 60)); |
| | | progressPanel.setVisible(false); |
| | | progressPanel.setOpaque(true); |
| | | |
| | | // 进度信息 |
| | | JPanel infoPanel = new JPanel(new BorderLayout()); |
| | | infoPanel.setBackground(CARD_COLOR); |
| | | infoPanel.setMaximumSize(new Dimension(400, 25)); |
| | | infoPanel.setOpaque(true); |
| | | |
| | | JLabel downloadingLabel = new JLabel("下载更新..."); |
| | | downloadingLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); |
| | | downloadingLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | downloadingLabel.setOpaque(true); |
| | | downloadingLabel.setBackground(CARD_COLOR); |
| | | |
| | | progressPercentLabel = new JLabel("0%"); |
| | | progressPercentLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); |
| | | progressPercentLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | progressPercentLabel.setOpaque(true); |
| | | progressPercentLabel.setBackground(CARD_COLOR); |
| | | |
| | | infoPanel.add(downloadingLabel, BorderLayout.WEST); |
| | | infoPanel.add(progressPercentLabel, BorderLayout.EAST); |
| | | |
| | | // 进度条 - 不透明 |
| | | downloadProgressBar = new JProgressBar(0, 100); |
| | | downloadProgressBar.setValue(0); |
| | | downloadProgressBar.setBackground(Color.WHITE); |
| | | downloadProgressBar.setForeground(SECONDARY_COLOR); |
| | | downloadProgressBar.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200))); |
| | | downloadProgressBar.setMaximumSize(new Dimension(400, 12)); |
| | | downloadProgressBar.setOpaque(true); |
| | | |
| | | progressPanel.add(infoPanel); |
| | | progressPanel.add(Box.createRigidArea(new Dimension(0, 8))); |
| | | progressPanel.add(downloadProgressBar); |
| | | |
| | | return progressPanel; |
| | | } |
| | | |
| | | private JPanel createButtonPanel() { |
| | | JPanel panel = new JPanel(); |
| | | panel.setLayout(new GridLayout(1, 2, 15, 0)); |
| | | panel.setBackground(CARD_COLOR); |
| | | panel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| | | panel.setMaximumSize(new Dimension(400, 50)); |
| | | panel.setOpaque(true); |
| | | |
| | | // 检查更新按钮 |
| | | checkUpdateButton = new JButton("检查更新"); |
| | | checkUpdateButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | checkUpdateButton.setBackground(PRIMARY_COLOR); |
| | | checkUpdateButton.setForeground(Color.WHITE); |
| | | checkUpdateButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0)); |
| | | checkUpdateButton.setFocusPainted(false); |
| | | checkUpdateButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | checkUpdateButton.setOpaque(true); |
| | | |
| | | // 立即更新按钮 |
| | | updateButton = new JButton("立即更新"); |
| | | updateButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | updateButton.setBackground(new Color(128, 128, 128)); |
| | | updateButton.setForeground(Color.WHITE); |
| | | updateButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0)); |
| | | updateButton.setFocusPainted(false); |
| | | updateButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | updateButton.setEnabled(false); |
| | | updateButton.setOpaque(true); |
| | | |
| | | panel.add(checkUpdateButton); |
| | | panel.add(updateButton); |
| | | |
| | | return panel; |
| | | } |
| | | |
| | | private void setupEventListeners() { |
| | | // 关闭按钮 |
| | | closeButton.addActionListener(e -> { |
| | | dispose(); |
| | | }); |
| | | |
| | | // 检查更新按钮 |
| | | checkUpdateButton.addActionListener(e -> { |
| | | checkForUpdates(); |
| | | }); |
| | | |
| | | // 立即更新按钮 |
| | | updateButton.addActionListener(e -> { |
| | | startUpdate(); |
| | | }); |
| | | } |
| | | |
| | | private void updateVersionInfo() { |
| | | currentVersionLabel.setText(currentVersion); |
| | | versionDateLabel.setText("最后更新: " + currentDate); |
| | | latestVersionLabel.setText(latestVersion); |
| | | |
| | | if (updateAvailable) { |
| | | showUpdateAvailable(); |
| | | } else { |
| | | showUpToDate(); |
| | | } |
| | | } |
| | | |
| | | private void showUpToDate() { |
| | | statusTitleLabel.setText("系统已是最新版本"); |
| | | statusMessageLabel.setText("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | latestVersionPanel.setVisible(false); |
| | | updateButton.setEnabled(false); |
| | | updateButton.setBackground(new Color(128, 128, 128)); |
| | | } |
| | | |
| | | private void showUpdateAvailable() { |
| | | statusTitleLabel.setText("发现新版本"); |
| | | statusMessageLabel.setText("有新版本可用,建议更新以获得更好的体验和功能。"); |
| | | latestVersionPanel.setVisible(true); |
| | | updateButton.setEnabled(true); |
| | | updateButton.setBackground(SECONDARY_COLOR); |
| | | } |
| | | |
| | | // 简单的 JSON 解析方法 |
| | | private String extractJsonValue(String json, String key) { |
| | | try { |
| | | String searchKey = "\"" + key + "\":"; |
| | | int startIndex = json.indexOf(searchKey); |
| | | if (startIndex == -1) return ""; |
| | | |
| | | startIndex += searchKey.length(); |
| | | int endIndex = json.indexOf(",", startIndex); |
| | | if (endIndex == -1) endIndex = json.indexOf("}", startIndex); |
| | | if (endIndex == -1) return ""; |
| | | |
| | | String value = json.substring(startIndex, endIndex).trim(); |
| | | // 去除引号 |
| | | if (value.startsWith("\"") && value.endsWith("\"")) { |
| | | value = value.substring(1, value.length() - 1); |
| | | } |
| | | return value; |
| | | } catch (Exception e) { |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | private int extractJsonInt(String json, String key) { |
| | | try { |
| | | String value = extractJsonValue(json, key); |
| | | return Integer.parseInt(value); |
| | | } catch (Exception e) { |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | private String normalizeVersion(String version) { |
| | | if (version == null || version.trim().isEmpty()) return ""; |
| | | String normalized = version.trim(); |
| | | if (!normalized.toUpperCase().startsWith("V")) { |
| | | normalized = "V" + normalized; |
| | | } |
| | | return normalized.replace("v", "V"); |
| | | } |
| | | |
| | | private void checkForUpdates() { |
| | | // 保存原始按钮状态 |
| | | String originalText = checkUpdateButton.getText(); |
| | | |
| | | // 显示检查中状态 |
| | | checkUpdateButton.setText("检查中..."); |
| | | checkUpdateButton.setEnabled(false); |
| | | statusTitleLabel.setText("检查更新"); |
| | | statusMessageLabel.setText("正在检查最新版本,请稍候..."); |
| | | |
| | | // 使用SwingWorker执行网络请求,避免阻塞UI |
| | | SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() { |
| | | @Override |
| | | protected Boolean doInBackground() throws Exception { |
| | | try { |
| | | // 构建请求URL |
| | | String urlStr = UPDATE_CHECK_URL + "?name=" + URLEncoder.encode(APP_NAME, "UTF-8"); |
| | | URL url = new URL(urlStr); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | connection.setConnectTimeout(10000); |
| | | connection.setReadTimeout(15000); |
| | | |
| | | int responseCode = connection.getResponseCode(); |
| | | if (responseCode == 200) { |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | String inputLine; |
| | | StringBuilder response = new StringBuilder(); |
| | | |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | in.close(); |
| | | |
| | | // 解析JSON响应(使用简单字符串处理) |
| | | String jsonResponse = response.toString(); |
| | | int code = extractJsonInt(jsonResponse, "code"); |
| | | |
| | | if (code == 0) { |
| | | // 提取data对象 |
| | | int dataStart = jsonResponse.indexOf("\"data\":"); |
| | | if (dataStart == -1) { |
| | | throw new Exception("响应中未找到data字段"); |
| | | } |
| | | |
| | | String serverVersionRaw = extractJsonValue(jsonResponse, "version"); |
| | | String serverVersion = normalizeVersion(serverVersionRaw); |
| | | String currentVersionNormalized = normalizeVersion(currentVersion); |
| | | |
| | | // 比较版本 |
| | | boolean hasUpdate = !serverVersion.equals(currentVersionNormalized); |
| | | |
| | | if (hasUpdate) { |
| | | latestVersion = serverVersion; |
| | | downloadUrl = extractJsonValue(jsonResponse, "address"); |
| | | releaseNotes = extractJsonValue(jsonResponse, "info"); |
| | | if (releaseNotes.isEmpty()) { |
| | | releaseNotes = "有新版本可用,建议更新以获得更好的体验和功能。"; |
| | | } |
| | | |
| | | String uptime = extractJsonValue(jsonResponse, "uptime"); |
| | | if (!uptime.isEmpty()) { |
| | | currentDate = uptime; |
| | | } |
| | | } |
| | | |
| | | return hasUpdate; |
| | | } else { |
| | | throw new Exception("服务器返回错误代码: " + code); |
| | | } |
| | | } else { |
| | | throw new Exception("HTTP错误代码: " + responseCode); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | protected void done() { |
| | | try { |
| | | updateAvailable = get(); |
| | | |
| | | if (updateAvailable) { |
| | | showUpdateAvailable(); |
| | | statusMessageLabel.setText("检查完成,发现新版本可用"); |
| | | } else { |
| | | showUpToDate(); |
| | | statusMessageLabel.setText("检查完成,系统已是最新版本"); |
| | | } |
| | | |
| | | // 恢复按钮状态 |
| | | checkUpdateButton.setText(originalText); |
| | | checkUpdateButton.setEnabled(true); |
| | | |
| | | // 3秒后恢复原消息 |
| | | Timer restoreTimer = new Timer(3000, e2 -> { |
| | | if (updateAvailable) { |
| | | statusMessageLabel.setText(releaseNotes.isEmpty() ? |
| | | "有新版本可用,建议更新以获得更好的体验和功能。" : releaseNotes); |
| | | } else { |
| | | statusMessageLabel.setText("您的系统运行的是最新稳定版本,所有功能正常运行。"); |
| | | } |
| | | }); |
| | | restoreTimer.setRepeats(false); |
| | | restoreTimer.start(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | checkUpdateButton.setText(originalText); |
| | | checkUpdateButton.setEnabled(true); |
| | | statusTitleLabel.setText("检查失败"); |
| | | statusMessageLabel.setText("检查更新失败,请检查网络后重试。"); |
| | | |
| | | JOptionPane.showMessageDialog(banbenguanli.this, |
| | | "检查更新失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | worker.execute(); |
| | | } |
| | | |
| | | private void requestLoginToken() { |
| | | SwingWorker<String, Void> worker = new SwingWorker<String, Void>() { |
| | | @Override |
| | | protected String doInBackground() throws Exception { |
| | | try { |
| | | URL url = new URL(LOGIN_URL); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | | connection.setRequestProperty("Content-Type", "application/json"); |
| | | connection.setConnectTimeout(10000); |
| | | connection.setReadTimeout(15000); |
| | | connection.setDoOutput(true); |
| | | |
| | | // 构建登录数据 |
| | | String loginData = "username=" + URLEncoder.encode(LOGIN_USERNAME, "UTF-8") + |
| | | "&password=" + URLEncoder.encode(LOGIN_PASSWORD, "UTF-8"); |
| | | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | |
| | | OutputStream os = connection.getOutputStream(); |
| | | os.write(loginData.getBytes()); |
| | | os.flush(); |
| | | |
| | | int responseCode = connection.getResponseCode(); |
| | | if (responseCode == 200) { |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | String inputLine; |
| | | StringBuilder response = new StringBuilder(); |
| | | |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | in.close(); |
| | | |
| | | String jsonResponse = response.toString(); |
| | | int code = extractJsonInt(jsonResponse, "code"); |
| | | |
| | | if (code == 0) { |
| | | String tokenType = extractJsonValue(jsonResponse, "tokentype"); |
| | | String token = extractJsonValue(jsonResponse, "token"); |
| | | return tokenType + " " + token; |
| | | } else { |
| | | throw new Exception("登录失败,错误代码: " + code); |
| | | } |
| | | } else { |
| | | throw new Exception("HTTP错误代码: " + responseCode); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void done() { |
| | | try { |
| | | loginToken = get(); |
| | | statusMessageLabel.setText("正在下载最新安装包,请稍候..."); |
| | | downloadPackage(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | handleDownloadError("获取授权失败,请稍后重试。"); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | worker.execute(); |
| | | } |
| | | |
| | | private void startUpdate() { |
| | | if (!updateAvailable || downloadUrl.isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, |
| | | "无法更新:下载地址无效", "错误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | // 禁用按钮 |
| | | checkUpdateButton.setEnabled(false); |
| | | updateButton.setEnabled(false); |
| | | updateButton.setText("更新中..."); |
| | | |
| | | // 显示下载进度 |
| | | progressPanel.setVisible(true); |
| | | downloadProgressBar.setValue(0); |
| | | progressPercentLabel.setText("0%"); |
| | | |
| | | statusTitleLabel.setText("更新中"); |
| | | statusMessageLabel.setText("正在准备下载,请稍候..."); |
| | | |
| | | // 请求登录token |
| | | requestLoginToken(); |
| | | } |
| | | |
| | | private void downloadPackage() { |
| | | SwingWorker<Void, Integer> downloadWorker = new SwingWorker<Void, Integer>() { |
| | | @Override |
| | | protected Void doInBackground() throws Exception { |
| | | //System.out.println("开始下载,URL: " + downloadUrl); |
| | | //System.out.println("使用的Token: " + loginToken); |
| | | try { |
| | | URL url = new URL(downloadUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | |
| | | |
| | | // 添加认证头 |
| | | if (!loginToken.isEmpty()) { |
| | | connection.setRequestProperty("Authorization", loginToken); |
| | | //System.out.println("设置Authorization头: " + loginToken); |
| | | } |
| | | |
| | | connection.setConnectTimeout(10000); |
| | | connection.setReadTimeout(30000); |
| | | |
| | | int fileSize = connection.getContentLength(); |
| | | |
| | | // 创建下载目录 |
| | | File downloadDir = new File("downloads"); |
| | | if (!downloadDir.exists()) { |
| | | downloadDir.mkdirs(); |
| | | } |
| | | |
| | | // 创建输出文件 |
| | | String fileName = "Issue_cards_" + latestVersion + ".apk"; |
| | | File outputFile = new File(downloadDir, fileName); |
| | | |
| | | try (InputStream inputStream = connection.getInputStream(); |
| | | FileOutputStream outputStream = new FileOutputStream(outputFile)) { |
| | | |
| | | byte[] buffer = new byte[4096]; |
| | | int bytesRead; |
| | | long totalBytesRead = 0; |
| | | |
| | | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| | | outputStream.write(buffer, 0, bytesRead); |
| | | totalBytesRead += bytesRead; |
| | | |
| | | // 计算并发布进度 |
| | | int progress = 0; |
| | | if (fileSize > 0) { |
| | | progress = (int) (totalBytesRead * 100 / fileSize); |
| | | } else { |
| | | // 如果无法获取文件大小,使用模拟进度 |
| | | progress += (int) (Math.random() * 8); |
| | | if (progress >= 100) progress = 100; |
| | | } |
| | | |
| | | publish(progress); |
| | | |
| | | // 模拟下载速度 |
| | | Thread.sleep(50); |
| | | |
| | | if (isCancelled()) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | connection.disconnect(); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | protected void process(java.util.List<Integer> chunks) { |
| | | int progress = chunks.get(chunks.size() - 1); |
| | | downloadProgressBar.setValue(progress); |
| | | progressPercentLabel.setText(progress + "%"); |
| | | } |
| | | |
| | | @Override |
| | | protected void done() { |
| | | try { |
| | | get(); // 检查是否有异常 |
| | | |
| | | // 下载完成 |
| | | downloadProgressBar.setValue(100); |
| | | progressPercentLabel.setText("100%"); |
| | | |
| | | // 更新完成 |
| | | SwingUtilities.invokeLater(() -> { |
| | | finishUpdate(); |
| | | }); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | handleDownloadError("下载失败,请检查网络后重试。"); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | downloadWorker.execute(); |
| | | } |
| | | |
| | | private void finishUpdate() { |
| | | // 隐藏进度条 |
| | | progressPanel.setVisible(false); |
| | | |
| | | updateButton.setText("更新完成"); |
| | | updateButton.setBackground(new Color(149, 165, 166)); |
| | | |
| | | // 更新版本信息 |
| | | currentVersion = latestVersion; |
| | | currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); |
| | | updateAvailable = false; |
| | | updateVersionInfo(); |
| | | |
| | | // 显示成功消息 |
| | | statusTitleLabel.setText("更新完成"); |
| | | statusMessageLabel.setText("系统已成功更新到最新版本,所有功能已优化。"); |
| | | |
| | | // 重置状态 |
| | | checkUpdateButton.setEnabled(true); |
| | | updateButton.setEnabled(false); |
| | | |
| | | // 清理临时数据 |
| | | downloadUrl = ""; |
| | | releaseNotes = ""; |
| | | loginToken = ""; |
| | | |
| | | JOptionPane.showMessageDialog(banbenguanli.this, |
| | | "更新完成!安装包已下载到 downloads 文件夹。", |
| | | "更新成功", JOptionPane.INFORMATION_MESSAGE); |
| | | } |
| | | |
| | | private void handleDownloadError(String message) { |
| | | progressPanel.setVisible(false); |
| | | updateButton.setText("立即更新"); |
| | | updateButton.setEnabled(updateAvailable); |
| | | checkUpdateButton.setEnabled(true); |
| | | |
| | | statusTitleLabel.setText("更新失败"); |
| | | statusMessageLabel.setText(message); |
| | | |
| | | JOptionPane.showMessageDialog(this, message, "错误", JOptionPane.ERROR_MESSAGE); |
| | | } |
| | | |
| | | // 进度面板引用 |
| | | private JPanel progressPanel; |
| | | |
| | | |
| | | |
| | | // 静态方法:显示版本管理对话框 |
| | | public static void showVersionManagementDialog(JFrame parent) { |
| | | SwingUtilities.invokeLater(() -> { |
| | | banbenguanli dialog = new banbenguanli(parent); |
| | | dialog.setVisible(true); |
| | | }); |
| | | } |
| | | |
| | | } |