下面这个程序的pg.setColor (Color.RED);
cvs.getGraphics().drawOval(20,20,100,100);
这两行程序无法通过编译,请问是什么原因?import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*; 
import java.awt.event.*;
import javax.swing.*;
public class MPan extends JFrame implements ActionListener  {
JPanel list;
Canvas cvs;
JButton btn1, btn2;
Graphics cvsg, pg;
Image pic;
public MPan() {
list = new JPanel();
this.setLayout(new BorderLayout());
list.setSize(360, 230);
this.add(list, BorderLayout.NORTH);
cvs = new Canvas();
cvs.setBackground(Color.BLACK);
list.setLayout(new BorderLayout());
this.add(cvs, BorderLayout.CENTER);
    
btn1 = new JButton ("BB");
list.add(btn1,BorderLayout.NORTH);
btn1.addActionListener(this);
btn2 = new JButton ("AA");
list.add(btn2,BorderLayout.SOUTH);
cvs.setBackground(Color.BLACK);
pg = cvs.getGraphics();
pg.setColor (Color.RED);
cvs.getGraphics().drawOval(20,20,100,100);
this.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) { 
System.out.println("HER");
//pg.drawOval(200,200,10,10);
//cvs.getGraphics().drawImage(pg,0,0,this);

}
}
public static void main(String args[]) {
MPan d= new MPan();
d.setSize(500,500);
d.setVisible(true);
}}

解决方案 »

  1.   

    首先 楼主这个我不是很懂 我直接复制了你的代码去执行 
    发现当我调用 cvs.getGraphics().drawImage(); 方法的时候 提示的传入的参数依次是Image、int、int、ImageObserver 第一个参数是Image 你这里传入的是pg 但是你在申明的时候pg是 Graphics 可能是这个问题吧我的意见仅供参考 
    关注、等大牛解释
      

  2.   

    这是关于这个方法的说明
    Creates a graphics context for this component. This method will return null if this component is currently not displayable. 没有显示时返回值是null
      

  3.   

    这是关于这个方法的说明
    Creates a graphics context for this component. This method will return null if this component is currently not displayable.  没有显示时返回值是null
    ==========
    能够把这句话解释清楚吗?
      

  4.   

    请问去哪寻找有关getGraphics()方法的说明?
      

  5.   

    cvs.getGraphics().drawImage(pg,0,0,this );中this是MPan类型的,不符合该方法参数要求。
      

  6.   


    以下是j2se API 中关于这个方法的解释
    getGraphics
    public Graphics getGraphics()
    Creates a graphics context for this component. This method will return null if this component is currently not displayable. Returns:
    a graphics context for this component, or null if it has none按字面意思上说就是你应该先用setvisible(true),
    然后再调用getGraphics()这个方法,
    否则会导致返回Null值。