下面是我写的一个类,把它添加到面板上却什么也不显示,请帮忙看一下错在哪!!!class ChessCanvas extends Canvas {
public void paint(Graphics g){
int w=getSize().width;
int h=getSize().height;
for(int i=1;i<16;i++){
g.drawRect(i*w/16,h/16,15*w/16,15*h/16);
g.drawRect(h/16,i*h/16,15*h/16,15*w/16);
}
}
}Email:[email protected]

解决方案 »

  1.   

    我试了一下,可以显示,虽然你画得比较糟糕。。代码:import java.awt.*;
    import java.awt.event.*;public class MyFrame extends Frame implements ActionListener
    {

    ChessCanvas can=new ChessCanvas();
    //TextArea testarea=new TextArea();
    Panel panel1=new Panel();
    Panel panel2=new Panel();
    Button read=new Button("read");
    Button clear=new Button("clear");
      
        public MyFrame()
        {
            
    //this.read();
    this.setLayout(new BorderLayout());
    this.add("Center", panel1);
    this.add("South",panel2);
    panel1.setLayout(new BorderLayout());
    panel1.add("Center", can);

    panel2.setLayout(new FlowLayout());
    panel2.add(read);
    panel2.add(clear);
    read.addActionListener(this);
    clear.addActionListener(this);

    validate();
    setSize(800,600);
    setVisible(true);



    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
        }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==read)
    {
        

    }
    else if(e.getSource()==clear)
    {

        }

        
    }


    public static void main(String args[])
    {
    MyFrame test =new MyFrame();


    }
    }