【问题标题】:Perform a task periodically if some action does not happen within the period如果在一段时间内没有发生某些操作,则定期执行任务
【发布时间】:2014-01-19 14:15:53
【问题描述】:

我有这个程序来生成一个随机验证码。

我想做的就是这个。

生成代码后,如果该代码未在 TextField 中输入并在一分钟内提交,则应生成新代码。

如何在 Java 中实现这一点?我现有的代码发布在下面。

import java.awt.*;
import javax.swing.*;
import java.security.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Verification extends JFrame implements ActionListener { // Start main class

JPanel p;
JTextField t;
JLabel l, l1;
JButton b;

public Verification() { // Start constructor
super("Verification");
p = new JPanel();
add(p);
p.setLayout(null);
p.setBackground(Color.pink);
l = new JLabel("Enter Code");
l.setBounds(20, 50, 100, 20);
l.setFont(new Font("arial", Font.BOLD, 15));
p.add(l);
l1 = new JLabel("");
l1.setBounds(50, 110, 200, 40);
l1.setFont(new Font("arial", Font.BOLD, 20));
p.add(l1);
t = new JTextField();
t.setBounds(110, 50, 250, 20);
p.add(t);
b = new JButton("Login");
b.setBounds(370, 50, 100, 20);
p.add(b);
b.addActionListener(this);
setSize(500, 190);
setVisible(true);
setLocation(400, 200);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // End constructor

public void RSA() { //Start RSA
  byte[] n = {6, 1, 3, 4, 8, 9, 0, 5, 11, 24, 12, 57, 12, 78};
  for (int i = 0; i < n.length; i++) { // Start for
    n[i] = (byte) (Math.random() * 80);
    System.out.print(n[i]);
  } // End for
  System.out.println("\n");
} // End RSA

public static String MD5(String input) { // Start MD5
 byte[] n = {6, 1, 3, 4, 8, 9, 0, 5, 11, 24, 12, 57, 12, 78};
 try { // Start try1
    for (int i = 0; i < n.length; i++) { // Start for
        n[i] = (byte) (Math.random() * 80);
        System.out.print(n[i]);
    } // End for
    n = input.getBytes("UTF-8");
  } // End try1
  catch (UnsupportedEncodingException e) {
    n = input.getBytes();
  }

  String result = null;
  char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
      '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  try { // Start try2
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(n);
      byte temp[] = md.digest();
      char str[] = new char[16 * 2];
      int k = 0;
      for (int i = 0; i < 16; i++) { // Start for
        byte byte0 = temp[i];
        str[k++] = hexDigits[byte0 >>> 4 & 0xf];
        str[k++] = hexDigits[byte0 & 0xf];
     } // End for
      result = new String(str);
   } // End try2
  catch (Exception e) {
    e.printStackTrace();
   }
   return result;
 } // End MD5

 public void actionPerformed(ActionEvent e) { // Start actionPerformed
  } // End actionPerformed

public static void main(String[] args) { // Start main method
  Verification a = new Verification();
  System.out.println("The RSA Code is : \n");
  a.RSA();
  System.out.println("The MD5 on RSA Code is : \n");
  System.out.println(MD5("Javarmi.com"));
 } // End main method
} // End main class

【问题讨论】:

  • 您确实需要更详细地解释应用程序堆栈。没有足够的细节来提供全面的答复。

标签: java time scheduled-tasks timertask


【解决方案1】:

使用Timer 定期执行任务:

private Timer timer;
private String code;
public static void main(String[] args) { // Start main method
    final Verification a = new Verification();

    a.timer = new Timer(
        60 * 1000,
        new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println("The RSA Code is : \n");
                a.RSA();
                a.code = MD5("Javarmi.com");
                System.out.println("The MD5 on RSA Code is : \n");
                System.out.println(a.code);
            }
        }
    );
    a.timer.setInitialDelay(0);
    a.timer.start();
} // End main method

可以在actionPerformed(ActionEvent) 方法中执行验证。

【讨论】:

  • 谢谢,但我不明白你写的这段代码是如何工作的,你能解释一下吗..
  • Timer 实例将每 60 秒执行一次 ActionListener 代码。这负责重置code 成员变量。在这种情况下,code 不断重置为相同的值,但这是一个不同的问题。您的actionPerformed(ActionEvent) 方法应将t.getText()code 进行比较,并酌情调用timer.stop()
  • 感谢您的澄清 :)
猜你喜欢
  • 2012-05-16
  • 2017-06-08
  • 2015-11-18
  • 1970-01-01
  • 2015-11-21
  • 2012-03-28
  • 1970-01-01
  • 2015-11-13
  • 2013-01-17
相关资源
最近更新 更多