让用户使用Swing等待 [英] Making the user wait using Swing

查看:152
本文介绍了让用户使用Swing等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make the user to wait for a certain amount of time (10 seconds). I know in JSP or in servlets we use the META tag <META HTTP-EQUIV="Refresh" CONTENT="3">. Is there any way in Swing that we can make the user wait for some time. I am using Swing; I want to make the user wait for certain amount of time, and I want to display some information that will be fetched from the database. Is it possible through Swing?

推荐答案

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

You can use javax.swing.Timer. For example:

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--;
        }
    }
}

这篇关于让用户使用Swing等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆