为 Java 中的标签设置计时器

setting a timer for a label in Java

目前正在开发ATM模拟,主要有"display balance"、"withdraw money"、"deposit money"三个功能。由于这只是一个模拟,没有自动提款机,因此,当客户想存钱到账户时,我分配了一个随机数额。

关于我的问题,当用户点击存款时,该屏幕中有一个标签。我想让那个标签写 "counting money" 2 秒,然后显示实际数量,这是随机生成的。

我的问题是第一部分。如何让标签写"counting"2秒?

感谢您的回答和时间。

尝试使用线程。

Thread t; 
t = new Thread(){
  @Override
  public void run(){
    label.setText("counting...");
    Thread.sleep(2000);//Time in Milliseconds
    label.setText("Display what you want.");
  }
}
t.start();

Swing 提供了一个timer for this sort of thing, have a look at the documentation。例如:

label.setText("Counting");
Timer timer = new Timer(2000, e -> label.setText("Done"));
timer.setRepeats(false);
timer.start();

正如一位评论者指出的那样,它是 javax.swing.Timer 而不是您想要的 java.util.Timer,因为前者在 EDT 上执行它的操作。

假设您已经创建了一个选择面板(JPanel、JDesktopPane 等)并为存款按钮制作了一个类似的 JButton,为要在其中显示的标签制作了一个 JLabel "Counting Money",您将需要创建一个 Thread 它将 运行 与程序中的其他代码并行,这样您的程序就不必等待计数过程,直到您可以做其他事情。因此,您将创建一个 Thread 对象,如下所示,并使用代码 Calendar.getInstance().getTimeInMillis() 获取当前时间并设置一个 long 变量来保存开始时间。然后使用 while 循环,你会不断检查开始时间和当前时间之间的差异,看看是否已经过去了 2 秒。将此差异保存在循环内的另一个 long 变量中,并使循环检查其值是否超过 2000 毫秒(2 秒)。当时间超过 2 秒时,while 循环停止,Thread 可以继续执行下一行代码,将您的标签设置为空(您可以将文本更改为您想要的任何内容)。此后 Thread 停止。您的代码应如下所示:

    JLabel lblCount = new JLabel("");
    lblCount.setBounds(92, 28, 243, 90);
    windowPane.add(lblClock);

    JButton btnDeposit = new JButton("Deposit Money");
    btnDeposit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            lblCount.setText("Counting Money.....Please Wait");
            long start = Calendar.getInstance().getTimeInMillis();
            Thread timer = new Thread(){
                public void run()
                { 
                    long time = Long.valueOf(0);
                     while(time < Long.valueOf(2))
                     {
                         time = (Calendar.getInstance().getTimeInMillis() - start)/1000;
                     }

                     lblCount.setText("");
                }
            };
        }
    });
    btnDeposit.setBounds(78, 175, 118, 53);
    windowPane.add(btnDeposit);
public void crono() {
    TimerTask tarea = new TimerTask() {

        @Override
        public void run() {
            int ok = 0;
            if (actualTime < maxTime * 1000) {
                ok = 1;
                //because its in miliseconds
                actualTime = actualTime + 1000;
            }

            switch (ok) {
                case 1:
                    int displayTime= actualTime/ 1000;
                    label.setText(Integer.toString(displayTime));
                    break;
                //if actual is over maxtime
                case 0:
                    label.setText("TIME IS UP");
                    break;
                default:
                    break;
            }

        }

    };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(tarea, 0, 1000);

//the first argument will be the task, the second the starting time, and the final one is //the period, in this case it will be one second



}