簡體   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