我正在使用getImage
来读取文件并保存它们,然后将这些图像设置为jpanels的背景。但是,首次加载小程序时,图像不可见。只是,如果我调整大小或向上和向下滚动,则会显示图像。有什么问题?
@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));
}
答案 0 :(得分:2)
最初绘制组件时可能无法读取图像。尝试:
//g2d.drawImage(panelBack, 0, 0, null);
g2d.drawImage(panelBack, 0, 0, this);