由Timer事件添加后,图像不出现在JFrame中

时间:2014-04-26 16:56:54

标签: java image debugging timer jframe

我有一个JFrame,其中包含一个带有JButton“Press Me”的JPanel

enter image description here

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

enter image description here

我有一个Timer功能,它会在JFrame中添加另一张卡,该功能将在6秒后运行。

问题是我必须在卡片出现在JFrame之前展开或最小化JFrame窗口。我希望卡片显示在JFrame中而不扩展或最小化JFrame窗口

有人可以帮助我解决这个问题并向我解释什么是幸福

由于

用于运行项目的主类

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类

package testing;


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
                File Diamondfile = new File("./src/testing/Ace_Diamond_1_1.png"); //deck image file location

        try
        {    
          Diamondimg = ImageIO.read(Diamondfile); //read deck image


        }

        catch (IOException e)
        {

        }

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

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



    }

    JLabel DeckLabel;
    JPanel DeckPanel;
    BufferedImage Deckimg;

    JLabel DiamondLabel;
    JPanel DiamondPanel;
    BufferedImage Diamondimg;



}

用于在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 :(得分:1)

这一行之后:

add(DiamondLabel);

添加:

revalidate();
repaint();

这将告诉包含JPanel重新布局它拥有的所有组件,然后重新绘制自己。