简体   繁体   English

在其他JPanel内部重绘JPanel

[英]Repaint JPanel inside other JPanel

How Can I repaint a JPanel that is inside another JPanel? 如何重绘另一个JPanel内的JPanel? I have tried some answers that I found in the internet but I couldn't make it work... There is a piece of my code: 我尝试了一些我在互联网上找到的答案,但是无法正常工作...我的代码有一段:

My Main Panel: 我的主面板:

    contentPane = new JPanel() {
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponents(g);
            try {
                g.drawImage(ImageIO.read(JanelaJogo.class
                        .getResource("/imagens/fundo/fundo4.jpg")), 0, 0,
                        this);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    };
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

This is the panel Inside: 这是内部面板:

bonusPanel = new JPanel() {
        @Override
        public void paintComponents(Graphics g) {
            // TODO Auto-generated method stub
             super.paintComponent(g);                

        }
        @Override
        public Dimension getPreferredSize()
        {
            Dimension dim = contentPane.getSize();
            int largura = dim.width;
            dim = new Dimension(largura, 150);
           return new Dimension(dim);
        }

    };
bonusPanel.setBackground(new Color(0, 0, 0, 0));
contentPane.add(bonusPanel, BorderLayout.SOUTH);

My bonusPanel have 2 labels that are image icons, and I want that image refresh when some user do a combination in the game, the problem is that it isn't refreshing... I have tried repaint() , revalidade() , validade() in contentPane and in bonusPanel but none work... Thank you in advance for the help! 我的bonusPanel具有2个标签,它们是图像图标,我希望当某些用户在游戏中进行组合时刷新图像,问题是它不刷新...我尝试了repaint()revalidade()validade()contentPanebonusPanel但是没有用...谢谢您的帮助!

Update: There is an image and I will explain better what I want... 更新:有一张图片,我会更好地解释我想要的... 在此处输入图片说明

I put in java console, the outputs of the program, so, When there is a match of "bombs", the power (the bomb on the button) should change the image... and im printing to console the current image, as you can see, it changed to image "bonus_bomba_1.png" and "bonus_bomba_2.png", but the problem is refreshing the GUI... I updated the code that I posted before for my current one... I just didn't change the drawImage because I didn't figure out what should I do yet and since it's working, I didn't changed it yet... 我将java控制台放入程序的输出中,因此,当出现“炸弹”匹配项时,电源(按钮上的炸弹)应更改图像...,并通过im打印来控制台当前图像,如下所示:您可以看到,它更改为图像“ bonus_bomba_1.png”和“ bonus_bomba_2.png”,但是问题在于刷新了GUI ...我更新了之前为当前代码发布的代码...我只是没有更改drawImage,因为我还没有弄清楚应该怎么做,并且因为它正在工作,所以我还没有更改它...

This is what I'm doing in the "power" for update the image of the label: 这是我在“电源”中用于更新标签图像的操作:

private void iconBomb(String s) {

    try {
        bombPic = ImageIO.read(this.getClass().getResource(s));
        powerBomb.setIcon(new ImageIcon(bombPic));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

Thank you in advance for the help! 预先感谢您的帮助!

1) Don't invoke super.paintChildren(). 1)不要调用super.paintChildren()。 Swing will do that for you. Swing将为您做到这一点。 All you do is invoke super.paintComponent(); 您要做的只是调用super.paintComponent();。

2) Don't read a File in the painting method. 2)不要使用绘画方法读取文件。 The painting methods are for painting only. 绘画方法仅用于绘画。

3) You are adding the bonus panel to the SOUTH of the frame, so the panel is displayed at its preferred size. 3)您正在将奖金面板添加到框架的南方,因此该面板以其首选尺寸显示。 You need to override the getPreferredSize() method of your panel to return an reasonable size: 您需要重写面板的getPreferredSize()方法以返回合理的大小:

@Override
public Dimension getPreferredSize()
{
    return new Dimension(...);
}

Read the section from the Swing tutorial on Custom Painting for more information and working examples. 阅读有关定制绘画的Swing教程中的部分,以获取更多信息和工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM