import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ResourceTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run() {
ResourceFrame frame = new ResourceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ResourceFrame extends JFrame{
public ResourceFrame(){
setSize(500, 300);
URL url = getClass().getResource("speed.gif");
Image img = Toolkit.getDefaultToolkit().getImage(url);
setIconImage(img);
JTextArea area = new JTextArea();
InputStream stream = getClass().getResourceAsStream("student.txt");
Scanner in = new Scanner(stream);
while(in.hasNext())
area.append(in.nextLine()+"\n");
add(area);
}
}
------解决方案--------------------
把speed.gif,student.txt添加到类路径下
------解决方案--------------------
把student.txt和speed.gif放置在该类同目录下就行了。
------解决方案--------------------
和ResourceTest.class放到一起。