Graphics在Jpanel上画图 出现空指针 怎么回事 求大神啊 !代码如下 在测试类中先设置了setVisible(true)
packagecn.yzg;importjava.awt.Graphics;importjava.util.Stack;importjavax.swing.JFrame;...
package cn.yzg;
import java.awt.Graphics;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle {
int x,y;
Stack <Circle> c;
public Circle(int x,int y){
this.x=x;
this.y=y;
}
public void paint(Circle circle,JPanel panel){
Graphics g = panel.getGraphics(); //此处出错
g.drawOval(x, y, 20, 20);
g.fillOval(x, y, 20, 20);
c.push(circle);
}
public void delete(){
if(!c.isEmpty()){
c.pop();
}
}
}
//这是我的测试类
public class Test extends JFrame{
Mingling m =new PaintCircle();
Request request =new Request();
public Test(){
super();
this.setLayout(new BorderLayout());
this.setSize(200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.add(request.getLabel1(),BorderLayout.NORTH);
this.add(request.getPanel(),BorderLayout.CENTER);
this.add(request.getLabel2(),BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Test();
}
} 展开
import java.awt.Graphics;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle {
int x,y;
Stack <Circle> c;
public Circle(int x,int y){
this.x=x;
this.y=y;
}
public void paint(Circle circle,JPanel panel){
Graphics g = panel.getGraphics(); //此处出错
g.drawOval(x, y, 20, 20);
g.fillOval(x, y, 20, 20);
c.push(circle);
}
public void delete(){
if(!c.isEmpty()){
c.pop();
}
}
}
//这是我的测试类
public class Test extends JFrame{
Mingling m =new PaintCircle();
Request request =new Request();
public Test(){
super();
this.setLayout(new BorderLayout());
this.setSize(200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.add(request.getLabel1(),BorderLayout.NORTH);
this.add(request.getPanel(),BorderLayout.CENTER);
this.add(request.getLabel2(),BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Test();
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询