?? moveexample.java
字號:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends JButton implements FocusListener
{
int number;
Color c;
Person(int number,Icon s)
{
super(s);
this.number=number;
c=getBackground();
setFont(new Font("宋體",Font.CENTER_BASELINE,14));
addFocusListener(this); // 當前按鈕注冊為本身的監視器
}
public void focusGained(FocusEvent e) // FocusListener接口方法的聲明
{
setBackground(Color.cyan);
}
public void focusLost(FocusEvent e) // FocusListener接口方法的聲明
{
setBackground(c);
}
}
class Hua_Rong_Road extends JFrame implements KeyListener,MouseListener,ActionListener
{
Person person[]=new Person[10];
JButton left,right,above,below;
JButton restart=new JButton("重新開始");
Container con;
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init()
{
con=getContentPane();
con.setLayout(null);
con.add(restart);
restart.setBounds(100,5,120,25);
restart.addActionListener(this);
Icon icon[]={new ImageIcon("a.jpg"),new ImageIcon("b.jpg"),new ImageIcon("c.jpg"),new ImageIcon("d.jpg"),new ImageIcon("e.jpg"),new ImageIcon("f.jpg"),new ImageIcon("g.jpg"),new ImageIcon("g.jpg"),new ImageIcon("g.jpg"),new ImageIcon("g.jpg")};
for(int i=0;i<10;i++)
{
person[i]=new Person(i,icon[i]);
person[i].addKeyListener(this); // 將當前窗口注冊為person[i]的KeyEvent事件監視器
person[i].addMouseListener(this); // 將當前窗口注冊為person[i]的MouseEvent事件監視器
con.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].requestFocusInWindow(); // person[9]獲取焦點
left=new JButton();
right=new JButton();
above=new JButton();
below=new JButton();
con.add(left);
con.add(right);
con.add(above);
con.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);
con.validate();
}
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 keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
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)
{
con.removeAll();
init();
validate();
repaint();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -