今天把CDKEY生成器给完善了一下,增加了大写字母,小写字母,数字多选择性生成。
package randomString;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RandomString {
/**
* 生成随机字符串
*/
private JFrame jFrame;
private JPanel jp;
private JButton jb;
private JTextField tf,tf2;
private JLabel jl;
private JCheckBox cb1;
private JCheckBox cb2;
private JCheckBox cb3;
private Toolkit toolkit;
private Clipboard clipboard; //clipboard.setContents(Transferable contents, ClipboardOwner owner);
private StringSelection stringSel; //实现传输 String 所需能力的 Transferable
private int model;
public RandomString(){
jFrame = new JFrame("字符串生成工具 by:毛兴宇");
jp = new JPanel();
jb = new JButton("生成(s)");
tf = new JTextField(20);
tf2 = new JTextField("10",3);
jl = new JLabel("位");
cb1 = new JCheckBox("大写字母",true);
cb2 = new JCheckBox("小写字母",true);
cb3 = new JCheckBox("数字",true);
toolkit = Toolkit.getDefaultToolkit();//获得工具包
clipboard = toolkit.getSystemClipboard();//获得系统剪贴板
MyActionListener mal = new MyActionListener(); //监听器定义
JCheckBox jcb[]={cb1,cb2,cb3};
//UI添加
for (int i = 0; i < jcb.length; i++) {
jp.add(jcb[i]);
}
jp.add(tf);
jp.add(jb);
jp.add(tf2);
jp.add(jl);
jFrame.add(jp);
//快捷键设置ALT+S
jb.setMnemonic('s');
//添加监听器
jb.addActionListener(mal);
for (int i = 0; i < jcb.length; i++) {
jcb[i].addActionListener(mal);
}
//UI显示
jFrame.pack();
int w = jFrame.getToolkit().getScreenSize().width;//宽度
int h = jFrame.getToolkit().getScreenSize().height;//高度
jFrame.setLocation(w/2-301,h/2-36);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jb)){
try {
//获取JCheckBox的值,判断并调用字符串.
if(cb1.isSelected() == true && cb2.isSelected() == true && cb3.isSelected() == true)
{
model = 1;
}else if (cb2.isSelected() == true && cb3.isSelected() == true)
{
model = 2;
}else if (cb1.isSelected() == true && cb3.isSelected() == true)
{
model = 3;
}else if (cb1.isSelected() == true && cb2.isSelected() == true)
{
model = 4;
}else if (cb3.isSelected() == true)
{
model = 5;
}else if (cb2.isSelected() == true)
{
model = 6;
}else if (cb1.isSelected() == true)
{
model = 7;
}
//设置JTextField文字显示,并保存字符串到系统剪贴板.
tf.setText(RandomString.getRandomString(Integer.parseInt(tf2.getText()), model));
stringSel = new StringSelection(tf.getText());
clipboard.setContents(stringSel, null);
} catch (Exception x) {
JOptionPane.showMessageDialog(null, "请输入数字");
}
}
}
}
public static String getRandomString(int length,int model) { //length表示生成字符串的长度
String base1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String base2 = "abcdefghijklmnopqrstuvwxyz";
String base3 = "0123456789";
//根据model设置字符串由哪些组成.
Random random = new Random();
String base = new String();
if(model == 1)
{
base = base1 + base2 + base3;
}else if (model == 2) {
base = base2 + base3;
}else if (model == 3) {
base = base1 + base3;
}else if (model == 4) {
base = base1 + base2;
}else if (model == 5) {
base = base3;
}else if (model == 6) {
base = base2;
}else if (model == 7) {
base = base1;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
public static void main(String[] args) {
new RandomString();
}
}