我正在创建Jfreame应用程序,我遇到了问题。我想创建显示图像的事件。我尝试了一些方法,但它不会工作。 如何解决?
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getName();
String filePath = f.getPath();
String mimetype= new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
if(type.equals("image")){
JPanel imageP = new JPanel();
imageP.setBackground(Color.YELLOW);
BufferedImage image = null;
try {
image = ImageIO.read(new File(filePath));
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon image1 = new ImageIcon(getClass().getResource(filePath));
JLabel label1 = new JLabel(image1);
add(label1);
}
else{
JOptionPane.showMessageDialog(null, "It's NOT an image");
}
}