gpt4 book ai didi

java - JPanel 中的图像没有立即出现

转载 作者:行者123 更新时间:2023-12-02 06:52:54 25 4
gpt4 key购买 nike

我正在使用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));

}

最佳答案

最初绘制组件时可能无法读取图像。尝试:

//g2d.drawImage(panelBack, 0, 0, null);
g2d.drawImage(panelBack, 0, 0, this);

关于java - JPanel 中的图像没有立即出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com