Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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

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 如何将经过检查的包装中的图像添加到标签、框架等中_Java_Image - Fatal编程技术网

Java 如何将经过检查的包装中的图像添加到标签、框架等中

Java 如何将经过检查的包装中的图像添加到标签、框架等中,java,image,Java,Image,我想在以下文件夹中添加图像:G:\my java\DesktopApplication1\build\classes\DesktopApplication1\resources 如何将此文件夹中的图像添加到我的标签或框架中?您可以执行以下操作: ImageIcon image = new ImageIcon("path_to_image"); JLabel label = new JLabel(image); JLabel l2=new JLabel(""); t

我想在以下文件夹中添加图像:G:\my java\DesktopApplication1\build\classes\DesktopApplication1\resources


如何将此文件夹中的图像添加到我的标签或框架中?

您可以执行以下操作:

ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
         JLabel l2=new JLabel("");
      try {
        Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
        l2.setIcon(new ImageIcon(img));
          }
      catch (IOException ex) {
        }

您可以这样做:

ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
         JLabel l2=new JLabel("");
      try {
        Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
        l2.setIcon(new ImageIcon(img));
          }
      catch (IOException ex) {
        }

我是这样做的:

ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
         JLabel l2=new JLabel("");
      try {
        Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
        l2.setIcon(new ImageIcon(img));
          }
      catch (IOException ex) {
        }

它确实有效,但如果显示GIF动画,我会更喜欢它。然而,如果要显示静态图像,这是一种方法。

我做了如下操作:

ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
         JLabel l2=new JLabel("");
      try {
        Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
        l2.setIcon(new ImageIcon(img));
          }
      catch (IOException ex) {
        }

它确实有效,但如果显示GIF动画,我会更喜欢它。尽管如此,如果要显示静态图像,这是一种方法。

一旦构建,图像通常会在Jar中。Jar中的资源可以通过多种方式访问,这里有一种

URL urlToImage = this.getClass().getResource(
    "/desktopapplication1/resources/the.png");
ImageIcon imageIcon = new ImageIcon(urlToImage);
JLabel ...

一旦建立,图像通常会在一个罐子里。Jar中的资源可以通过多种方式访问,这里有一种

URL urlToImage = this.getClass().getResource(
    "/desktopapplication1/resources/the.png");
ImageIcon imageIcon = new ImageIcon(urlToImage);
JLabel ...

@但是如果显示GIF动画,我会更喜欢它“你做错了什么?”?动画GIF在这里的JLabels中显示得很好。顺便说一句:单词“I”应该总是大写,不管它在句子中的什么位置。@sunandan:如果你在寻找动画gif解决方案,试着看看这个线程:@overbose:我看了这个线程。我已将ImageObserver属性设置为l2,其中添加了动画gif。但是,没有显示任何动画。@sunandan re
catch(IOException ex){}
这在代码中断时非常没有帮助-选择类似于
catch(IOException ex){ex.printStackTrace();}
@overbose:仍然是陌生人;如果我设置了映像的路径(从文件系统复制);我得到了动画。@sunandan“。但如果GIF动画显示为“你做错了什么?”?动画GIF在这里的JLabels中显示得很好。顺便说一句:单词“I”应该总是大写,不管它在句子中的什么位置。@sunandan:如果你在寻找动画gif解决方案,试着看看这个线程:@overbose:我看了这个线程。我已将ImageObserver属性设置为l2,其中添加了动画gif。但是,没有显示任何动画。@sunandan re
catch(IOException ex){}
这在代码中断时非常没有帮助-选择类似于
catch(IOException ex){ex.printStackTrace();}
@overbose:仍然是陌生人;如果我设置了映像的路径(从文件系统复制);我喜欢动画。