图像没有加载到JPanel中

时间:2017-06-08 10:05:46

标签: java image swing file java-io

我有一个JPanel名称" imagePanel"和一个按钮名称" browseBtn"。全部包含在JFrame类中。当按下browseBtn时,文件选择器将打开,选择PNG图像文件后,图像将直接出现在imagePanel中。

这是browseBtn

的动作事件
private void browseBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
      JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(null);
    if (result == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        if (accept(file)) {
            try {
                ImageIcon image = new ImageIcon(file.getPath());
                JLabel l = new JLabel(image);
                imagePanel.add(l);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Error reading file !");
            }
        }
        else {
            JOptionPane.showMessageDialog(this, "Choose png file only !");
        } 
    }

}                                         

public boolean accept(File file) {
    return file.isDirectory() || file.getAbsolutePath().endsWith(".png");
}

我选择了正确的.png文件,但我不明白为什么图片没有显示在imagePanel中。你可以解释一下吗? 干杯。

2 个答案:

答案 0 :(得分:2)

每次想要显示图像时都应该避免创建新对象,想象一下如果你改变它5次,你只需要创建一个对象就会创建5倍!

如评论中所述,您最好的方法是在创建面板时创建标签,将其添加到所述面板,然后在加载图像时更改此标签的图标。

        browseBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            int result = fc.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                if (accept(file)) {
                    try {
                        ImageIcon image = new ImageIcon(file.getPath());
                        label.setIcon(image);
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(this, "Error reading file !");
                    }
                }
                else {
                    JOptionPane.showMessageDialog(this, "Choose png file only !");
                } 
            }
        }

        public boolean accept(File file) {
            return file.isDirectory() || file.getAbsolutePath().endsWith(".png");
        }


    });

假设label是对组件初始化时创建的JLabel的引用。

答案 1 :(得分:0)

或者你可以试试这个:

browseBtn.addActionListener(new ActionListener() {
@Override
 public void actionPerformed(ActionEvent e) {
  JFileChooser fc = new JFileChooser();
  int result = fc.showOpenDialog(null);
  if (result == JFileChooser.APPROVE_OPTION) {
  File file = fc.getSelectedFile();
  if (accept(file)) {
  try {
   ImageIcon imageIcon = new ImageIcon(new   ImageIcon(file.getPath()).getImage().getScaledInstance(20, 20,    Image.SCALE_DEFAULT)); //resizing
   label.setIcon(imageIcon);

  /*try { // or try this
     InputStream inStream =  this.getClass().getClassLoader().getResourceAsStream(file.getPath());
     BufferedImage img = ImageIO.read(inStream);
     Image rimg = img.getScaledInstance(width, height,  Image.SCALE_STANDARD);
     label.setIcon(rimg);
    } catch (IOException e) {}*/
  } catch (Exception ex) {JOptionPane.showMessageDialog(this, "Error reading file !");}
  } else {JOptionPane.showMessageDialog(this, "Choose png file  only  !");} 
 }
 }
  public boolean accept(File file) {
   return file.isDirectory() || file.getAbsolutePath().endsWith(".png");
  }
 });
相关问题