求助?一个格子游戏的麻烦!

hyl716 2004-05-04 09:19:40
做个格子移动游戏16个格子4行4列!15个数字1个空格 判断空格上下左右来移动
我的问题就是 我向坐和上可以移动下和右不能移动还报异常
大致思路是:
做了个BEAN继承BUTTON 4个SET属性设置格子的4个临近的格子
4个GET属性分别获得4个临近格子的标签
FRAM是卡片布局4-4 格子按顺序排列不动 移动标签 判断被点击格子4周是否有空格子
有 就替换 没有就变
排列是 1 2 3 4
5 6 7 8
9 空格 10 11
12 13 14 15
有几个格子是特例 比如1 他只有 右和下 没有 左和上 所以我分别设置的成1自己!

以下是具体代码 问题部分用!!!!!!!标示出!以下是JBULIDER9

BEAN部分:
public class BtnBean extends JButton implements Serializable{
private BtnBean left,right,up,down;
private String label;

public void setLeft(BtnBean leftBtn){
left=leftBtn;
}
public void setRight(BtnBean rightBtn){
right=rightBtn;
}
public void setUp(BtnBean upBtn){
up=upBtn;
}
public void setDown(BtnBean downBtn){
down=downBtn;
}

public BtnBean getLeft(){
return left;
}
public BtnBean getRight(){
return right;
}
public BtnBean getUp(){
return up;
}
public BtnBean getDown(){
return down;
}

public void setLabel(String lbl){
this.label=lbl;
super.setLabel(lbl);
}

public String getLabel(){
return label;
}

public BtnBean(String lbl) {
super.setLabel(lbl);
try {
this.setLabel(lbl);
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(UIManager.getColor("Tree.selectionBorderColor"));
}
}


FARM部分:
public class TestBean extends JFrame {
GridLayout gridLayout1 = new GridLayout();
BtnBean bbn1,bbn2,bbn3,bbn4,bbn5,bbn6,bbn7,bbn8,bbn9,bbn10,bbn11,bbn12,
bbn13,bbn14,bbn15,bbn16;
!!!!!!!!!!!!!以下是格子移动部分也是问题部分!!!!!!!!!!!
private void find(BtnBean bbn){
String tempString="";
BtnBean tempBbn=null;
tempBbn=bbn.getLeft();
if(tempBbn.getLabel()==null){
tempString=bbn.getLabel();
bbn.setLabel(tempBbn.getLabel());
tempBbn.setLabel(tempString);
return;
}
tempBbn=bbn.getUp();
if(tempBbn.getLabel()==null){
tempString=bbn.getLabel();
bbn.setLabel(tempBbn.getLabel());
tempBbn.setLabel(tempString);
return;
}
tempBbn=bbn.getRight();
if(tempBbn.getLabel()==null){
tempString=bbn.getLabel();
bbn.setLabel(tempBbn.getLabel());
tempBbn.setLabel(tempString);
return;
}
tempBbn=bbn.getDown();
if(tempBbn.getLabel()==null){
tempString=bbn.getLabel();
bbn.setLabel(tempBbn.getLabel());
tempBbn.setLabel(tempString);
return;
}

}
!!!!!!!!!!!!!!以上是问题部分 不能向右和下移动 报指针空错误!!!
public TestBean() {
super("格子游戏");
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestBean testBean = new TestBean();
testBean.setSize(400,400);
testBean.show();
}
private void jbInit() throws Exception {
gridLayout1.setColumns(4);
gridLayout1.setRows(4);
this.getContentPane().setLayout(gridLayout1);
bbn1=new BtnBean("1");
bbn1.setLeft(bbn1);
bbn1.setUp(bbn1);
bbn1.setRight(bbn2);
bbn1.setDown(bbn5);
bbn1.addActionListener(new TestBean_bbn1_actionAdapter(this));
bbn2=new BtnBean("2");
bbn2.setLeft(bbn1);
bbn2.setUp(bbn2);
bbn2.setRight(bbn3);
bbn2.setDown(bbn6);
bbn2.addActionListener(new TestBean_bbn2_actionAdapter(this));
bbn3=new BtnBean("3");
bbn3.setLeft(bbn2);
bbn3.setUp(bbn3);
bbn3.setRight(bbn4);
bbn3.setDown(bbn7);
bbn3.addActionListener(new TestBean_bbn3_actionAdapter(this));
bbn4=new BtnBean("4");
bbn4.setLeft(bbn3);
bbn4.setUp(bbn4);
bbn4.setRight(bbn4);
bbn4.setDown(bbn8);
bbn4.addActionListener(new TestBean_bbn4_actionAdapter(this));
bbn5=new BtnBean("5");
bbn5.setLeft(bbn5);
bbn5.setUp(bbn1);
bbn5.setRight(bbn6);
bbn5.setDown(bbn9);
bbn5.addActionListener(new TestBean_bbn5_actionAdapter(this));
bbn6=new BtnBean("6");
bbn6.setLeft(bbn5);
bbn6.setUp(bbn2);
bbn6.setRight(bbn7);
bbn6.setDown(bbn10);
bbn6.addActionListener(new TestBean_bbn6_actionAdapter(this));
bbn7=new BtnBean("7");
bbn7.setLeft(bbn6);
bbn7.setUp(bbn3);
bbn7.setRight(bbn8);
bbn7.setDown(bbn11);
bbn7.addActionListener(new TestBean_bbn7_actionAdapter(this));
bbn8=new BtnBean("8");
bbn8.setLeft(bbn7);
bbn8.setUp(bbn4);
bbn8.setRight(bbn8);
bbn8.setDown(bbn12);
bbn8.addActionListener(new TestBean_bbn8_actionAdapter(this));
bbn9=new BtnBean(null);
bbn9.setLeft(bbn9);
bbn9.setUp(bbn5);
bbn9.setRight(bbn10);
bbn9.setDown(bbn13);
bbn9.addActionListener(new TestBean_bbn9_actionAdapter(this));
bbn10=new BtnBean("10");
bbn10.setLeft(bbn9);
bbn10.setUp(bbn6);
bbn10.setRight(bbn11);
bbn10.setDown(bbn14);
bbn10.addActionListener(new TestBean_bbn10_actionAdapter(this));
bbn11=new BtnBean("11");
bbn11.setLeft(bbn10);
bbn11.setUp(bbn7);
bbn11.setRight(bbn12);
bbn11.setDown(bbn15);
bbn11.addActionListener(new TestBean_bbn11_actionAdapter(this));
bbn12=new BtnBean("12");
bbn12.setLeft(bbn11);
bbn12.setUp(bbn8);
bbn12.setRight(bbn12);
bbn12.setDown(bbn16);
bbn12.addActionListener(new TestBean_bbn12_actionAdapter(this));
bbn13=new BtnBean("13");
bbn13.setLeft(bbn13);
bbn13.setUp(bbn9);
bbn13.setRight(bbn14);
bbn13.setDown(bbn13);
bbn13.addActionListener(new TestBean_bbn13_actionAdapter(this));
bbn14=new BtnBean("14");
bbn14.setLeft(bbn13);
bbn14.setUp(bbn10);
bbn14.setRight(bbn15);
bbn14.setDown(bbn14);
bbn14.addActionListener(new TestBean_bbn14_actionAdapter(this));
bbn15=new BtnBean("15");
bbn15.setLeft(bbn14);
bbn15.setUp(bbn11);
bbn15.setRight(bbn16);
bbn15.setDown(bbn15);
bbn15.addActionListener(new TestBean_bbn15_actionAdapter(this));
bbn16=new BtnBean("9");
bbn16.setLeft(bbn15);
bbn16.setUp(bbn12);
bbn16.setRight(bbn16);
bbn16.setDown(bbn16);
bbn16.addActionListener(new TestBean_bbn16_actionAdapter(this));
this.getContentPane().add(bbn1);
this.getContentPane().add(bbn2);
this.getContentPane().add(bbn3);
this.getContentPane().add(bbn4);
this.getContentPane().add(bbn5);
this.getContentPane().add(bbn6);
this.getContentPane().add(bbn7);
this.getContentPane().add(bbn8);
this.getContentPane().add(bbn9);
this.getContentPane().add(bbn10);
this.getContentPane().add(bbn11);
this.getContentPane().add(bbn12);
this.getContentPane().add(bbn13);
this.getContentPane().add(bbn14);
this.getContentPane().add(bbn15);
this.getContentPane().add(bbn16);
}

void bbn1_actionPerformed(ActionEvent e) {
//find(bbn1);
// bbn1.setText(bbn3.getText());
}
因为太长了 所以这里就给一个按建的实现 后面删了


...全文
84 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧