2 augussmile augussmile 于 2016.05.12 01:20 提问

java 多线程问题,新手求教!!!!!!

public class LinesRectsOvalsJPanel extends JPanel implements Runnable
{
private final int sleeptime=5;
private int x=400;
private int y=0;
private double xspeed=1.0,yspeed=1.0;
public LinesRectsOvalsJPanel(int x,int y,double xspeed,double yspeed){
this.x=x;
this.y=y;
this.xspeed=xspeed;
this.yspeed=yspeed;
}
public int getx(){
return x;
}
public int gety(){
return y;
}
public void setballxy(int x,int y){
this.x=x;
this.y=y;
}
public void paint( Graphics g)
{
super.paint(g);
this.setBackground( Color.WHITE );
g.setColor( Color.MAGENTA );
g.fillOval( this.getx(),this.gety(),20, 20);
} // end method paintComponent
public void run()
{

   while(true)
   {
     if(x + xspeed + 2*20 > 800 || x + xspeed < 0){         //当在X轴上碰到墙时,X轴行进方向改变
               xspeed*=-1;
    }else{
               x += xspeed;                 //没碰壁时继续前进
    }
    if(y + yspeed + 2*20 > 800 || y + yspeed < 0){         //当在Y轴上碰到墙时,Y轴行进方向改变
              yspeed*=-1;
    }else{
              y += yspeed;
    }
    this.repaint();
      try{       
             Thread.sleep(sleeptime); 
         }catch(InterruptedException e){
             e.printStackTrace();
         }
   }

}
} // end class LinesRectsOvalsJPanel
public class LinesRectsOvals
{
// execute application
public static void main( String[] args )
{

  // create frame for LinesRectsOvalsJPanel
  JFrame frame = 
     new JFrame( "Drawing lines, rectangles and ovals" );
  frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  LinesRectsOvalsJPanel linesRectsOvalsJPanel = 
     new LinesRectsOvalsJPanel(400,0,1.0,1.0); 
 LinesRectsOvalsJPanel linesRectsOvalsJPane2 = 
             new LinesRectsOvalsJPanel(700,0,1.0,1.0); 
  linesRectsOvalsJPanel.setBackground( Color.WHITE ); 
  linesRectsOvalsJPane2.setBackground( Color.WHITE );
  ExecutorService threadExecutor=Executors.newCachedThreadPool();

  frame.add( linesRectsOvalsJPanel ); // add panel to frame
  frame.add( linesRectsOvalsJPane2 );
  frame.setSize( 800, 800 ); // set frame size
  frame.setVisible( true ); // display frame
  threadExecutor.execute(linesRectsOvalsJPane2);
  threadExecutor.execute(linesRectsOvalsJPanel);

//  linesRectsOvalsJPane2.ballmove();

} // end main
} // end class LinesRectsOvals
程序中明明初始化了两个弹球,为什么最后运行的时候还是一个。。。新手,不懂。。。

4个回答

augussmile
augussmile   2016.05.12 01:23

图片说明
运行界面是这样。。。。。

CSDNXIAOD
CSDNXIAOD   2016.05.12 01:32

关于Java多线程的synchronized的问题
Java 多线程面试问题
Java多线程面试问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Grindstone_Fos
Grindstone_Fos   2016.05.12 09:02

刚刚跑了一下你的代码,好像是因为
linesRectsOvalsJPane2把linesRectsOvalsJPanel挡住了,
所以你看不到linesRectsOvalsJPanel的小球。
考虑一下JPanel是否能满足您的需求吧。

Grindstone_Fos
Grindstone_Fos 回复augussmile: 结果是这个意思GUI的东西好久没接触了忘了许多了。
一年多之前 回复
augussmile
augussmile 两个小球的起点专门设置不同,是因为JPanel把整个JFrame占据了吗?
一年多之前 回复
havedream_one
havedream_one   2016.05.12 09:06

可以在小球运动的时候println打印这两个小球的轨迹,这样可以看看是不是两个小球再跑,然后再确定查找问题的方向

augussmile
augussmile 好的,我试试,谢谢!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!