当前位置: 代码迷 >> J2SE >> 为什么第一次点击鼠标的时候不画图?该如何解决
  详细解决方案

为什么第一次点击鼠标的时候不画图?该如何解决

热度:71   发布时间:2016-04-24 01:50:06.0
为什么第一次点击鼠标的时候不画图?
我为JPanel添加了一个MouseListener监听,当鼠标点击的时候,就在上面画一个圆,现在第一次点击的时候,画完圆就马上消失了,然后再点击就正常了,这是怎么回事?
代码如下:
Java code
import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFrame;import javax.swing.JPanel;public class MouseRe {    public static void main(String[] args){        JFrame frame = new JFrame();        QQ a =new QQ();        frame.add(a);        frame.setBounds(200, 100, 700, 500);        frame.setVisible(true);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    }class QQ extends JPanel implements MouseListener{    /**     *      */    private static final long serialVersionUID = 1L;    private int i = 30;    private int j = 30;    public QQ(){        addMouseListener(this);    }    @Override    public void mouseClicked(MouseEvent arg0) {        // TODO Auto-generated method stub        System.out.println("tadfa");        picture(i,j);        i = i + 20;        j = j + 20;    }    @Override    public void mouseEntered(MouseEvent arg0) {        // TODO Auto-generated method stub    }    @Override    public void mouseExited(MouseEvent arg0) {        // TODO Auto-generated method stub        }    @Override    public void mousePressed(MouseEvent arg0) {        // TODO Auto-generated method stub    }    @Override    public void mouseReleased(MouseEvent arg0) {        // TODO Auto-generated method stub    }    public void picture(int i,int j){        Graphics g = this.getGraphics();        g.fillOval(i, j, 20,20);    }}


------解决方案--------------------
Java code
import java.awt.Graphics;import java.awt.Point;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.ArrayList;import java.util.List;import javax.swing.JFrame;import javax.swing.JPanel;public class MouseRe {    public static void main(String[] args){        JFrame frame = new JFrame();        QQ a =new QQ();        frame.add(a);        frame.setBounds(200, 100, 700, 500);        frame.setVisible(true);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          }    }class QQ extends JPanel implements MouseListener{    /**     *      */    private static final long serialVersionUID = 1L;    private int i = 30;    private int j = 30;    private List<Point> dots = new ArrayList<Point>();        public QQ(){        addMouseListener(this);    }    @Override    public void mouseClicked(MouseEvent arg0) {                picture(i,j);          i = i + 20;          j = j + 20;    }        @Override     public void paint(Graphics g)    {        super.paint(g);        for(Point p:dots)            g.fillOval(p.x, p.y, 20, 20);    }    @Override    public void mouseEntered(MouseEvent arg0) {        // TODO Auto-generated method stub    }    @Override    public void mouseExited(MouseEvent arg0) {        // TODO Auto-generated method stub        }    @Override    public void mousePressed(MouseEvent arg0) {        // TODO Auto-generated method stub    }    @Override    public void mouseReleased(MouseEvent arg0) {          // TODO Auto-generated method stub          }    public void picture(int i,int j){                dots.add(new Point(i,j));        repaint();    }}
  相关解决方案