2017-04-07 49 views
0

我使用JFileChooser將圖像從桌面加載到JTextArea,但是當我從PC加載圖像時,軟件掛起。無法使用Swing將圖像加載到JTextArea

這裏是文件選擇器的OpenActionPerformed方法的代碼。

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {          
    int returnVal = fileChooser.showOpenDialog(this); 
if (returnVal == fileChooser.APPROVE_OPTION) { 
    File file = fileChooser.getSelectedFile(); 
    try { 
     // What to do with the file, e.g. display it in a TextArea 
     textarea.read(new FileReader(file.getAbsolutePath()), null); 

    } catch (IOException ex) { 
     System.out.println("problem accessing file"+file.getAbsolutePath()); 
    } 
} else { 
    System.out.println("File access cancelled by user."); 
} 

回答

1

JTextArea用於文本而不是圖像。

如果要顯示圖像,請將ImageIcon添加到JLabel,並將標籤添加到JFrame

閱讀Swing教程How to Use Icons中的部分,以獲取有關閱讀圖像和顯示圖標的更多信息。

+0

但我使用Netbeans。沒有選項 –

+1

@ Vipul.Johri首先,我會停止使用netbeans表單編輯器並開始手動編寫你的UI,它會更好地理解API,第二,是的 – MadProgrammer