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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
import java.net.URI;
import java.awt.Container; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Desktop;
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JPanel; import javax.swing.BorderFactory; import javax.swing.JOptionPane;
import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.Transferable;
class RandomMaker { public static void main(String args[]) { new RandomMakerUI().setVisible(true); } }
class RandomMakerUI extends JFrame { Container c = getContentPane(); JTextField txtRandomString = new JTextField(); JLabel lbLength = new JLabel(); JTextField txtLength = new JTextField(); JButton btnAddPoint = new JButton(); JButton btnOverPoint = new JButton(); JLabel lbGoLink = new JLabel(); JCheckBox cbNumber = new JCheckBox(); JCheckBox cbSmallChar = new JCheckBox(); JCheckBox cbBigChar = new JCheckBox(); JCheckBox cbCommand = new JCheckBox(); JPanel gbType = new JPanel(); JButton btnCopy = new JButton(); JButton btnMake = new JButton(); RandomMakerUI() { super("隨機數生成器"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(225,240); setResizable(false); setLocationRelativeTo(null);
this.initUI(); }
private void initUI() { c.setLayout(null);
txtRandomString.setSize(200,20); txtRandomString.setLocation(10,10); txtRandomString.setEditable(false); c.add(txtRandomString);
lbLength.setText("長度 : "); lbLength.setSize(40, 20); lbLength.setLocation(10, 40); c.add(lbLength);
txtLength.setText("20"); txtLength.setSize(20, 20); txtLength.setLocation(50, 40); txtLength.addKeyListener(new TxtLengthKeyAdapter()); c.add(txtLength);
btnAddPoint.setText("+"); btnAddPoint.setSize(45, 20); btnAddPoint.setLocation(80, 40); btnAddPoint.addActionListener(new AddPointActionPerformer()); c.add(btnAddPoint);
btnOverPoint.setText("-"); btnOverPoint.setSize(45, 20); btnOverPoint.setLocation(130, 40); btnOverPoint.addActionListener(new OverPointActionPerformer()); c.add(btnOverPoint);
lbGoLink.setText("版權"); lbGoLink.setSize(30, 20); lbGoLink.setLocation(180, 40); lbGoLink.addMouseListener(new lbGoLinkActionPerformer()); c.add(lbGoLink);
cbNumber.setText("只是數字"); cbCommand.setText("標點符號"); cbSmallChar.setText("小寫字母"); cbBigChar.setText("大寫字母"); cbNumber.setSelected(true); cbSmallChar.setSelected(true); cbBigChar.setSelected(true); gbType.add(cbNumber); gbType.add(cbCommand); gbType.add(cbSmallChar); gbType.add(cbBigChar);
gbType.setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder("包含類型"))); gbType.setSize(200, 100); gbType.setLocation(10, 70); c.add(gbType);
btnCopy.setText("複製"); btnCopy.setSize(85, 20); btnCopy.setLocation(10, 180); btnCopy.addActionListener(new btnCopyActionPerformer()); c.add(btnCopy);
btnMake.setText("生成"); btnMake.setSize(85, 20); btnMake.setLocation(120, 180); btnMake.addActionListener(new btnMakeActionPerformer()); c.add(btnMake); }
class AddPointActionPerformer implements ActionListener { public void actionPerformed(ActionEvent e) { String lenStr = txtLength.getText().toString(); int lenInt = Integer.parseInt(lenStr); txtLength.setText( (lenStr == "" || lenInt <= 0) ? "1" : String.valueOf(lenInt + 1) ); lenStr = null; } }
class OverPointActionPerformer implements ActionListener { public void actionPerformed(ActionEvent e) { String lenStr = txtLength.getText().toString(); int lenInt = Integer.parseInt(lenStr); txtLength.setText( (lenStr == "" || lenInt <= 0) ? "1" : String.valueOf(lenInt - 1) ); lenStr = null; } }
class lbGoLinkActionPerformer implements MouseListener { public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mousePressed(MouseEvent me) {} public void mouseReleased(MouseEvent me) {} public void mouseClicked(MouseEvent me) { try { Desktop.getDesktop().browse(new URI("http://skhk.uni.cc/")); }catch(Exception e) { } } }
class TxtLengthKeyAdapter implements KeyListener { String temp;
public TxtLengthKeyAdapter() { temp = txtLength.getText().toString(); } public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {this.checkFormat();}
private void checkFormat() { String lenStr = txtLength.getText().toString();
if (!SeekLib.isNumber(txtLength.getText().toString())) { SeekLib.showMessage("不是數字"); txtLength.setText(temp); return; }
if (Integer.parseInt(lenStr) < 0) { SeekLib.showMessage("不可以是負數"); txtLength.setText(temp); return; } } }
class btnCopyActionPerformer implements ActionListener { public void actionPerformed(ActionEvent e) { CopyToClipboard ctc = new CopyToClipboard(); ctc.toClipboard(txtRandomString.getText().toString()); SeekLib.showMessage("複製完成"); } }
class btnMakeActionPerformer implements ActionListener { public void actionPerformed(ActionEvent e) { String lenStr = txtLength.getText().toString();
if (!SeekLib.isNumber(lenStr)) { SeekLib.showMessage("不是數字"); return; }
if (Integer.parseInt(lenStr) < 0) { SeekLib.showMessage("不可以是負數"); return; }
StringBuilder temp = new StringBuilder(); StringBuilder temp2= new StringBuilder();
if (cbNumber.isSelected()) temp.append("0123456789"); if (cbCommand.isSelected()) temp.append("!@#$%^&*()_+{}[]:;.,?~`|"); if (cbSmallChar.isSelected()) temp.append("abcdefghijklmnopqrstuvwxyz"); if (cbBigChar.isSelected()) temp.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
if (temp.length() <= 0) { SeekLib.showMessage("請先選取生成包含"); return; }
char[] charArr = temp.toString().toCharArray(); int numAt;
for(int i=0; i<Integer.parseInt(lenStr); i++) { numAt = (int)(Math.random()*(temp.length()-1)); temp2.append( charArr[numAt] ); }
txtRandomString.setText(temp2.toString());
System.gc(); } } }
class SeekLib { public static boolean isNumber(String str) { try { Integer.parseInt(str); return true; }catch (Exception e) { return false; } }
public static void showMessage(String msg) { JOptionPane.showMessageDialog(null, msg); } }
class CopyToClipboard implements ClipboardOwner { public void toClipboard(String rndStr) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { try { sm.checkSystemClipboardAccess(); }catch (Exception e) { e.printStackTrace(); } } Toolkit tk = Toolkit.getDefaultToolkit(); StringSelection st = new StringSelection(rndStr); Clipboard cp = tk.getSystemClipboard(); cp.setContents(st, this); }
public void lostOwnership(Clipboard clip, Transferable tr) { System.out.println("Lost Clipboard Ownership?!?"); } }
|