JPanel 在调整 Jframe 大小之前不会更新 [英] JPanel doesn't update until resize Jframe

查看:46
本文介绍了JPanel 在调整 Jframe 大小之前不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了JPanel来覆盖paintComponent(Graphics),我想在jframe中将图像绘制到jpanel上.

I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe.

但是直到我更改 jframe 的大小后,我的图像才显示出来.这是我的代码:

But my image hasn't shown up until I make a change to jframe's size. This is my code:

public class ImagePanel extends JPanel{

    public void setImage(BufferedImage bi)
    {
        image = bi;
        revalidate();
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(image != null)
        {
            g.drawImage(image, 0, 0, this);
        }
    }
}

推荐答案

验证您在添加组件并调用 pack(),如相关示例中所述.您可能还需要采用适当的布局.按照此处的建议调用repaint(),可能会解决症状,但不能解决根本原因.

Verify that you invoke setVisible() after adding components and calling pack(), as discussed in this related example. You may also need to adopt an appropriate layout. Invoking repaint(), as suggested here, may fix the symptom but not the underlying cause.

这篇关于JPanel 在调整 Jframe 大小之前不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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