如何设置计时器以向 JTextField 显示结果时间?

How to set timer for showing result time to JTextField?

我点击一个 JButton,我应该在下面的 JTextField 中得到最终输出:

01234567

我想设置一个计时器,以便每个数字的结果显示缓慢。

例如(在 JTextField 中),我希望的结果应该是这样的: 0(1 秒后) 01(1 秒后) 012(1 秒后) 0123 .......... 01234567 (JTextField 中的输出是 01234567)

我目前正在使用 Thread.sleep,但我没有得到我想要的结果。 我首先单击 JButton: (1 秒后) 01234567

我目前正在使用代码

button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        try {
            textfield.setText("");

            for (int i=0; i<8; i++)
            {
                textfield.setText(i);
                Thread.sleep(1000);
            }
        } 
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
});

有没有办法在不改变"button.addActionListener(new ActionListener()......"的情况下使用定时器?? (如果我使用 Timer,我希望不使用 Thread.sleep)

我认为您必须输入 textfield.setText(textfield.getText()+i),因为如果不这样做,您将覆盖实际内容

使用 Swing 定时器,定时器的 actionPerformed 方法将被重复调用,这将是您的 "loop"。所以去掉方法里面的for循环,肯定去掉Thread.sleep(...)

ActionListener timerListener = new ActionListener(){
    private String text = "";
    private int count = 0;

    public void actionPerformed(ActionEvent e){
        text += // something based on count
        count++;
        textField.setText(text);
        // code to stop timer once count has reached max
    }
});

例如,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class Tester extends JPanel {
   public static final int TIMER_DELAY = 1000;
   public static final String TEST_TEXT = "01234567";
   private JTextField textField = new JTextField(10);
   private JButton button = new JButton(new ButtonAction());
   private Timer timer;

   public Tester() {
      add(textField);
      add(button);
   }

   private class ButtonAction extends AbstractAction {

      public ButtonAction() {
         super("Press Me");
         putValue(MNEMONIC_KEY, KeyEvent.VK_P);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (timer != null && timer.isRunning()) {
            return;
         }
         textField.setText("");
         timer = new Timer(TIMER_DELAY, new TimerListener());
         timer.start();
      }
   }

   private class TimerListener implements ActionListener {
      private String text = "";
      private int counter = 0;

      @Override
      public void actionPerformed(ActionEvent e) {
         text += TEST_TEXT.charAt(counter);
         textField.setText(text);
         counter++;
         if (counter >= TEST_TEXT.length()) {
            timer.stop();
         }
      }
   }

   private static void createAndShowGui() {
      Tester mainPanel = new Tester();

      JFrame frame = new JFrame("Tester");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}