本帖最后由 lz12366 于 2009-10-07 21:36:25 编辑

解决方案 »

  1.   

    我发现  这样运行 总是出现  JFrame  的背景是后面桌面上的 图像
    如果把
    public void paint(Graphics g) 
        {         g.drawLine(x1, y1, x2, y2);         x1 = x2;         y1 = y2;     } 
    去掉 则显示正常
    怎么回事呢???
      

  2.   

    一般是用双缓冲去处理,如果不喜欢的话,用下面代码
    import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestTest extends JFrame
    {    /**
    *
    */
    private static final long serialVersionUID = 1L;
    int x1, y1, x2, y2;    public TestTest()
        {        setVisible(true);        setSize(300, 300);
            repaint();        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);
                }
            });        addMouseListener(new MouseAdapter() {            public void mousePressed(MouseEvent e) {                x1 = e.getX();                y1 = e.getY();
                }
            });        addMouseMotionListener(new MouseMotionAdapter() {            public void mouseDragged(MouseEvent e) {                x2 = e.getX();
                    y2 = e.getY();
                    paint();
                    //repaint();            }
            });
        }    public void paint()
        {
         Graphics g=getGraphics();
            g.drawLine(x1, y1, x2, y2);        x1 = x2;        y1 = y2;    }
        public static void main(String args[]) {        new TestTest();
        }
    }
      

  3.   

    jframe用 paint 可以实现
    有时常出现显示不正常