兩天來我試圖從文件中加載圖像到JPanel
。我不能!加載圖像到JPanel不起作用
我用JLabel
和Icon
它的加載沒問題,但我需要直接加載圖像到JPanel
,這是不可能的?
因爲差不多我看到很多這樣的相關問題,很多人推薦問問題的人把圖像加載到標籤中!
這是代碼:
public class ReadingImage extends JPanel {
JPanel panel;
JFrame frame;
JPanel secPanel;
private BufferedImage img;
public ReadingImage(String path){
frame = new JFrame();
frame.setVisible(true);
frame.setLocation(300, 300);
frame.setSize(300, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
secPanel = new JPanel();
secPanel.setVisible(true);
//secPanel.setLayout(new FlowLayout());
secPanel.repaint();
frame.getContentPane().add(secPanel);
try{
FileImageInputStream fi = new FileImageInputStream(new File(path));
//System.out.println(path);
img = ImageIO.read(fi);
this.repaint();
}
catch (IOException io){ io.printStackTrace();}
}
@Override
protected void paintComponent(Graphics g){
super.paintComponents(g);
if (img!=null){
g.drawImage(img, 0, 0, this);
repaint();
}
}
}
它不扔任何異常,但它不是在JPanel
顯示圖像!
我調整引起的擴展JPanel
並使用另一個JPanel的代碼很多,很多次..
在這個任何幫助:)
感謝,
您是否嘗試過這裏提到的解決方案,http://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel – 2015-04-03 11:04:21