0

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));

}
4

1 回答 1

2

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

//g2d.drawImage(panelBack, 0, 0, null);
g2d.drawImage(panelBack, 0, 0, this);
于 2013-07-23T01:24:59.040 回答