不可见的对象,因为重新绘制方法-Java Swing [英] Invisible objects because repaint method - 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问题
推荐答案
-
在运行时不要
add
/remove
JPanels
或其内容,请使用don't
add
/remove
JPanels
or its contents on runtime, use CardLayout instead您的
JPanelFondo p = new JPanelFondo();
与public class JPanelFondoPrincipal extends javax.swing.JPanel {
为了获得更好的帮助,请尽快使用 SSCCE ,
for better help sooner edit your question with an SSCCE,
这篇关于不可见的对象,因为重新绘制方法-Java Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!