mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-04 23:31:56 -05:00
update:demo12
This commit is contained in:
5
com/study/demo12/CodeGenerator.java
Normal file
5
com/study/demo12/CodeGenerator.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
public interface CodeGenerator {
|
||||||
|
String generate();
|
||||||
|
}
|
||||||
8
com/study/demo12/LoginCodeGenerator.java
Normal file
8
com/study/demo12/LoginCodeGenerator.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
public class LoginCodeGenerator implements CodeGenerator {
|
||||||
|
@Override
|
||||||
|
public String generate() {
|
||||||
|
return "LOGIN" + System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
com/study/demo12/PaymentCodeGenerator.java
Normal file
8
com/study/demo12/PaymentCodeGenerator.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
public class PaymentCodeGenerator implements CodeGenerator {
|
||||||
|
@Override
|
||||||
|
public String generate() {
|
||||||
|
return "PAY" + System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
com/study/demo12/TestCodeGenerator.java
Normal file
11
com/study/demo12/TestCodeGenerator.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
public class TestCodeGenerator {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CodeGenerator loginGenerator = new LoginCodeGenerator();
|
||||||
|
CodeGenerator paymentGenerator = new PaymentCodeGenerator();
|
||||||
|
|
||||||
|
System.out.println("登录验证码: " + loginGenerator.generate());
|
||||||
|
System.out.println("支付校验码: " + paymentGenerator.generate());
|
||||||
|
}
|
||||||
|
}
|
||||||
14
com/study/demo12/TestVerifyCodeLogic.java
Normal file
14
com/study/demo12/TestVerifyCodeLogic.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
public class TestVerifyCodeLogic {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
|
String last4Digits = timestamp.substring(timestamp.length() - 4);
|
||||||
|
String code = "CODE" + last4Digits;
|
||||||
|
|
||||||
|
System.out.println("完整时间戳: " + timestamp);
|
||||||
|
System.out.println("时间戳后4位: " + last4Digits);
|
||||||
|
System.out.println("生成的验证码: " + code);
|
||||||
|
System.out.println("验证码格式正确: " + (code.startsWith("CODE") && code.length() == 8));
|
||||||
|
}
|
||||||
|
}
|
||||||
38
com/study/demo12/VerifyCodeFrame.java
Normal file
38
com/study/demo12/VerifyCodeFrame.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.study.demo12;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
public class VerifyCodeFrame extends Frame {
|
||||||
|
private Label codeLabel;
|
||||||
|
|
||||||
|
public VerifyCodeFrame() {
|
||||||
|
setTitle("验证码生成窗口");
|
||||||
|
setSize(300, 150);
|
||||||
|
setLayout(new FlowLayout());
|
||||||
|
|
||||||
|
Button codeButton = new Button("获取验证码");
|
||||||
|
codeLabel = new Label("点击按钮获取验证码");
|
||||||
|
|
||||||
|
codeButton.addActionListener(e -> {
|
||||||
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
|
String last4Digits = timestamp.substring(timestamp.length() - 4);
|
||||||
|
codeLabel.setText("CODE" + last4Digits);
|
||||||
|
});
|
||||||
|
|
||||||
|
addWindowListener(new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
add(codeButton);
|
||||||
|
add(codeLabel);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new VerifyCodeFrame();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user