先看代码import java.awt.event.*;
import java.awt.*;
import javax.swing.*;public class AGraphMov extends JFrame
{
Graphics gg;
Circle circle;
GPanel pan=new GPanel();
JButton s=new JButton("Start");
public AGraphMov()
{
super("AGraphMov");
setSize(400,600);
setLayout(null);
pan.setLocation(0,0);
pan.setSize(400,400);
pan.setBackground(Color.RED);
s.setLocation(200,410);
s.setSize(75,30);
add(pan);
add(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
s.addActionListener(new ButtonHandler());
}

class GPanel extends JPanel
{
int x,y,w,h;
int ax,ay;

public GPanel()
{
super();
x=1;y=200;w=60;h=60;
ax=1;ay=-1;
}

public void setOval(int x1,int y1,int w1,int h1)
{
x=x1;y=y1;w=w1;h=h1;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public int getAx()
{return ax;}

public int getAy()
{return ay;}

public void paint(Graphics g)
{
super.paint(g);
g.drawOval(x,y,w,h);
}
}

public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
circle=new Circle();
circle.start();
}
}

class Circle extends Thread
{
public Circle()
{}

public void run()
{
while(pan.getX()+60<400&&pan.getX()>0&&pan.getY()>0&&pan.getY()+60<400)
{
pan.setOval(pan.getX()+pan.getAx(),pan.getY()+pan.getAy(),60,60);
repaint();
try
{
this.sleep(2);
}
catch(InterruptedException ie){}
}
}
}

public static void main(String args[])
{
AGraphMov application=new AGraphMov();
}

}预期的功能就是是GPanel上的圆在红色画布的范围内向的右上方移动,可是运行之后觉得很奇怪:
1.我没有用布局管理器,把GPanel的Location设在了(0,0)的位置,为什么运行程序是(按下按钮之前)GPanel的位置是在偏中间的位置,而JButton的位置却没有问题。
2.按下按钮后,圆形开始向右上方移动,但是一意想不到的是画布也跟着在移!这是为什么?
就是以上的问题,麻烦高手指导!

解决方案 »

  1.   

    呵呵 这个错误我以前也出过
    你把JPanel的getX()和getY()
    覆盖了
    把这两个方法换个名字就好了import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;public class AGraphMov extends JFrame
    {    
        Graphics gg;
        Circle circle;
        GPanel pan=new GPanel();
        JButton s=new JButton("Start");
        public AGraphMov()
        {
            super("AGraphMov");
            setSize(400,600);
            setLayout(null);
            pan.setLocation(0,0);
            pan.setSize(400,400);
            pan.setBackground(Color.RED);
            s.setLocation(200,410);
            s.setSize(75,30);
            add(pan);
            add(s);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            s.addActionListener(new ButtonHandler());
        }
        
        class GPanel extends JPanel
        {    
            int x,y,w,h;
            int ax,ay;
            
            public GPanel()
            {
                super();
                x=1;y=200;w=60;h=60;
                ax=1;ay=-1;
            }
            
            public void setOval(int x1,int y1,int w1,int h1)
            {    
                x=x1;y=y1;w=w1;h=h1;
            }
            
            public int getXX()
            {
                return x;
            }
            
            public int getYY()
            {
                return y;
            }
            
            public int getAx()
            {return ax;}
            
            public int getAy()
            {return ay;}
            
            public void paint(Graphics g)
            {    
                super.paint(g);
                g.drawOval(x,y,w,h);
            }
        }
        
        public class ButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                circle=new Circle();
                circle.start();
            }
        }
        
        class Circle extends Thread
        {    
            public Circle()
            {}
            
            public void run()
            {
                while(pan.getXX()+60<400&&pan.getXX()>0&&pan.getYY()>0&&pan.getYY()+60<400)
                {
                    pan.setOval(pan.getXX()+pan.getAx(),pan.getYY()+pan.getAy(),60,60);
                    repaint();
                    try
                    {
                        this.sleep(2);
                    }
                    catch(InterruptedException ie){}
                }        
            }
        }
        
        public static void main(String args[])
        {
            AGraphMov application=new AGraphMov();
        }
        
    }
      

  2.   

    JFrame默认的布局管理器是BorderLayout,所以运行程序是(按下按钮之前)GPanel的位置是在偏中间的位置