2016-07-28 12 views
0

我正在用java編寫一個ID CARD處理應用程序,但是在上傳和顯示客戶端圖像的代碼中存在問題,我在爲圖片創建的標籤中顯示。下面的代碼唯一得到的是圖像路徑,但不是它自己的圖像。如何使用代碼在窗體上載圖像netbeans

這是我迄今嘗試過的代碼。

FileFilter ff = new FileNameExtensionFilter("images","jpeg"); 
     fc.addChoosableFileFilter(ff); 
     int open = fc.showOpenDialog(this); 

     if (open == javax.swing.JFileChooser.APPROVE_OPTION){ 

      java.io.File path = fc.getSelectedFile(); 

      String file_name = path.toString(); 

      pathe.setText(file_name); 

      java.io.File image = fc.getSelectedFile(); 

      ImageIcon photo = new ImageIcon(image.getAbsolutePath()); 
+0

請提供你嘗試到目前爲止的代碼 –

+0

上面的代碼是我迄今爲止,真正需要你的幫助。謝謝 –

回答

0
The code that does the magic is below 

     FileFilter ff = new FileNameExtensionFilter("images","jpeg"); 
     fc.addChoosableFileFilter(ff); 
     int open = fc.showOpenDialog(this); 

     if (open == javax.swing.JFileChooser.APPROVE_OPTION){ 

      java.io.File path = fc.getSelectedFile(); 

      String file_name = path.toString(); 

      pathe.setText(file_name); 

      BufferedImage bi; // bi is the object of the class BufferedImage 
// Now you use try and catch `enter code here` 

try{ 
bi = ImageIO.read(path); // path is your file or image path 
jlabel.setIcon(new ImageIcon(bi)); 
}catch(IOException e){ } 
相關問題