0
我正在使用getImage
來讀取文件並保存它們,然後將這些圖像設置爲jpanels的背景。但是,當小程序第一次加載時,圖像不可見。只有當我調整大小或上下滾動時,纔會顯示圖像。問題是什麼?JPanel中的圖像沒有立即顯示
@Override
public void init(){
setSize(800, 600);
setLayout(new FlowLayout());
setup();
box1.setText(texts[0]);
box2.setText(texts[1]);
box3.setText(texts[2]);
box4.setText(texts[3]);
add(box1);
add(box2);
add(box3);
add(box4);
add(testPanel);
add(localPanel);
add(background2);
}
public void setup(){
box1 = new JTextArea();
box2 = new JTextArea();
box3 = new JTextArea();
box4 = new JTextArea();
box1.setText(texts[0]);
box2.setText(texts[1]);
box3.setText(texts[2]);
box4.setText(texts[3]);
//*********** this loads immediately **********//
Image back2 = getImage(getDocumentBase(), "blank_blue.png");
background2 = new JLabel(new ImageIcon(back2));
panelBack = getImage(getDocumentBase(), "CardBar.png");
localPanel = new JPanel(){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(panelBack, 0, 0, null);
}
};
localPanel.setPreferredSize(new Dimension(100, 400));
}
您發佈的代碼不會顯示問題的根源。我懷疑是在GUI渲染完成後向GUI添加組件,並且沒有告訴GUI重新驗證和重新繪製容器,但目前我只能猜測。你將需要發佈更多的代碼。 –
@HovercraftFullOfEels我更新了代碼 – rasen58