为什么我不能访问一个png图像到我的项目的文件夹?

时间:2022-02-09 06:25:07

I have a simple Java Swing application that show a login form (a JFrame) that have inside a JPanel with a background immage.

我有一个简单的Java Swing应用程序,它显示了一个在JPanel中具有背景限制的登录表单(JFrame)。

The problem is that if I put the location of the immagage using the absolute path of the immage I have no problem but if I try to use the relative path it go into error because seems that can't find the .png background immage.

问题是,如果我把immagage的位置用了,我没有问题的绝对路径,但是如果我尝试使用相对路径,它会出错,因为似乎找不到。png背景。

This is my code:

这是我的代码:

package com.test.login;

import javax.swing.JButton;

import java.awt.Container;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import net.miginfocom.swt.MigLayout;

import org.jdesktop.application.SingleFrameApplication;

public class LoginFrame2 extends SingleFrameApplication {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame2.class, args);
    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("Inside startup()");


        JFrame mainFrame = this.getMainFrame();         // main JFrame that represents the Windows
        mainFrame.setTitle("XCloud Login");

        mainFrame.setPreferredSize(INITAL_SIZE);
        mainFrame.setResizable(false);

        Container mainContainer = mainFrame.getContentPane();       // main Container into the main JFrame


        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            externalPanel = new JPanelWithBackground("/resources/logo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");

        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        //mainFrame.add(mainContainer);

        show(mainFrame);


    }

}

The logo.png file is into the resources folder inside my project (at the same level of the src folder).

的标志。png文件进入我项目的资源文件夹(在src文件夹的同一级别)。

The line in which I try to access to this resource is:

我试图访问该资源的一行是:

externalPanel = new JPanelWithBackground("/resources/logo.png");

and the error is:

错误的是:

DENTRO: LoginFrame() ---> main()
Inside startup()
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at com.test.login.JPanelWithBackground.<init>(JPanelWithBackground.java:19)
    at com.test.login.LoginFrame2.startup(LoginFrame2.java:52)
    at org.jdesktop.application.Application$1.run(Application.java:187)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
11-nov-2013 10.49.42 org.jdesktop.application.Application$1 run
GRAVE: Application class com.test.login.LoginFrame2 failed to launch
java.lang.NullPointerException
    at com.test.login.LoginFrame2.startup(LoginFrame2.java:58)
    at org.jdesktop.application.Application$1.run(Application.java:187)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class com.test.login.LoginFrame2 failed to launch
    at org.jdesktop.application.Application$1.run(Application.java:192)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.NullPointerException
    at com.test.login.LoginFrame2.startup(LoginFrame2.java:58)
    at org.jdesktop.application.Application$1.run(Application.java:187)
    ... 14 more

How can I solve it?

我该怎么解决呢?

4 个解决方案

#1


1  

Try it without forward slash. Works fine for me running from NetBeans and Eclipse

尝试一下,不要斜线。对我来说,从NetBeans和Eclipse中运行是没问题的。

"resources/logo.png" 

#2


0  

The root of your project is probably even before your "src" directory. Try either setting the path to

您的项目的根可能甚至在您的“src”目录之前。尝试设置路径到。

"/src/path/to/your/resources/logo.png"

“/ src /道路/ /你/资源/ logo.png”

or navigate relatively like this

或者像这样导航。

"../resources/logo.png"

“. . /资源/ logo.png”

Another way is to set the root directory to a variable.. This might come in handy for later use

另一种方法是将根目录设置为变量。这可能迟早会派上用场。

private final String _RESOURCE_DIR = "/src/path/to/resources/";
File png = new File(_RESOURCE_DIR + "logo.png");

#3


0  

What's the output folder for your resources folder?

您的资源文件夹的输出文件夹是什么?

I suppose it's the root of the built application ('bin'-directory, and later the root of the JAR), in that case the resource path would be:

我认为它是构建应用程序的根('bin'-目录,然后是JAR的根),在这种情况下,资源路径是:

"/logo.png"

Usually (depends on the IDE) you can have multiple source and resource folders, and define the output folders for each of them, as well as filters which content should be copied upon a build. Most generic example:

通常(取决于IDE)您可以拥有多个源和资源文件夹,并为每个文件夹定义输出文件夹,以及在构建中复制内容的过滤器。最通用的例子:

  • Source folder: /src, Output folder: /bin, Filter: all but .java (Compiler will create and copy the class files instead)
  • 源文件夹:/src,输出文件夹:/bin,过滤器:all but .java(编译器将创建并复制类文件)
  • Resource folder: /rsc, Output folder: /bin, Filter: all files.
  • 资源文件夹:/rsc,输出文件夹:/bin,过滤器:所有文件。
  • Assembly: Content of /bin is packaged into a JAR
  • 组装:将/bin的内容封装到一个JAR中。

Example: resource /rsc/images/icons/foo.png will be copied to /bin/images/icons/foo.png, and it's path at runtime (given 'bin' as root, or when packaged into a JAR) will be /images/icons/foo.png, that's then the path you use to load this resource at runtime.

例如:资源/ rsc /图片/图标/ foo。png将被复制到/bin/ images/icons/footer。png,它在运行时的路径(给定“bin”作为根,或者打包成一个JAR)将是/图像/图标/foo。png,那是你在运行时用来加载这个资源的路径。

#4


0  

I think problem in next: You try to get image like next way new ImageIcon("/resources/logo.png") try to use next code: getClass().getResource("/resources/logo.png") . I think it helps you.

我认为下一个问题是:你试图获得下一个新ImageIcon(“/resources/logo.png”)的图像,尝试使用下一个代码:getClass(). getresource(“/resources/logo.png”)。我想这对你有帮助。

EDIT: for creating File from it use

编辑:用于创建文件的使用。

    URL resource = getClass().getResource("/resources/logo.png");
    File f = new File(resource.getFile());

Read more about icons.

阅读更多关于图标。

#1


1  

Try it without forward slash. Works fine for me running from NetBeans and Eclipse

尝试一下,不要斜线。对我来说,从NetBeans和Eclipse中运行是没问题的。

"resources/logo.png" 

#2


0  

The root of your project is probably even before your "src" directory. Try either setting the path to

您的项目的根可能甚至在您的“src”目录之前。尝试设置路径到。

"/src/path/to/your/resources/logo.png"

“/ src /道路/ /你/资源/ logo.png”

or navigate relatively like this

或者像这样导航。

"../resources/logo.png"

“. . /资源/ logo.png”

Another way is to set the root directory to a variable.. This might come in handy for later use

另一种方法是将根目录设置为变量。这可能迟早会派上用场。

private final String _RESOURCE_DIR = "/src/path/to/resources/";
File png = new File(_RESOURCE_DIR + "logo.png");

#3


0  

What's the output folder for your resources folder?

您的资源文件夹的输出文件夹是什么?

I suppose it's the root of the built application ('bin'-directory, and later the root of the JAR), in that case the resource path would be:

我认为它是构建应用程序的根('bin'-目录,然后是JAR的根),在这种情况下,资源路径是:

"/logo.png"

Usually (depends on the IDE) you can have multiple source and resource folders, and define the output folders for each of them, as well as filters which content should be copied upon a build. Most generic example:

通常(取决于IDE)您可以拥有多个源和资源文件夹,并为每个文件夹定义输出文件夹,以及在构建中复制内容的过滤器。最通用的例子:

  • Source folder: /src, Output folder: /bin, Filter: all but .java (Compiler will create and copy the class files instead)
  • 源文件夹:/src,输出文件夹:/bin,过滤器:all but .java(编译器将创建并复制类文件)
  • Resource folder: /rsc, Output folder: /bin, Filter: all files.
  • 资源文件夹:/rsc,输出文件夹:/bin,过滤器:所有文件。
  • Assembly: Content of /bin is packaged into a JAR
  • 组装:将/bin的内容封装到一个JAR中。

Example: resource /rsc/images/icons/foo.png will be copied to /bin/images/icons/foo.png, and it's path at runtime (given 'bin' as root, or when packaged into a JAR) will be /images/icons/foo.png, that's then the path you use to load this resource at runtime.

例如:资源/ rsc /图片/图标/ foo。png将被复制到/bin/ images/icons/footer。png,它在运行时的路径(给定“bin”作为根,或者打包成一个JAR)将是/图像/图标/foo。png,那是你在运行时用来加载这个资源的路径。

#4


0  

I think problem in next: You try to get image like next way new ImageIcon("/resources/logo.png") try to use next code: getClass().getResource("/resources/logo.png") . I think it helps you.

我认为下一个问题是:你试图获得下一个新ImageIcon(“/resources/logo.png”)的图像,尝试使用下一个代码:getClass(). getresource(“/resources/logo.png”)。我想这对你有帮助。

EDIT: for creating File from it use

编辑:用于创建文件的使用。

    URL resource = getClass().getResource("/resources/logo.png");
    File f = new File(resource.getFile());

Read more about icons.

阅读更多关于图标。