package Everyday;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class huarongdao extends JFrame implements KeyListener, ActionListener{
Button[] people=new Button[10]; //人物按钮
Button restart; //重玩按钮
JLabel label=new JLabel("曹操到这胜利"); //设置标签
String[] name={"张辽","曹操","曹仁","张飞","关羽","刘备","兵","兵","兵","兵"}; //人物名称标签
Rectangle[] rect=new Rectangle[10]; //人物按钮所在的矩形
int x1,y1; //选中按钮时获取的坐标
JPanel jp=new JPanel(null); //面板,放置棋子(按钮)
//构造方法
public huarongdao(){
this.setLayout(null); //容器为null布局
restart=new Button("重新开始");
restart.addActionListener(this);
restart.setBounds(110,10,70,30); //按钮“重新开始”设置坐标大小
label.setBounds(60,210,80,30); //标签位置
jp.setBounds(50,50,200,250); //面板坐标大小
jp.setBackground(Color.cyan); //面板背景色
for(int i=0;i<name.length;i++){
people[i]=new Button(name[i]); //依次把人物名称添加到按钮
people[i].addKeyListener(this); //人物按钮依次注册鼠标事件
people[i].setBackground(Color.orange); //人物按钮背景色
}
people[0].setBounds(0,0,50,100);
people[1].setBounds(50,0,100,100);
people[2].setBounds(150,0,50,100);
people[3].setBounds(0,100,50,100);
people[4].setBounds(50,100,100,50);
people[5].setBounds(150,100,50,100); //人物按钮设置坐标大小
people[6].setBounds(50,150,50,50);
people[7].setBounds(100,150,50,50);
people[8].setBounds(0,200,50,50);
people[9].setBounds(150,200,50,50);

for(int i=0;i<name.length;i++){
jp.add(people[i]); //向面板中添加人物按钮
}
jp.add(label); //把标签加到面板
this.add(restart); //向容器中添加“重新开始”按钮
this.add(jp); //向容器中添加“面板”
this.setSize(300,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出
}
public void actionPerformed(ActionEvent ae){ //实现“重新开始”按钮
dispose(); //先消毁
new huarongdao(); //然后重新载入
}
public void keyPressed(KeyEvent e) {
Button peo=(Button)e.getSource();
x1=peo.getBounds().x;
y1=peo.getBounds().y;
Rectangle nowrect=peo.getBounds();
if(e.getKeyCode()==KeyEvent.VK_UP){ //按键为上
y1=y1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
for(int i=0;i<10;i++){
rect[i]=people[i].getBounds();
if(peo!=people[i]&&nowrect.intersects(rect[i])){
y1=y1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
} if(peo.getBounds().y<0){
y1=y1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){ //按键为下
y1=y1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
for(int i=0;i<10;i++){
rect[i]=people[i].getBounds();
if(peo!=people[i]&&nowrect.intersects(rect[i])){
y1=y1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(peo.getBounds().y+peo.getBounds().height>250){
y1=y1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){ //按键为左
x1=x1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
for(int i=0;i<10;i++){
rect[i]=people[i].getBounds();
if(peo!=people[i]&&nowrect.intersects(rect[i])){
x1=x1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(peo.getBounds().x<0){
x1=x1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){ //按键为右
x1=x1+50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
for(int i=0;i<10;i++){
rect[i]=people[i].getBounds();
if(peo!=people[i]&&nowrect.intersects(rect[i])){
x1=x1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
if(peo.getBounds().x+peo.getBounds().width>200){
x1=x1-50;
peo.setLocation(x1,y1);
nowrect.setLocation(x1,y1);
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args){
new huarongdao();
}
}
//一个华容道游戏,运行后,下面的兵左右移动时按钮没出现直到没路走又出现,按键盘‘下’‘上’或者按个按钮就出现了,怎么让他移动了就出现呢?