JAVA 编程 序列号
发布网友
发布时间:2022-05-12 15:07
我来回答
共1个回答
热心网友
时间:2023-10-11 08:12
//哎....
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
public class CodeTest extends JFrame {
private static final long serialVersionUID = 1L;
private JFormattedTextField formatField = null;
private JButton ba = null;
private String pattern = "AAAAA-AAAAA-AAAAA-AAAAA";
private JLabel code = new JLabel("注册码: ");
private JLabel input = new JLabel("序列号: ");
private JTextField codeField = new JTextField();
private JLabel rel = new JLabel();
public CodeTest(){
init();
}
public void init(){
MaskFormatter mft = CodeTest.getFormatter(pattern);
mft.setPlaceholderCharacter('X');
formatField = new JFormattedTextField();
formatField = new JFormattedTextField();
mft.install(formatField);
this.setBounds(200, 200, 240, 240);
this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formatField.setBounds(30, 30, 180, 25);
ba = new JButton("注册");
ba.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(formatField.getText().length() >= 1) {
}
String text = formatField.getText();
long vlong = getCode(text);
code.setText("注册码: (" + vlong + ")");
if((vlong + "").equals(codeField.getText())){
rel.setText("注册成功!");
System.exit(0);
} else {
rel.setText("注册失败!");
}
}
});
codeField.setBounds(30, 85, 180, 25);
code.setBounds(30, 60, 180, 25);
ba.setBounds(30, 125, 180, 25);
rel.setBounds(30, 155, 100, 25);
input.setForeground(Color.RED);
code.setForeground(Color.RED);
rel.setForeground(Color.RED);
input.setBounds(30, 5, 100, 25);
this.add(rel);
this.add(codeField);
this.add(input);
this.add(code);
this.add(ba);
this.add(formatField);
ba.setDefaultCapable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new CodeTest();
}
public static MaskFormatter getFormatter(String pattern){
try {
return new MaskFormatter(pattern);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public long getCode(String text){
char [] chs = text.toCharArray();
long vlong = 0;
for(char c: chs){
if(c != '-'){
vlong += 199 * (int)c;
}
}
return vlong;
}
}