让用户使用Swing等待

use*_*101 3 java swing

我想让用户等待一段时间(10秒).我知道在JSP或servlet中我们使用META标记<META HTTP-EQUIV="Refresh" CONTENT="3">.在Swing中是否有任何方法可以让用户等待一段时间.我正在使用Swing; 我想让用户等待一段时间,我想显示一些将从数据库中提取的信息.通过Swing可以吗?

Eng*_*uad 5

你可以用javax.swing.Timer.例如:

在此输入图像描述

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SimpleTimer extends JFrame implements ActionListener 
{
    private JLabel label;
    private Timer timer;
    private int counter = 10; // the duration
    private int delay = 1000; // every 1 second
    private static final long serialVersionUID = 1L;

    public SimpleTimer()
    {
        super("Simple Timer");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 65);
        label = new JLabel("Wait for " + counter + " sec");
        getContentPane().add(label);
        timer = new Timer(delay, this);
        timer.setInitialDelay(0);
        timer.start();
        setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new SimpleTimer();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(counter == 0)
        {
            timer.stop();
            label.setText("The time is up!");
        }
        else
        {
            label.setText("Wait for " + counter + " sec");
            counter--;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)