📄 moveexample.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;
Person(int number,String s)
{
super(s);
this.number=number;
setFont(new Font("宋体",Font.CENTER_BASELINE,14));
setBackground(Color.pink);
addFocusListener(this); //当前按钮注册为本身的监视器。
}
public void focusGained(FocusEvent e) // FocusListener接口方法的声明。
{
setBackground(Color.cyan);
}
public void focusLost(FocusEvent e) // FocusListener接口方法的声明。
{
setBackground(Color.pink);
}
}
class Hua_Rong_Road extends Frame implements KeyListener,MouseListener,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,22,100,25);
restart.addActionListener(this);
String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"};
for(int i=0;i<name.length;i++)
{
person[i]=new Person(i,name[i]);
person[i].addKeyListener(this); //将当前窗口注册为person[i]的KeyEvent事件监视器。
person[i].addMouseListener(this); //将当前窗口注册为person[i]的MouseEvent事件监视器。
add(person[i]);
}
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(); //person[9]获取焦点。
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) //判断是否按下了“↓”键。
{
goDown(man);
}
if(e.getKeyCode()==KeyEvent.VK_UP) //判断是否按下了“↑”键。
{
goUp(man);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT) //判断是否按下了“←”键。
{
goLeft(man);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) //判断是否按下了“→”键。
{
goRight(man);
}
}
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)
{
goDown(man);
}
if(y<h/2)
{
goUp(man);
}
if(x<w/2)
{
goLeft(man);
}
if(x>w/2)
{
goRight(man);
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void goDown(Person man)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
y=y+50;
manRect.setLocation(x,y);
Rectangle belowRect=below.getBounds();
for(int i=0;i<10;i++)
{
Rectangle personRect=person[i].getBounds();
if((manRect.intersects(personRect))&&(man.number!=i))
{
move=false;
}
}
if(manRect.intersects(belowRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void goUp(Person man)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
y=y-50;
manRect.setLocation(x,y);
Rectangle aboveRect=above.getBounds();
for(int i=0;i<10;i++)
{
Rectangle personRect=person[i].getBounds();
if((manRect.intersects(personRect))&&(man.number!=i))
{
move=false;
}
}
if(manRect.intersects(aboveRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void goLeft(Person man)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
x=x-50;
manRect.setLocation(x,y);
Rectangle leftRect=left.getBounds();
for(int i=0;i<10;i++)
{
Rectangle personRect=person[i].getBounds();
if((manRect.intersects(personRect))&&(man.number!=i))
{
move=false;
}
}
if(manRect.intersects(leftRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void goRight(Person man)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
x=x+50;
manRect.setLocation(x,y);
Rectangle rightRect=right.getBounds();
for(int i=0;i<10;i++)
{
Rectangle personRect=person[i].getBounds();
if((manRect.intersects(personRect))&&(man.number!=i))
{
move=false;
}
}
if(manRect.intersects(rightRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{
removeAll();
init();
validate();
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -