簡體   English   中英

我如何使用 Java Swing 計時器延遲我的界面(java swing)?

[英]how can i have a delay on my interface (java swing) using Java Swing Timer?

我是 Java 的新手,我試圖在按下 Jbutton 啟動時添加時間延遲。 我使用了 TimeUnit.SECONDS.sleep() 但它沒有用,然后我研究並發現了 java swing 計時器,但它也沒有用,我不明白為什么

btnStart.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              Timer timer = new Timer(1000, new ActionListener() {
                 @Override
                 public void actionPerformed(ActionEvent e) {
                     System.out.println("start DONE");          
                     Object step;
                     for (int i = 1; i < n; i++) {                         
                       //code that shows on interface
                       // then i want a delay here then to carry on with the iteration of for
                       timer.start();
                       };
                 }
             });
         }
     });

你快到了。 但是您似乎誤解了Timer實際上為您做了什么。

Timer充當一種偽循環,具有內置延遲。 即在每個時間段之后,都會執行。 這意味着,每次觸發ActionListener時,您都希望執行邏輯中的下一步。

例如...

在此處輸入圖像描述

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPanel contentPane = new JPanel(new GridBagLayout());

        public TestPane() {
            setLayout(new BorderLayout());
            add(new JScrollPane(contentPane));

            JButton startButton = new JButton("Start");
            startButton.addActionListener(new ActionListener() {
                private int row = 0;
                private int count = 1000;
                private DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
                @Override
                public void actionPerformed(ActionEvent e) {
                    row = 0;
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridwidth = gbc.REMAINDER;
                    gbc.weightx = 1;
                    contentPane.removeAll();
                    contentPane.revalidate();
                    contentPane.repaint();

                    Timer timer = new Timer(100, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            row++;
                            if (row >= count) {
                                ((Timer)(e.getSource())).stop();
                                return;
                            }

                            JLabel label = new JLabel(LocalDateTime.now().format(formatter));
                            contentPane.add(label, gbc);
                            contentPane.revalidate();
                            contentPane.repaint();
                            // This is only required because the layout pass seems
                            // to be execute in different run cycle, so the label's
                            // bounds are not been updated yet.  We force the layout
                            // pass so we can scroll to the label, but otherwise
                            // isn't needed
                            contentPane.doLayout();
                            Rectangle bounds = label.getBounds();
                            bounds.y += bounds.height;
                            contentPane.scrollRectToVisible(bounds);
                        }
                    });
                    timer.start();
                }
            });
            add(startButton, BorderLayout.NORTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM