[英]How to display an image(all major formats), selected by JFileChooser in java
I am developing an image editing app, so want to display an image selected by JFileChooser
, so what would be best approach so that it can display all formats jpg
, png
, gif
etc. OpenButton
is used for invocation of filechooser. 我正在开发一个图像编辑应用程序,因此想显示由
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();
}
}
}
The easiest way is probably to create an ImageIcon from the URL of the file (or from the content of the file as bytes, or from the file name), and to wrap this ImageIcon into a JLabel: 最简单的方法可能是从文件的URL(或从文件的内容(以字节为单位,或从文件名))创建ImageIcon,并将此ImageIcon包装到JLabel中:
iconLabel.setIcon(new ImageIcon(file.toURI().toURL()));
But if your app is supposed to edit the image, then you'll have to learn how to manipulate java.awt.Image
instances, and the easiest way won't be sufficient. 但是,如果您的应用程序应该编辑图像,那么您将不得不学习如何操作
java.awt.Image
实例,而最简单的方法是不够的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.