不可见的对象,因为重新绘制方法-Java Swing [英] Invisible objects because repaint method - Java Swing

查看:97
本文介绍了不可见的对象,因为重新绘制方法-Java Swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来了,因为我覆盖了jPanel的paintComponent方法,所以当我重新绘制时,所有对象都被隐藏,直到我将它们聚焦. 我需要覆盖paintComponent方法,因为它是我在互联网上找到的唯一更改jFrame背景图像的答案.

The problem comes because I overwrite the paintComponent method of a jPanel, so when I repaint all the objets are hidden till I focus them. I need to overwrite the paintComponent method cause it's the only one answer I'd found in internet to change the background image of a jFrame.

所以首先我创建一个jPanel类:

So firstly I create a jPanel class:

    public class JPanelFondoPrincipal extends javax.swing.JPanel {

    public JPanelFondoPrincipal(){    
        this.setSize(800,500);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Dimension tamanio = getSize();
        ImageIcon imagenFondo = new ImageIcon(getClass().getResource("/images/fondo_principal.png"));        
        g.drawImage(imagenFondo.getImage(),0,0,tamanio.width, tamanio.height, null);        
        setOpaque(false);
    }
}

在我的jPanelForm中:

And in my jPanelForm:

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:
    JPanelFondo p = new JPanelFondo();
    this.add(p);
    validate();
    p.repaint();
}

我已经尝试将所有Objet(标签,textFields ...)添加到新的面板中,以便可以在重新绘制后添加它,并手动设置所有对象可见性,但是一切仍然不可见.

I'd already tried to add all my Objets (labels, textFields...) to a new Panel so I can add it after repaint, and set all the objets visibles manually but everything is still invisible.

非常感谢,我需要在6天之内完成该应用程序,而我一分钟就会发疯了

Many thanks, I need to finish the app in 6 days and I'm getting crazy by the minute

已解决了CARDLAYOUT问题

推荐答案

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