![](/img/trans.png)
[英]JFileChooser that will only select java supported image file formats
[英]How to display an image(all major formats), selected by JFileChooser in java
我正在開發一個圖像編輯應用程序,因此想顯示由JFileChooser
選擇的圖像,因此最好的方法是使其可以顯示jpg
, png
, gif
等所有格式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.