红蓝按钮交替移动

来源:互联网 发布:机器人保姆知乎 编辑:程序博客网 时间:2024/04/30 15:15

编写一个应用程序,除了主线程外,还有两个线程:first和second。first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。另外还有一个start按钮,当点击start按钮后,红色按钮平行移动从左边移动到右边,当红色按钮移动到绿色按钮位置后,红色按钮停止在绿色按钮起始位置,然后绿色按钮接着移动。当绿色按钮移动到指定位置后,所有按钮位置重置,然后循环执行上述过程。

importjava.awt.Button;

importjava.awt.Color;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

 

importjavax.swing.JFrame;

 

public classMoveButton extends JFrame implements Runnable, ActionListener {

   

    private int distance=10;

 

    Thread first,second;

    Button redButton,greenButton,startButton;

 

    /*

     * 构造方法进行初始化

     */

    public MoveButton(){

      

       /*

        * 创建线程

        */

       first=new Thread(this);

       second=new Thread(this);

      

       setLayout(null);//清除默认布局

      

       /*

        * 设置红色按钮的颜色

        * 设置起始位置与大小

        * 加入窗体

        */

       redButton=new Button();

       redButton.setBackground(Color.red);

       redButton.setBounds(10, 60, 15, 15);

       add(redButton);

      

       /*

        * 设置红色按钮的颜色

        * 设置起始位置与大小

        * 加入窗体

        */

       greenButton=new Button();

       greenButton.setBackground(Color.green);

       greenButton.setBounds(100,60,15,15);

       add(greenButton);

          

 

       /*

        * 设置开始按钮的起始位置与大小

        * 添加监听器

        * 加入窗体

        */

       startButton=newButton("start");

       startButton.addActionListener(this);

       startButton.setBounds(10, 100, 30, 30);

       add(startButton);

      

       setBounds(0, 0, 300, 200);//设置窗体的起始位置与长宽

       setVisible(true);//设置窗体可见

      

       /*

        * 关闭窗时释放内存资源

        */

       addWindowListener(new WindowAdapter() {

           public void windowClosing(WindowEvente){

              System.exit(0);

           }  

       });

    }

    /*

     * 实现按钮的移动

     *synchronized 方法控制对类成员变量的访问

     */

    private synchronized void moveComponent(Button button) {

      

       if(Thread.currentThread() == first){

           while(distance>100 &&distance<=200){

              try {

                  wait();

                  System.out.println("你好");//线程等待,直到其他线程调用notify或notifyAll方法通知该线程醒来

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

           }

           distance+=1;

           button.setLocation(distance, 60);

           if(distance>=100){

               if(distance<=200){

                  button.setLocation(100, 60);//在蓝色按钮运动期间红色按钮始终位于蓝色按钮最初位置

              }else{

                  button.setLocation(10, 60);//当距离移动距离大于200时,蓝色按钮归位

              }

              notify();//唤醒单个等待的线程(由于约束条件的存在,此程序中势必只有一个等待的线程,故可用此方法替换)

              //notifyAll();//唤起全部等地的线程

           }

       }

      

       if(Thread.currentThread() == second){

           while(distance>=10 &&distance<100){

              try {

                  wait();//线程等待,直到其他线程调用notify或notifyAll方法通知该线程醒来

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

           }

           distance+=1;

           button.setLocation(distance, 60);

           if(distance>200){

              button.setLocation(100, 60);//当距离移动距离大于200时,蓝色按钮归位

              distance=10;//distance置初值

              notify();//

           }

       }

      

    }

   

    /*

     *赋写Run()方法

     */

    public void run() {

      

       while(true){

           /*

            * 判断当前执行的线程是否为first

            * 如果是,调用moveComponent()方法,移动redButton

            * 线程睡眠20ms

            */

           if(Thread.currentThread()==first){

              moveComponent(redButton);

              try {

                  Thread.sleep(20);

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

           }

           /*

            * 判断当前执行的线程是否为second

            * 如果是,调用moveComponent()方法,移动greenButton

            * 线程睡眠10ms

            */

           if(Thread.currentThread()==second){

              moveComponent(greenButton);

              try {

                  Thread.sleep(10);

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

           }

       }

    }

   

    /*

     * 事件监听,启动线程(由于只对startButton按钮绑定监听器,所以默认监听该按钮)

     */

    public void actionPerformed(ActionEvent e) {

   

       try {

           first.start();//启动线程

           second.start();

       } catch (Exception e1) {

           e1.printStackTrace();

       }

 

    }

   

    public static void main(String[] args) {

       new MoveButton();

    }

}


0 0