如何使用java swing計時器在我的接口(java swing)上進行延遲?

我是Java新手,我試圖在按下Jbutton start時添加一個時間延遲。我使用了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時,您都希望執行邏輯中的下一步。

For example...

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);
        }
    }
}
主站蜘蛛池模板: 无码毛片一区二区三区中文字幕 | 亚洲av无码一区二区三区不卡| 国产在线视频一区| 在线观看一区二区三区av| 成人免费av一区二区三区| 国产一区二区三区免费视频| 亚洲一区二区三区写真 | 国产精品乱码一区二区三区| 日本一区频道在线视频| 亚洲免费视频一区二区三区| 国模无码一区二区三区| 国产香蕉一区二区在线网站| 日韩精品无码一区二区三区 | 国产一区二区三区在线2021| 亚洲国产精品成人一区| 免费av一区二区三区| 嫩B人妻精品一区二区三区| 免费萌白酱国产一区二区三区 | 久久人妻无码一区二区 | 视频一区二区三区在线观看| 国内精品视频一区二区三区| 日本一区二区三区精品视频| 久久蜜桃精品一区二区三区| 成人无号精品一区二区三区| 国产成人久久一区二区三区| 国产在线精品观看一区| 国产精品无码AV一区二区三区| 中文字幕Av一区乱码| 日韩电影一区二区| 国产精品高清一区二区三区不卡| 亚洲一区欧洲一区| 国产在线步兵一区二区三区| 日韩精品一区二三区中文| 成人免费一区二区三区| 少妇激情一区二区三区视频| 亚洲综合一区国产精品| 精品一区二区三区视频在线观看| 中文字幕一区二区人妻性色| 久久精品午夜一区二区福利| 久久久无码精品人妻一区| 97久久精品一区二区三区|