Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像没有';t在JPanel中加载_Java_Image_Swing_File_Java Io - Fatal编程技术网

Java 图像没有';t在JPanel中加载

Java 图像没有';t在JPanel中加载,java,image,swing,file,java-io,Java,Image,Swing,File,Java Io,我有一个JPanel名称“imagePanel”和一个按钮名称“browseBtn”。都包含在JFrame类中。按browseBtn时,将打开文件选择器,选择PNG图像文件后,图像将直接显示在imagePanel中 这是browseBtn的操作事件 private void browseBtnActionPerformed(java.awt.event.ActionEvent evt) { // TODO

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

您应该避免每次显示图像时都创建新对象,想象一下,如果您将其更改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");
        }


    });

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

您应该避免每次要显示图像时创建新对象,想象一下,如果您将其更改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");
        }


    });

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

或者您可以尝试以下方法:

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

或者你可以试试这个:

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

1) 对于初学者,
newimageicon(file.getPath())最好是
新图像图标(文件)2)动态调用添加组件很棘手。我建议在启动时添加
JLabel
,然后简单地调用
l.setIcon(…)
。但是使用ImageIcon(文件)D'Oh时,它会抛出一个错误:“没有为ImageIcon(文件)找到合适的构造函数!”!我的(和先知的)坏。当某个东西应该表示
文件
路径时,它们应该为
文件
提供一个构造函数(该死!)。是的,但我做到了。我用标签代替了面板,效果很好。谢谢你,先生!当我选择分辨率比画框大的图片时,只有一个问题,它似乎有点大,我如何控制它。1)对于初学者,
newimageicon(file.getPath())最好是
新图像图标(文件)2)动态调用添加组件很棘手。我建议在启动时添加
JLabel
,然后简单地调用
l.setIcon(…)
。但是使用ImageIcon(文件)D'Oh时,它会抛出一个错误:“没有为ImageIcon(文件)找到合适的构造函数!”!我的(和先知的)坏。当某个东西应该表示
文件
路径时,它们应该为
文件
提供一个构造函数(该死!)。是的,但我做到了。我用标签代替了面板,效果很好。谢谢你,先生!当我选择分辨率大于边框的图片时,只有一个问题,它似乎有点大,我如何控制它。我制作了它,谢谢,但当我选择分辨率大于边框的图片时,它似乎有点大,我如何控制它?有多种选择。最简单的方法是将标签放在滚动窗格中。更复杂的是调整图像的大小。更复杂的是将图像大小调整到可用空间,然后如果用户将GUI拖得更大或更小,则调整其大小。我制作了它,谢谢,但当我选择分辨率大于边框的图片时,它似乎有点大,我如何控制它?有多种选项。最简单的方法是将标签放在滚动窗格中。更复杂的是调整图像的大小。更复杂的是将图像调整到可用空间,然后在用户将GUI拖得更大或更小时调整其大小。