[英]Invisible objects because repaint method - Java Swing
出現問題是因為我覆蓋了jPanel的paintComponent方法,所以當我重新繪制時,所有對象都被隱藏,直到我將它們聚焦為止。 我需要覆蓋paintComponent方法,因為它是我在互聯網上找到的唯一更改jFrame背景圖像的答案。
所以首先我創建一個jPanel類:
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中:
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 ...)添加到新面板中,以便可以在重新繪制后添加它,並手動設置所有objets可見性,但所有內容仍然不可見。
非常感謝,我需要在6天之內完成該應用程序,而我此刻正變得瘋狂
編輯:解決了卡布局問題
Swing程序應該重寫paintComponent()而不是重寫paint()。
http://java.sun.com/products/jfc/tsc/articles/painting/
並且您應該調用super.paintComponent(g);
首先在重寫的paintComponent()中;
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);
}
這是處理在JPanel組件上繪畫的正確方法。
不要在運行時add
/ remove
JPanels
或其內容,而應使用CardLayout
您的JPanelFondo p = new JPanelFondo();
與public class JPanelFondoPrincipal extends javax.swing.JPanel {
不對應, public class JPanelFondoPrincipal extends javax.swing.JPanel {
為了獲得更好的幫助,請盡快使用SSCCE編輯您的問題,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.