我有一个简单的 Java Swing应用程序,它显示了一个登录表单(一个 JFrame),它在一个带有背景图像的 JPanel 内部。
问题是,如果我使用图像的绝对路径放置图像的位置,我没有问题,但如果我尝试使用相对路径,则会出错,因为似乎找不到.png背景图像。
这是我的代码:
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
我该如何解决?