0

我正在开发一个图像编辑应用程序,所以想显示由 选择的图像JFileChooser,那么最好的方法是什么,以便它可以显示所有格式jpgpng等等gifOpenButton用于调用文件选择器。

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();
           }
       }
}
4

1 回答 1

1

最简单的方法可能是从文件的 URL(或从文件的内容作为字节,或从文件名)创建一个 ImageIcon,并将这个 ImageIcon 包装到 JLabel 中:

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

但是,如果您的应用程序要编辑图像,那么您将必须学习如何操作java.awt.Image实例,而最简单的方法是不够的。

于 2012-06-02T21:05:10.767 回答