Making the user wait using Swing

前端 未结 4 986
梦谈多话
梦谈多话 2021-01-21 05:48

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

相关标签:
4条回答
  • 2021-01-21 05:57

    Prior to WebSockets, HTTP servers could not send HTTP clients 'events'; the interactions were basically request-response. Many applications work around this problem using a (client-side) polling approach. The refresh meta-tag is one way of implementing polling.

    Swing is very different -- you have the full strength of events. So the idea of making a user wait for a predetermined amount of time is usually the incorrect interaction. (Some sort of game/quiz/animation are a few of the exceptions, where simply waiting makes sense.)

    You should design a Swing GUI that is functional and responsive while the results haven't been computed/received. Once the results are available, update a model, and fire an event advertising that the model has changed.

    The model itself can do background computation, polling etc as necessary; that code is not swing specific. If you would like help on that aspect, consider looking for/asking about that separately on Stack Overflow.

    Finally, remember that sleeping on the swing thread will make the UI unresponsive. And on a related note, any event fired by your model should be queued onto the Swing event thread. See SwingUtilities.invokeLater(...) about how that is done.

    0 讨论(0)
  • 2021-01-21 05:59

    I think JProgressBar is what you need!..

    0 讨论(0)
  • 2021-01-21 06:13

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

    enter image description here

    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--;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 06:13

    Did you try with threads. Using

       Thread.sleep(10000);
    

    you can acheive it easily. You can get more information here

    0 讨论(0)
提交回复
热议问题