简体   繁体   English

如何显示图像(所有主要格式),由JFileChooser在Java中选择

[英]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选择的图像,因此最好的方法是使其可以显示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();
           }
       }
}

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.

相关问题 JFileChooser,它将仅选择Java支持的图像文件格式 - JFileChooser that will only select java supported image file formats 如何 <move selected jpeg image file> 借助JFileChooser到Java swing中的选定文件夹? - How to <move selected jpeg image file> with the help of JFileChooser to a selected folder in java swing? Java:JFileChooser如何在textField中显示所选文件 - Java: JFileChooser How to show Selected File in a textField 如何使用JFileChooser在JFrame中上传和显示图像 - how to upload and display image in JFrame using JFileChooser 如何使用JFileChooser在JPanel中显示图像 - How to use JFileChooser to display image in a JPanel 如何使Java JFileChooser显示所有文件,包括以点开头的文件 - How to make Java JFileChooser display all files including ones starting with a dot 如何通过JFileChooser将所有选定的文件绝对路径/名称设置为JTextField - How to set all selected file absolute paths/names by JFileChooser into a JTextField 如何在Java中使用JFileChooser选择的文件附加字符串 - how to append a string to a file that has been selected with JFileChooser in java 如何使用JFileChooser在Java中获取选定的文件夹名称? - How to Get selected folder name in java using JFileChooser? 如何在Java 9+中使用JFileChooser显示网络共享? - How to display network shares using JFileChooser in Java 9+?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM