![](/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.