为什么我无法访问png图像到我的项目的文件夹中?

时间:2013-11-11 09:52:55

标签: java swing jframe jpanel

我有一个简单的Java Swing 应用程序,它显示一个登录表单(一个JFrame),它包含一个带有背景图像的JPanel。

问题在于,如果我使用immage的绝对路径来放置immagage的位置我没有问题但是如果我尝试使用相对路径它因为似乎无法找到 .png 背景immage。

这是我的代码:

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


    }

}

logo.png 文件位于我项目内的资源文件夹中(位于src文件夹的同一级别)。

我尝试访问此资源的行是:

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

,错误是:

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

我该如何解决?

4 个答案:

答案 0 :(得分:1)

尝试不使用正斜杠。适用于从NetBeans和Eclipse运行的

"resources/logo.png" 

答案 1 :(得分:0)

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

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

"/logo.png"

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

  • 源文件夹/src,输出文件夹:/bin,过滤器:除.java以外的所有文件(编译器将创建并复制类文件)
  • 资源文件夹/rsc,输出文件夹:/bin,过滤:所有文件。
  • 汇编/bin的内容已打包成JAR

示例:资源/rsc/images/icons/foo.png将被复制到/bin/images/icons/foo.png,并且它在运行时的路径(以root身份给定'bin',或者打包到JAR中时)将/images/icons/foo.png ,那就是你在运行时加载这个资源的路径。

答案 2 :(得分:0)

我认为下一个问题: 您试图像下一个方式获取图像new ImageIcon("/resources/logo.png")尝试使用下一个代码: getClass().getResource("/resources/logo.png")。我觉得它对你很有帮助。

编辑:要从中创建File,请使用

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

详细了解icons

答案 3 :(得分:0)

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

  

“/ SRC /路径/到/你的/资源/ logo.png”

或相对像这样导航

  

“../资源/ logo.png”

另一种方法是将根目录设置为变量..这可能会在以后使用时派上用场

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