【问题标题】:Image didn't load in JPanel图像未在 JPanel 中加载
【发布时间】:2017-06-08 10:05:45
【问题描述】:

我有一个 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 中。你能解释一下吗? 干杯。

【问题讨论】:

  • 1) 对于初学者来说,new ImageIcon(file.getPath()); 最好是new ImageIcon(file); 2) 动态添加组件很棘手。我建议改为在启动时添加JLabel,然后只需调用l.setIcon(..)
  • 但在使用 ImageIcon(file) 时会抛出错误:"no suitable constructor found for ImageIcon(File)"
  • 哦!我的(和甲骨文的)不好。当某些东西应该代表File 路径时,它们应该File 提供构造函数(该死!)。
  • 是的,但我做到了。我用标签代替面板来代替,它工作得很好。无论如何,谢谢,先生!选择分辨率比边框大的图片只有一个问题,好像有点大,怎么控制。

标签: java image swing file java-io


【解决方案1】:

您应该避免在每次想要显示图像时都创建新对象,想象一下如果您更改它 5 次,那么您将创建 5 次对象,而您只显示一个!

就像在 cmets 中所说的那样,最好的办法是在创建面板时创建标签,将其添加到所述面板,然后在加载图像时简单地更改此标签的图标。

        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");
        }


    });

假设标签是对所述 JLabel 的引用,在组件初始化时创建。

【讨论】:

  • 我做到了,谢谢,但是当我选择分辨率比框架大的图片时,它似乎有点大,我该如何控制?
  • 有多种选择。最简单的是将标签放在滚动窗格中。更复杂的是调整图像大小。更复杂的是将图像调整到可用空间,然后在用户将 GUI 拖得更大或更小时调整其大小。
【解决方案2】:

或者你可以试试这个:

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");
  }
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2012-12-17
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多