【问题标题】:How to set timer for showing result time to JTextField?如何设置计时器以向 JTextField 显示结果时间?
【发布时间】:2015-06-24 22:16:39
【问题描述】:

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

01234567

我想设置一个 Timer,这样每个数字的结果都会慢慢显示出来。

例如(在 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??(如果我使用 Timer,我希望不使用 Thread.sleep)

【问题讨论】:

    标签: java swing timer jtextfield sleep


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用 Swing Timer 并且 Timer 的 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();
               }
            });
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多