Pergunta

I have a JFrame which contains a JPanel with the JButton "Press Me"

enter image description here

Pressing the "Press me" button will change to another JPanel(SecondPanel) within the same JFrame

enter image description here

I am facing a problem where there is a 10 second delay when i press the "Press Me" button before the SecondPanel appears.

This 10 second delay is caused the Timer event .

I wish for the SecondPanel to appear before the Timer event start.

What is happening now is that the Timer event starts , i am waiting at the "Press Me" button for 10 seconds , before the SecondPanel appears ,

can someone help me resolve this issue

Thanks

Main class used to run the project

package testing;

import java.io.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;


public class Testing extends JPanel 
{



    public static void main(String[] args) 
    {
        frame = new JFrame();
        LoginPanel lp = new LoginPanel();
        frame.add(lp);
        frame.pack();
        frame.validate();
        frame.setVisible(true);

    }

   static JFrame frame;
}

LoginPanel class

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginPanel extends JPanel
{
    LoginPanel()
    {
        Loginbtn = new JButton("Press Me");
        Loginbtn.addActionListener(new LoginButtonListener());
        add(Loginbtn);


    }

     private class LoginButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {

            SecondPanel sp = new SecondPanel();
            Utility.ChangePanel(sp);
            sp.run();
        }
    }

    JButton Loginbtn;

}

SecondPanel class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class SecondPanel extends JPanel
{
    SecondPanel()
    {
        setLayout(new GridLayout(2,2));

        //set deck image
        File deckfile = new File("./src/testing/Ace_Club_1_1.png"); //deck image file location

        try
        {    
          Deckimg = ImageIO.read(deckfile); //read deck image


        }

        catch (IOException e)
        {

        }

         Image scaledInstance = Deckimg.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
         DeckLabel = new JLabel(new ImageIcon(scaledInstance));
         add(DeckLabel);

    }

    public void run()
    {
        Timer timer = new Timer(5000, new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                // Code to be executed
                System.out.println("HowareYou");

            }
        });
        timer.setRepeats(false); // Only execute once
        timer.start(); // Go go go!

        try
        {
             Thread.sleep(7000);
        }



        catch(InterruptedException ie)
        {

        }

    }

    JLabel DeckLabel;
    JPanel DeckPanel;
    BufferedImage Deckimg;

}

Utility class used to switch JPanels within the JFrame

package testing;

import java.security.MessageDigest;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Utility 
{    
    public static void ChangePanel(JPanel jp)
    {


        testing.Testing.frame.getContentPane().removeAll();
        testing.Testing.frame.add(jp);
        testing.Testing.frame.validate();


    }

}
Foi útil?

Solução

You're blocking the Swing event thread in SecondPanel.run() with Thread.sleep(7000). This will stop any GUI updates happening. If you remove the sleep you should see the second panel appear before the timer fires.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top