我正在尝试做一个画布对象...是作业..但我想制作一个对象
Canvas obj=new Canvas();
但我无法画画..
package canvasobjeto;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
/**
*
* @author Robin
*/
public class CanvasObjeto {
JFrame Frame=new JFrame();
public CanvasObjeto() {
Frame.setTitle("CanvasObjeto");
Frame.setName("CanvasObjeto");
Frame.setSize(300, 300);
Frame.setResizable(false);
Frame.setUndecorated(false);
Frame.setLayout(null);
Frame.setLocationRelativeTo(null);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.getContentPane().setBackground(Color.WHITE);
Formato();
Frame.setVisible(true);
}
private void Formato() {
Canvas lien=new Canvas();
Graphics g= lien.getGraphics();
g.drawLine(0, 0, 20, 20);
lien.setBounds(0, 0, 200, 200);
lien.setBackground(Color.red);
lien.repaint();
Frame.add(lien);
}
public static void main(String[] args) {
CanvasObjeto Ventana=new CanvasObjeto();
}
}
这是错误......我不知道为什么说如果我做了obj ..
canvasobjeto.CanvasObjeto.Formato(CanvasObjeto.java:43)中线程“main”java.lang.NullPointerException中的异常