使用java中的Graphics类时候,我继承了JFrame类,然后定义Graphics时,Graphics g=getGraphisc();出错

提示出错,Cannot make a static reference to the non-static method getGraphics() from the type JFrame。如果直接定义Graohics g=new Graphisc()时候又提示Cannot instantiate the type Graphics 怎么解决,急,想要用drawLine方法但是不能用啊

第1个回答  2015-09-22
继承JFrame类以后,实现pant方法,就可以拿到Graphics了追问

怎么实现pant方法,用g还是用JFrame的对象?

追答public class TestMain extends JFrame {

/**
 * 
 */
public TestMain() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(200, 200);
}

@Override
public void paint(Graphics g) {
g.setColor(new Color(0xffffff));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(0));
render(g);
}
}

追问

Graphics g=null;
g.setColor(Color.BLACK);
g.fillRect(0, 0, j.getWidth(), j.getHeight());
为什么我这么写运行后什么都没有了

追答

你确定你这代码没有报空指针?

相似回答