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