1

Im making a game in jframe and i dont know how to use applet and making a inner class so i stayed with jframe style. and this is my problem. how can i add delay between components in jframe.

i added a "loading.gif" in the center of the frame. and a jbutton "continue" in the south part of the frame but i want to add delay for 3 seconds before the "continue button" appears and the "loading.gif" disappear. (like in a loading part of the games).

and this is my code...

//the p2 components
            ImageIcon loading = new ImageIcon("C:\\java pics\\loading.gif");
            JLabel startLoading = new JLabel(loading);
            JButton continue1 = new JButton("Continue");
            p2.add(startLoading,borderlayout.center);
            p2.add(continue1,borderlayout.south);

//the jbutton continue event

 start.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            f.add(p2);
            f.remove(p1);
            f.setVisible(true);
            f.revalidate();
            f.repaint();

            Timer t = new Timer();
            //delay for 3 seconds
            try {
            Timer.delay(3000);
            } catch (InterruptedException ee) {
            return;
            }
            t.start();     
        }
    });

I will appreciate any help.

1 Answers1

1

but i want to add delay for 3 seconds before the "continue button" appears and the "loading.gif" disappear.

Here is what you need to do:
1. Just show the GIF in a JLABEL when you start the game.
2. Spawn a TimerTask that will execute just once 3 seconds after your JFRAME is shown.
3. Inside the timer task, call invalidate() on JFRAME and then remove the JLABEL, call invalidate() and then add JBUTTON

BOOM ! You are good to go with an amazing loading screen.
Here is the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

An SO User
  • 24,612
  • 35
  • 133
  • 221