我正在开发一个图像编辑应用程序,因此想显示由JFileChooser
选择的图像,因此最好的方法是使其可以显示所有格式的jpg
,png
,gif
等。用于调用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
实例,而最简单的方法是不够的。关于java - 如何显示图像(所有主要格式),由JFileChooser在Java中选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10865915/