当组件在paintComponent()之外加载图像时,JFrame显示为空白;方法 [英] JFrame appears blank when components are loading images outside the paintComponent(); method

查看:332
本文介绍了当组件在paintComponent()之外加载图像时,JFrame显示为空白;方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这可能听起来很奇怪,这也是我认为它本身就是Java本身的一个错误的原因。

Now this might sound very strange, which is also the reason I think it's a bug in Java itself.

我正在为我的应用程序制作自定义组件。这些组件(范围 JComponent )覆盖 paintComponent(); 方法。出于某种原因,当我在组件中实现图像时,当使用任何这些组件时,框架显示为空白,我做了一些调试,我发现了以下内容:
一旦这个覆盖方法中的代码绘制了一个存储在方法本身之外的变量中的图像,就像非静态类变量一样,框架在显示时将显示为空白,直到它被调整大小。当使用存储在 paintComponent(); 方法本身的变量中的图像时,一切正常。这里发生了什么,我怎么能解决这个问题?我真的需要使用存储在类变量中的图像来缓存这些图像,否则一次又一次地加载每个图像会非常耗费性能。

I'm currently making custom components for my applications. These components (which extent JComponent) overwrite the paintComponent(); method. For some reason the frame appeared blank when any of these componenets was used as soon as I implemented images in the components, I did some debuggin and I found out the following: As soon as the code inside this overwritten method draws an image which was stored in a variable outside of the method itself, like a non-static class variable, the frame will appear blank when shown, until it's being resized. Everything works fine when using images stored in a variable in the paintComponent(); method itself. What happends here, and how am I able to solve this issue? I really need to use images stored in class variable to cache these images, otherwise it would be very performance intensive to load every image again and again.

类似于下面的代码示例工作正常;

Code similar to the folowing example works fine;

public class MyComponent extends JComponent {

    @Override
    public void paintComponenet(Graphics g) {
        Image img = ImageIO.read(getClass().getResource("/res/myImg.png"));
        g.drawImage(img, 0, 0, null);
    }
}

当使用类似的东西时,框架显示为空白;

The frame appears blank when something like this is used;

public class MyComponent extends JComponent {

    private Image img = ImageIO.read(getClass().getResource("/res/myImg.png"));

    @Override
    public void paintComponenet(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }
}

加载图像(在上例中)构造函数或任何其他方法都不起作用。

Loading the image (in the above example) inside the constructor or any other method won't have effect.

注意:组件使用的框架在显示之前(或之后)未打包。这应该没有任何意义,因为它在 paintComponent(); 方法本身中使用变量时工作正常。

NOTE: The frame the components are used in isn't packed before (or after) it's being shown. This shouldn't make any sense though, because it works fine when using variables inside the paintComponent(); method itself.

推荐答案

通过 @trashgod 回答

确保仅在事件派发线程

例如,使用以下代码初始化显示空白的框架,这应该可以解决您的问题;

For example, use the following code to initialize your frame that shows blank, this should solve your problem;

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        new MyFrame();
    }
});

这篇关于当组件在paintComponent()之外加载图像时,JFrame显示为空白;方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆