在更改JPanel之前运行Timer事件

时间:2014-04-26 08:08:32

标签: java swing debugging timer jpanel

我有一个JFrame,其中包含一个带有JButton的JPanel"按我"

enter image description here

按"按我"按钮将更改为同一JFrame中的另一个JPanel(SecondPanel)

enter image description here

我正面临一个问题,当我按下"按我"在SecondPanel出现之前按钮。

此10秒延迟是由Timer事件引起的。

我希望SecondPanel出现在Timer事件开始之前。

现在发生的事情是Timer事件开始,我正在等待#34;按我"按钮10秒,在SecondPanel出现之前,

有人可以帮助我解决此问题

由于

用于运行项目的主类

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类

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类

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;

}

用于在JFrame中切换JPanel的实用程序类

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();


    }

}

1 个答案:

答案 0 :(得分:3)

您使用SecondPanel.run()阻止了Thread.sleep(7000)中的Swing事件线程。这将阻止任何GUI更新发生。如果您取消睡眠,您应该会在计时器触发前看到第二个面板出现。