当前位置:编程学习 > JAVA >>

求Java程序源代码注释(越详细越好~)

import java.awt.*;

import java.awt.event.*;

public class MoveExample

{  public static void main(String args[])

   {  new Hua_Rong_Road();

   }

}

class Person extends Button

implements FocusListener 

{  int number;

   Color c=new Color(255,245,170);

   Person(int number,String s)

   {  super(s);

      setBackground(c);

      this.number=number;

      c=getBackground();

      addFocusListener(this);

   }

   public void focusGained(FocusEvent e)

   {  setBackground(Color.red);

   }

   public void focusLost(FocusEvent e)

   {  setBackground(c);

   }

}

class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener

{  Person person[]=new Person[10];

   Button left,right,above,below; 

   Button restart=new Button("重新开始");

   public Hua_Rong_Road()

   { init();

     setBounds(100,100,320,360);

     setVisible(true);

     validate();

     addWindowListener( new WindowAdapter()

                         {  public void windowClosing(WindowEvent e)

                             {  System.exit(0);

                             }

                         }

                       );

   }

   public void init()

   {  setLayout(null);

      add(restart);

      restart.setBounds(100,320,120,25);

      restart.addActionListener(this);

      String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"};

      for(int k=0;k<name.length;k++)

      {  person[k]=new Person(k,name[k]);

         person[k].addMouseListener(this);

         person[k].addKeyListener(this);

         add(person[k]);

      }

      person[0].setBounds(104,54,100,100);

      person[1].setBounds(104,154,100,50);

      person[2].setBounds(54, 154,50,100);

      person[3].setBounds(204,154,50,100);

      person[4].setBounds(54, 54, 50,100);

      person[5].setBounds(204, 54, 50,100); 

      person[6].setBounds(54,254,50,50); 

      person[7].setBounds(204,254,50,50);

      person[8].setBounds(104,204,50,50);

      person[9].setBounds(154,204,50,50);

      person[9].requestFocus();

      left=new Button();  right=new Button();

      above=new Button(); below=new Button();

      add(left);  add(right);

      add(above); add(below);

      left.setBounds(49,49,5,260);

      right.setBounds(254,49,5,260);

      above.setBounds(49,49,210,5);

      below.setBounds(49,304,210,5);

      validate();

   }

   public void keyTyped(KeyEvent e){}

   public void keyReleased(KeyEvent e){}

   public void keyPressed(KeyEvent e)

   {  Person man=(Person)e.getSource();

     if(e.getKeyCode()==KeyEvent.VK_DOWN)

     {  go(man,below);

     }

     if(e.getKeyCode()==KeyEvent.VK_UP)

     {  go(man,above);

     }

     if(e.getKeyCode()==KeyEvent.VK_LEFT)

     {  go(man,left);

     }

     if(e.getKeyCode()==KeyEvent.VK_RIGHT)

     {  go(man,right);

     }

   }

   public void mousePressed(MouseEvent e)

   { Person man=(Person)e.getSource();

     int x=-1,y=-1;

     x=e.getX();

     y=e.getY();

     int w=man.getBounds().width;

     int h=man.getBounds().height;

     if(y>h/2)

     { go(man,below);

     }

     if(y<h/2)

     { go(man,above);

     }  

     if(x<w/2)

     { go(man,left);

     }

     if(x>w/2)

     { go(man,right);

     }

   }

   public void mouseReleased(MouseEvent e) {}

   public void mouseEntered(MouseEvent e)  {}

   public void mouseExited(MouseEvent e)   {}

   public void mouseClicked(MouseEvent e)  {}

   public void go(Person man,Button direction)

   {  boolean move=true;

      Rectangle manRect=man.getBounds();

      int x=man.getBounds().x;

      int y=man.getBounds().y;

      if(direction==below)

         y=y+50;

      else if(direction==above)

         y=y-50;

      else if(direction==left)

         x=x-50;

      else if(direction==right)

         x=x+50;

      manRect.setLocation(x,y);

      Rectangle directionRect=direction.getBounds();

      for(int k=0;k<10;k++)

      {  Rectangle personRect=person[k].getBounds();

         if((manRect.intersects(personRect))&&(man.number!=k))

           {  move=false;

           }

      }

      if(manRect.intersects(directionRect))

      {    move=false;

      }

      if(move==true)

      {    man.setLocation(x,y);

      }

   }

  public void actionPerformed(ActionEvent e)

   {  dispose();

      new Hua_Rong_Road();

   }

答案:

import java.awt.*;   //导包

import java.awt.event.*;

public class MoveExample

{  public static void main(String args[])   //MAIN函数 新建Hua_Rong_Road()类

   {  new Hua_Rong_Road();

   }

}

class Person extends Button    //类Person 继承 Button 并实现易做图FocuListener

implements FocusListener 

{  int number;  //定义正型变量number

   Color c=new Color(255,245,170);

   Person(int number,String s)   //Person类的构造函数

   {  super(s);

      setBackground(c); //  设置背景

      this.number=number;  //类的变量赋值

      c=getBackground();

      addFocusListener(this);  //添加易做图

   }

   public void focusGained(FocusEvent e)  //方法focusGained用来设置背景

   {  setBackground(Color.red);

   }

   public void focusLost(FocusEvent e)

   {  setBackground(c);

   }

}

class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener

//  类Hua_Rong_Road 继承frame实现集中易做图

{  Person person[]=new Person[10];   //新建Person列实体数组

   Button left,right,above,below;     //定义按钮

   Button restart=new Button("重新开始");

   public Hua_Rong_Road()   //类Hua_Rong_Road 的构造函数对类进行实例化

   { init();

     setBounds(100,100,320,360);

     setVisible(true);

     validate();//刷新窗口

     addWindowListener( new WindowAdapter()

                         {  public void windowClosing(WindowEvent e)

                             {  System.exit(0);  //退出窗口

                             }

                         }

                       );

   }

   public void init()   //初始化方法

   {  setLayout(null);

      add(restart);

      restart.setBounds(100,320,120,25);

      restart.addActionListener(this);

      String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"};

      for(int k=0;k<name.length;k++)

      {  person[k]=new Person(k,name[k]);

         person[k].addMouseListener(this);

         person[k].addKeyListener(this);

         add(person[k]);

      }

      person[0].setBounds(104,54,100,100);

      person[1].setBounds(104,154,100,50);

      person[2].setBounds(54, 154,50,100);

      person[3].setBounds(204,154,50,100);

      person[4].setBounds(54, 54, 50,100);

      person[5].setBounds(204, 54, 50,100); 

      person[6].setBounds(54,254,50,50); 

      person[7].setBounds(204,254,50,50);

      person[8].setBounds(104,204,50,50);

      person[9].setBounds(154,204,50,50);

      person[9].requestFocus();

      left=new Button();  right=new Button();

      above=new Button(); below=new Button();

      add(left);  add(right);

      add(above); add(below);

      left.setBounds(49,49,5,260);

      right.setBounds(254,49,5,260);

      above.setBounds(49,49,210,5);

      below.setBounds(49,304,210,5);

      validate();

   }

   public void keyTyped(KeyEvent e){}

   public void keyReleased(KeyEvent e){}

   public void keyPressed(KeyEvent e)  ///事件处理  添加键盘事件

   {  Person man=(Person)e.getSource();

     if(e.getKeyCode()==KeyEvent.VK_DOWN)

     {  go(man,below);

     }

     if(e.getKeyCode()==KeyEvent.VK_UP)

     {  go(man,above);

     }

     if(e.getKeyCode()==KeyEvent.VK_LEFT)

     {  go(man,left);

     }

     if(e.getKeyCode()==KeyEvent.VK_RIGHT)

     {  go(man,right);

     }

   }

   public void mousePressed(MouseEvent e)

   { Person man=(Person)e.getSource();

     int x=-1,y=-1;

     x=e.getX();

     y=e.getY();

 &

上一个:java面向对象程序设计做个简单是介绍啊谢谢
下一个:java中refactor是什么意思,有什么作用?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,