此代码给出了一个错误,很可能是它的URL路径对象
package prosjekt_1139;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class HovedVinduet extends JFrame implements ActionListener {
private JPanel hukommelsepanel, hovedPanel, yathzyPanel,menypanel;
private JButton knapp1 = new JButton("Sudoku");
private JButton knapp2 = new JButton("Hukommelse");
private JButton knapp3 = new JButton("Yathzy");
public HovedVinduet() throws IOException {
super("Spillet");
hukommelsepanel = new Hukommelse(this);
yathzyPanel = new Yathzy(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//most likely its the url here that causing the problem
URL path = getClass().getResource("/Image/grass.jpg");
BufferedImage image = ImageIO.read(path);
Background contentPane = new Background(image);
contentPane.setOpaque(true);
contentPane.setLayout(new GridBagLayout());
contentPane.add(knapp1);
contentPane.add(knapp2);
contentPane.add(knapp3);
knapp1.addActionListener(this);
knapp2.addActionListener(this);
knapp3.addActionListener(this);
this.setContentPane(contentPane);
this.setSize(800, 600);
this.setLocation(200,200);
this.setVisible(true);
menypanel = contentPane;
}
public void setAktivtPanel(JPanel aPanel){
this.hovedPanel = aPanel;
this.setContentPane(hovedPanel);
this.pack();
this.setVisible(true);
}
public void setMenyPanelAktivt(){
this.setTitle("Hovedvinduet");
this.setSize(800, 600);
this.setLocation(200,200);
setAktivtPanel(menypanel);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new HovedVinduet();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == knapp1){
new Sudoku().setVisible(true);
}
if (e.getSource() == knapp2){
this.setAktivtPanel(hukommelsepanel);
this.setTitle("Hukommelse");
}
if (e.getSource() == knapp3){
this.setAktivtPanel(yathzyPanel);
this.setTitle("Yathzy");
}
}
}
例外是:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at prosjekt_1139.HovedVinduet.<init>(HovedVinduet.java:32)
at prosjekt_1139.HovedVinduet$1.run(HovedVinduet.java:74)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
以下行返回null:
URL path = getClass().getResource("/Image/grass.jpg");
检查类路径中是否有“Image / grass.jpg”文件。