解决方案 »

  1.   

    把监听器写详细一点试试,好久没弄过swing了,记得好像有个左键和右键监听事件吧,你google一下
      

  2.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;public class Exercise16_23 extends JFrame { public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new Exercise16_23();
    frame.pack();//估计也算一次
    frame.setTitle("Exercise16_23");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);//这里绝对是一次
    }

    public Exercise16_23() {
    add(new DrawPoint());
    }

    class DrawPoint extends JPanel {
    private ArrayList<Point> list = new ArrayList<Point>();
    private int x ;
    private int y ;

    public DrawPoint() {
    setPreferredSize(new Dimension(600,600));
    addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    repaint();
    }
    });
    }

    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    x = (int)(Math.random()*400);
    y = (int)(Math.random()*400);

    list.add(new Point(x,y));//你在这里添加Point,每次调用paintComponent都会添加一个新的Point。
    for(int i=0;i<list.size();i++) {
    g.drawOval(list.get(i).x, list.get(i).y, 10, 10);

    }
    System.out.println(list);
    }
    }}
    paintComponent方法不是只有你才能调用的,java自己也会调用。
    只要窗口需要重绘,不管调用的是repaint()还是update(),最后都会调用到这个方法
    比如窗口大小变化,显示消失。
    所以在你点鼠标之前出现了三个点,说明系统已经执行过三次这个方法了。具体哪三次我不清楚。但是窗口出现占了一个。另外两次我估计,只是估计是pack()和setTitle()
      

  3.   

    把这段移到mouseClicked里的repaint()前面:
    x = (int)(Math.random()*400);
                y = (int)(Math.random()*400);
                 
                list.add(new Point(x,y));//你在这里添加Point,每次调用paintComponent都会添加一个新的Point。
      

  4.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;public class Exercise16_23 extends JFrame { public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new Exercise16_23();
    frame.pack();
    frame.setTitle("Exercise16_23");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    public Exercise16_23() {
    add(new DrawPoint());
    }

    class DrawPoint extends JPanel {
    private ArrayList<Point> list = new ArrayList<Point>();
    private int x ;
    private int y ;
    private int count = 0;

    public DrawPoint() {
    setPreferredSize(new Dimension(600,600));
    addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    count++;//这里添加了一个计数器
    repaint();//这里是刷新再画
    }
    });
    }

    protected void paintComponent(Graphics g) {第一次画图跟监听器无关, 重鼠标按下开始, 再重画
    super.paintComponent(g);
    x = (int)(Math.random()*400);
    y = (int)(Math.random()*400);


    if (count > 0) {//当count>0时开始画,
    list.add(new Point(x,y));
    for(int i=0;i<list.size();i++) {
    g.drawOval(list.get(i).x, list.get(i).y, 10, 10);

    }
    System.out.println(list);
    }
    }
    }}希望有用 我也很菜