我想在面板內顯示圖像。所以我將圖像的路徑傳遞給此方法,打開圖像文件並將其傳遞給專用於在面板內繪製圖像的私有類的方法。問題是面板始終是空的,並沒有顯示任何東西。圖像沒有顯示在JPanel中
下面是代碼:
JPanel ImagePane; // I want to add image to this
public void getImagePath(String Path)
{
BufferedImage image = null;
try
{
image=ImageIO.read(new File(Path));
}
catch (IOException e)
{
e.printStackTrace();
}
DisplayImage display= new DisplayImage();
display.getImage(image);
}
private class DisplayImage extends JPanel
{
private BufferedImage image=null;
public void getImage(BufferedImage im)
{
image=im;
repaint();
}
public void paintComponents(Graphics g)
{
g.drawImage(image, 0, 0, image.getWidth() /2, image.getHeight()/2,ImagePane);
}
}
我缺少什麼?
還沒有什麼是displyed –
可能是一個大小問題。 「DisplayImage」的父容器的佈局是什麼? – Reimeus
它是組佈局。 –