我想在屏幕上随机画10个圆(当点下鼠标时),写了如下的程序代码,能够生成class文件,但就是不能画圆,请高手们指点我下,应该如何改进?import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class Test extends Frame implements ActionListener
{
final int A=10;
final int B=150;
int x[]=new int[10];
int y[]=new int[10];
public int j=0;
Button b1=new Button("清除");
Panel p1=new Panel(new FlowLayout());
public Test()
{
this.add(p1);
p1.add(b1);
b1.addActionListener(this);
this.addMouseListener(new LL());
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});

}
public void actionPerformed(ActionEvent e)
{
j=0;
repaint();
}

public void paint(Graphics g)         
{
int k=0;
for(int i=0;i<A;i++)
{
k=(int)(Math.random()*B);
g.setColor(Color.blue);
g.fillOval(x[j],y[j],k,k);
}
}

public static void main(String args[])
{
Test t1=new Test();
t1.setVisible(true);
}
class LL extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(j<A)
{
x[j]=e.getX();
y[j]=e.getY();
j++;

repaint();}
}
}

}

解决方案 »

  1.   

    改一下
     this.add(p1,BorderLayout.NORTH);
    是不是panel把Frame遮盖的问题?
    我是菜鸟,只是推测。
      

  2.   

    能生成class文件只能够说明程序没有语法错误能够通过编译,但并不能够说明这个class文件就一定可以正常运行并生成正确的结果……
      

  3.   

    //第一处错误,事件加错对象了this 改为 p1
    p1.addMouseListener(new LL());
      

  4.   

    错误太多了,我勉强给你调成可以出图的状态,你再自己慢慢调吧,你要搞清除两件事
    1,在那个组件上面画
    2,那个组件触发事件,那个组件刷新
    public class Test1 extends Frame implements ActionListener {
    final int A = 10;
    final int B = 150;
    int x[] = new int[10];
    int y[] = new int[10];
    public int j = 0;
    Button b1 = new Button("清除");
    // Panel p1 = new Panel(new FlowLayout()); public Test1() {
    // this.add(p1);
    // p1.add(b1);
    // b1.addActionListener(this);
    this.addMouseListener(new LL());//第一处错误,事件加错对象了
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
    }
    }); } public void actionPerformed(ActionEvent e) {
    j = 0;
    repaint();
    } public void paint(Graphics g) {
    int k = 0;
    for (int i = 0; i < A; i++) {
    k = (int) (Math.random() * B);
    g.setColor(Color.blue);
    System.out.println(x[i]+" "+y[i]+" "+ 10+" "+10);
    g.fillOval(x[i], y[i], k, k);
    }
    } public static void main(String args[]) {
    Test1 t1 = new Test1();
    t1.setVisible(true);
    } class LL extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    if (j < A) {
    x[j] = e.getX();
    y[j] = e.getY();
    j++;
    repaint();
    }
    }
    }}
      

  5.   

    为修改简单我把图画到Frame上面了把Panel去掉了.不要看我4楼的说明了,具体你到底想在谁身上画不得而知了,