繁体   English   中英

如何设置显示结果时间到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();
        }
    }
});

有没有一种方法可以使用Timer而不更改“ button.addActionListener(new ActionListener()......”)(我希望不使用Thread.sleep如果我使用Timer)

使用Swing计时器,该计时器的actionPerformed方法将被重复调用,这就是您的“循环”。 因此,摆脱方法内部的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();
         }
      });
   }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM