我的代码是这样的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.Timer;
public class chromeNPlayerScreen extends JFrame implements ActionListener{
DrawScreen dPnl = new DrawScreen();
public void actionPerformed(ActionEvent e){
}
public void main(String[ ] args){
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(dPnl);
this.setSize(600,600);;
this.setVisible(true);
this.setResizable(false);
this.setLocation(200, 200);
}
}
但是当我运行它时......
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
有人可以向我解释为什么这不起作用吗?
DrawScreen 代码是
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
public class DrawScreen extends JPanel {
String picPath = "pictures/";
ClassLoader cl = pokemonChromeNewPlayerScreen.class.getClassLoader();
URL imgURL = cl.getResource(picPath+"welcomeBG.png"),imgURL2 = cl.getResource(picPath+"dialogBox.png"),
imgURL3 = cl.getResource(picPath+"Professor.png");
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG, imgDialog, imgProfessor;
public void imgImport(){
imgBG = tk.createImage(imgURL);
imgDialog = tk.createImage(imgURL2);
imgProfessor = tk.createImage(imgURL3);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2 = (Graphics2D)g;
for(int x=0;x<=600;x+=25){
g2.drawLine(x,0,x,600);
g2.drawString(""+x,x+5,20);
}
for(int y=0;y<=600;y+=25){
g2.drawLine(0,y,600,y);
g2.drawString(" "+y,0,y+20);
}
}
}
这是DrawScreen的代码,它所做的一切都是拖动一个网格,但那是因为我刚刚启动它并想要不同位置的x,y值