diff --git a/com/study/demo12/CodeGenerator.java b/com/study/demo12/CodeGenerator.java new file mode 100644 index 0000000..8739c2a --- /dev/null +++ b/com/study/demo12/CodeGenerator.java @@ -0,0 +1,5 @@ +package com.study.demo12; + +public interface CodeGenerator { + String generate(); +} diff --git a/com/study/demo12/LoginCodeGenerator.java b/com/study/demo12/LoginCodeGenerator.java new file mode 100644 index 0000000..eae5b89 --- /dev/null +++ b/com/study/demo12/LoginCodeGenerator.java @@ -0,0 +1,8 @@ +package com.study.demo12; + +public class LoginCodeGenerator implements CodeGenerator { + @Override + public String generate() { + return "LOGIN" + System.currentTimeMillis(); + } +} diff --git a/com/study/demo12/PaymentCodeGenerator.java b/com/study/demo12/PaymentCodeGenerator.java new file mode 100644 index 0000000..3a9bc04 --- /dev/null +++ b/com/study/demo12/PaymentCodeGenerator.java @@ -0,0 +1,8 @@ +package com.study.demo12; + +public class PaymentCodeGenerator implements CodeGenerator { + @Override + public String generate() { + return "PAY" + System.currentTimeMillis(); + } +} diff --git a/com/study/demo12/TestCodeGenerator.java b/com/study/demo12/TestCodeGenerator.java new file mode 100644 index 0000000..67d293f --- /dev/null +++ b/com/study/demo12/TestCodeGenerator.java @@ -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()); + } +} diff --git a/com/study/demo12/TestVerifyCodeLogic.java b/com/study/demo12/TestVerifyCodeLogic.java new file mode 100644 index 0000000..341f408 --- /dev/null +++ b/com/study/demo12/TestVerifyCodeLogic.java @@ -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)); + } +} diff --git a/com/study/demo12/VerifyCodeFrame.java b/com/study/demo12/VerifyCodeFrame.java new file mode 100644 index 0000000..643fd4a --- /dev/null +++ b/com/study/demo12/VerifyCodeFrame.java @@ -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(); + } +}