package chapter10;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ResourceTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
ResourceTestFrame resourceTestFrame=new ResourceTestFrame();
resourceTestFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
resourceTestFrame.setVisible(true);
}
});
}
}
class ResourceTestFrame extends JFrame{
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public ResourceTestFrame(){
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
URL aboutURL=getClass().getResource("about.png");//import java.net.URL;
Image img=new ImageIcon(aboutURL).getImage();
setIconImage(img);
JTextArea testArea=new JTextArea();
InputStream stream=getClass().getResourceAsStream("about.text");
Scanner in=new Scanner(stream);
while(in.hasNext())testArea.append(in.nextLine()+"\n");
add(testArea);
}
}
抛出的异常有
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at java.util.Scanner.<init>(Scanner.java:608)
at chapter10.ResourceTestFrame.<init>(ResourceTest.java:35)
at chapter10.ResourceTest$1.run(ResourceTest.java:17)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
这个程序就是读取图片和文本,
我把图片,文本和包放在一起的,
请看图片的左边
------解决方案--------------------
要么只有about,没有后缀名,你自己看看那个about文件有没有后缀名不会吗。。
再说哪有text这种后缀名的。。