华容道java代码解释 华容道程序( 三 )


button.move("UP",100);
else if(x1==x2y1-y2==-100)
button.move("DOWN",100);
else if(x1-x2==100y1==y2)
button.move("LEFT",100);
else if(x1-x2==-100y1==y2)
button.move("RIGHT",100);
else
return;//不满足就不进行任何处理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//进行是否完成的判断
JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");
for(int i=0;i15;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用
hasAddActionListener=false;
}
}
}
3:
import javax.swing.Icon;
import javax.swing.JButton;
public class Cell extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;
Cell(Icon icon){//实际为ICON
super(icon);
this.setSize(100,100);
}
public void move(String direction,int sleep){//方格的移动
if(direction=="UP"){
this.setLocation(this.getBounds().x,this.getBounds().y-100);
}else if(direction=="DOWN"){
this.setLocation(this.getBounds().x,this.getBounds().y+100);
}else if(direction=="LEFT"){
this.setLocation(this.getBounds().x-100,this.getBounds().y);
}else{
this.setLocation(this.getBounds().x+100,this.getBounds().y);
}
}
}
用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(128,128,128);
Person(int number,String s)//构造方法
{
super(s);//调用父类s华容道java代码解释的构造方法
setBackground(c);//设置组件的背景色
this.number = number;//调用当前的number
c = getBackground();
addFocusListener(this);//添加焦点事件监听器
}
public void focusGained(FocusEvent e)//焦点事件触发
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦点事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person类的数组
Button left,right,above,below;
Button restart = new Button("重新开始");
public Hua_Rong_Road()//华容道的构造方法
{
init(); //初始化
setBounds(100,100,320,360);//设置窗口在屏幕上出现位置华容道java代码解释,和窗口大小
setVisible(true);//设置窗口可见
setResizable(true);//设置窗口可调节
validate();//刷新
addWindowListener( new WindowAdapter()//获得窗口事件监视器
{
public void windowClosing(WindowEvent e)//窗口正在被关闭时华容道java代码解释 , 窗口监视器调用该方法
{
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;kname.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);

推荐阅读