JFrame 中的图像不想与另一个 class 中的调用一起显示

Image in JFrame doesn't want to display with the call in another class

我使用 Swing 开发 Java 开发软件,但我的代码有问题,我想用 LoadingFrame class 显示图像,这是它的主要工作,但是当我调用构造函数和我的 main class 中的 start() 方法,框架打开但图像不显示(我没有异常)。 为什么它不适用于我的主 class?

public class LoadingFrame
{
    private  JFrame frame;

    public LoadingFrame()
    {
        frame = new JFrame();
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);               
        frame.setUndecorated(true);

        frame.setContentPane(new Panneau());   
    }
    public void start()
    {
        frame.setVisible(true);
    }

    public void stop()
    {
        frame.setVisible(false);
    }

    public static void main(String[] args)
    {
        LoadingFrame l = new LoadingFrame();
        l.start();
        try
        {
            Thread.sleep(3000);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        l.stop();
    }
}

public class Panneau extends JPanel
{
    public void paintComponent(Graphics g)
    {
        System.out.println("hello");

        try 
        {
          Image img = ImageIO.read(new File("Images/loading.png"));
          //g.drawImage(img, 0, 0, this);
          //Pour une image de fond
          g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
        } 
        catch (IOException e) 
        {
          e.printStackTrace();
        }
    }
}

应用 class 是我的主要 class :

public class App {
//Attributes used to display the application
    private JFrame frame;

//Attribute which display a waiting frame
    private static LoadingFrame loadingFrame;

/**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    loadingFrame = new LoadingFrame();
                    loadingFrame.start();

                    App window = new App();

                    loadingFrame.stop();

                    window.frame.setVisible(true);
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        });
    }

/**
     * Create the application.
     */
    public App() 
    {
        initialize();
        synchronizeScriptReferenceList();
        synchronizeTests();
    }
[...]
}

我能够从 App.java 开始使用它。出于某种原因,使用 EventQueue 并不能解决问题。我也尝试使用 SwingUtilities,但这也不起作用。最后,我尝试在主线程中直接删除 App.main 中的 Thready-stuff 运行。出于某种原因,这在其他方法不起作用时有效!这是我的代码:

// In the App class: 

public static void main(String[] args) {
    try {
        loadingFrame = new LoadingFrame();
        loadingFrame.start();

        App window = new App();

        loadingFrame.stop();

        window.frame.setVisible(true);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

当我使用这段代码时,我让它工作了! (出于某种我不知道的原因),这是 Panneau class:

的额外重写
class Panneau extends JPanel
{
    Image img;

    public Panneau() {
        try
        {
            img = ImageIO.read(new File("Images/loading.png"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
}

与此有两个主要区别class;我解决的问题。他们在这里:

  1. 我把super.paintComponent称为我们自己的第一个方法paintComponent
  2. 我只在构造函数中加载一次加载图像,而不是每次我想绘制时都加载图像,这使一切都变得更加顺畅。 (您不希望加载屏幕 CPU 很重,对吗?)

希望通过这些改进,我希望您可以使您的程序运行!它对我有用,所以我祝你好运。

P.S。不要调用 frame.pack(),那是我的错误。出于某种原因,我认为它不适用于未修饰的 windows。