簡體   English   中英

如何顯示圖像(所有主要格式),由JFileChooser在Java中選擇

[英]How to display an image(all major formats), selected by JFileChooser in java

我正在開發一個圖像編輯應用程序,因此想顯示由JFileChooser選擇的圖像,因此最好的方法是使其可以顯示jpgpnggif等所有格式OpenButton用於調用filechooser。

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
       int returnVal = fileChosser.showOpenDialog(this);
       if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fileChosser.getSelectedFile();
           // What to do with the file
           // I want code for this part
           try {
             //code that might create an exception 
           } 
           catch (Exception e1) {
             e.printStackTrace();
           }
       }
}

最簡單的方法可能是從文件的URL(或從文件的內容(以字節為單位,或從文件名))創建ImageIcon,並將此ImageIcon包裝到JLabel中:

iconLabel.setIcon(new ImageIcon(file.toURI().toURL()));

但是,如果您的應用程序應該編輯圖像,那么您將不得不學習如何操作java.awt.Image實例,而最簡單的方法是不夠的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM