826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package authorizationCode;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
 
public class LicenseGeneratorGUI {
     // µ¥ÊµÀýËøÎļþ·¾¶
    private static final String LOCK_FILE = System.getProperty("user.home") + File.separator + ".license_generator.lock";
    
    public static void main(String[] args) {
        // ¼ì²éÊÇ·ñÒÑÓÐʵÀýÔËÐÐ
        if (!acquireLock()) {
            JOptionPane.showMessageDialog(null, 
                "Èí¼þÒѾ­ÔÚÔËÐÐÖУ¡", 
                "¾¯¸æ", 
                JOptionPane.WARNING_MESSAGE);
            System.exit(0);
        }
        
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }
    
    /**
     * ³¢ÊÔ»ñȡӦÓÃËø
     * @return true ±íʾ³É¹¦»ñÈ¡Ëø£¨Ê×´ÎÔËÐУ©£¬false ±íʾÒÑÓÐʵÀýÔËÐÐ
     */
    private static boolean acquireLock() {
        try {
            // ´´½¨ËøÎļþ
            File lockFile = new File(LOCK_FILE);
            if (!lockFile.exists()) {
                lockFile.createNewFile();
            }
            
            // ³¢ÊÔ»ñÈ¡ÎļþËø
            RandomAccessFile raf = new RandomAccessFile(lockFile, "rw");
            FileChannel channel = raf.getChannel();
            FileLock lock = channel.tryLock();
            
            // Ìí¼ÓJVM¹Ø±Õ¹³×ÓÊÍ·ÅËø
            if (lock != null) {
                Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                    try {
                        lock.release();
                        channel.close();
                        raf.close();
                    } catch (Exception e) {
                        // ºöÂԹرÕÒì³£
                    }
                }));
                return true;
            }
        } catch (Exception e) {
            // ÎÞ·¨»ñÈ¡Ëø
        }
        return false;
    }
    private static void createAndShowGUI() {
        // ´´½¨Ö÷´°¿Ú
        JFrame frame = new JFrame("Èí¼þÊÚȨÂëÉú³É¹¤¾ß");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 450);
        frame.setLocationRelativeTo(null);
 
        // ´´½¨Ö÷Ãæ°å
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(new Color(240, 245, 255));
 
        // ´´½¨±êÌâ
        JLabel titleLabel = new JLabel("Èí¼þÊÚȨÂëÉú³É¹¤¾ß");
        titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setForeground(new Color(0, 80, 200));
        mainPanel.add(titleLabel, BorderLayout.NORTH);
 
        // ´´½¨ÊäÈëÃæ°å
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3, 2, 10, 15));
        inputPanel.setBorder(BorderFactory.createTitledBorder("ÊäÈëÐÅÏ¢"));
        inputPanel.setBackground(new Color(250, 250, 255));
        inputPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
 
        // ¼¤»îÂëÊäÈë
        JLabel activationLabel = new JLabel("¼¤»îÂë :");
        activationLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14));
        JTextField activationField = new JTextField();
        activationField.setFont(new Font("Consolas", Font.PLAIN, 14));
       
 
        // »úÆ÷ÂëÊäÈë
        JLabel machineLabel = new JLabel("É豸»úÆ÷Âë:");
        machineLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14));
        JTextField machineField = new JTextField();
        machineField.setFont(new Font("Consolas", Font.PLAIN, 14));
 
        // ½ØÖ¹ÈÕÆÚÊäÈë
        JLabel expiryLabel = new JLabel("ÊÚȨ½ØÖ¹ÈÕÆÚ (YYYY-MM-DD):");
        expiryLabel.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14));
        JTextField expiryField = new JTextField();
        expiryField.setFont(new Font("Consolas", Font.PLAIN, 14));
 
        // Ìí¼Ó×é¼þµ½ÊäÈëÃæ°å
        inputPanel.add(activationLabel);
        inputPanel.add(activationField);
        inputPanel.add(machineLabel);
        inputPanel.add(machineField);
        inputPanel.add(expiryLabel);
        inputPanel.add(expiryField);
 
        // ´´½¨°´Å¥Ãæ°å
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.setBackground(new Color(240, 245, 255));
 
        // Éú³É°´Å¥
        JButton generateButton = new JButton("Éú³É×¢²áÂë");
        generateButton.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 16));
        generateButton.setBackground(new Color(70, 130, 220));
        generateButton.setForeground(Color.WHITE);
        generateButton.setFocusPainted(false);
        generateButton.setPreferredSize(new Dimension(180, 40));
 
        // ´´½¨Êä³öÃæ°å
        JPanel outputPanel = new JPanel();
        outputPanel.setLayout(new BorderLayout(10, 10));
        outputPanel.setBorder(BorderFactory.createTitledBorder("Éú³ÉµÄ×¢²áÂë"));
        outputPanel.setBackground(new Color(250, 250, 255));
        outputPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
 
        // ×¢²áÂëÊä³ö
        JTextArea licenseArea = new JTextArea();
        licenseArea.setFont(new Font("Consolas", Font.PLAIN, 14));
        licenseArea.setEditable(false);
        licenseArea.setLineWrap(true);
        licenseArea.setWrapStyleWord(true);
        licenseArea.setBackground(new Color(245, 250, 255));
        
        JScrollPane scrollPane = new JScrollPane(licenseArea);
        scrollPane.setPreferredSize(new Dimension(500, 100));
 
        // ¸´Öư´Å¥
        JButton copyButton = new JButton("¸´ÖƵ½¼ôÌù°å");
        copyButton.setFont(new Font("΢ÈíÑźÚ", Font.PLAIN, 14));
        copyButton.setBackground(new Color(100, 180, 100));
        copyButton.setForeground(Color.WHITE);
        copyButton.setFocusPainted(false);
 
        // Ìí¼Ó×é¼þµ½Êä³öÃæ°å
        outputPanel.add(scrollPane, BorderLayout.CENTER);
        outputPanel.add(copyButton, BorderLayout.SOUTH);
 
        // Ìí¼Óʼþ¼àÌýÆ÷
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String base64Key = activationField.getText().trim();
                String machineCode = machineField.getText().trim();
                String expiryDate = expiryField.getText().trim();
                
                if (base64Key.isEmpty() || machineCode.isEmpty() || expiryDate.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "ËùÓÐ×ֶζ¼±ØÐëÌîд£¡", "ÊäÈë´íÎó", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                try {
                    // Ê¹ÓÃÐ޸ĺóµÄLicenseGeneratorÀࣨ¼ûÏ·½Ð޸ģ©
                    String licenseKey = LicenseGenerator.generateLicenseKey(base64Key, machineCode, expiryDate);
                    licenseArea.setText(licenseKey);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(frame, "Éú³É×¢²áÂëʱ³ö´í: " + ex.getMessage(), "´íÎó", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });
 
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String licenseText = licenseArea.getText();
                if (!licenseText.isEmpty()) {
                    StringSelection selection = new StringSelection(licenseText);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, null);
                    JOptionPane.showMessageDialog(frame, "×¢²áÂëÒѸ´ÖƵ½¼ôÌù°å£¡", "¸´ÖƳɹ¦", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
 
        // Ìí¼Ó×é¼þµ½Ö÷Ãæ°å
        buttonPanel.add(generateButton);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        // ´´½¨ÈÝÆ÷Ãæ°å
        JPanel container = new JPanel(new BorderLayout(10, 20));
        container.setBorder(new EmptyBorder(10, 20, 20, 20));
        container.add(mainPanel, BorderLayout.NORTH);
        container.add(outputPanel, BorderLayout.CENTER);
        
        frame.add(container);
        frame.setVisible(true);
    }
}