程序:鼠标每点一下,出现一个点,但是只有第一下出现一个点,求解析
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMouseAdapter {
public static void main(String args[]) {
new MyFrame("MouseDrawing...");
}
}
class MyFrame extends Frame {
ArrayList points = null;
MyFrame(String s) {
super(s);
setBounds(400,400,400,400);
setBackground(Color.YELLOW);
setVisible(true);
points = new ArrayList();
addMouseListener(new Monitor());
}
public void paint(Graphics g) {
Iterator i = points.iterator();
if(i.hasNext()) {
Point p = (Point)i.next();
g.setColor(Color.BLUE);
g.fillOval(p.x,p.y,10,10);
}
}
public void addPoint(Point p) {
points.add(p);
}
}
class Monitor extends MouseAdapter {
public void mousePressed(MouseEvent e) {
MyFrame mf = (MyFrame)e.getSource();
mf.addPoint(new Point(e.getX(),e.getY()));
mf.repaint();
}
}
------解决方案--------------------
把你的if(i.hasNext())改成while就行了