最近做了一个小游戏,遇到一些书写问题。贴上原代码,望大神解惑

pipiaiwo 2016-12-05 09:31:07
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

//import com.cc.util.GameButton;

class GameButton extends JButton {
public GameButton(String string) {

//super(str);
super.setSize(5, 5);
super.setBackground(Color.white);
super.setFont(new java.awt.Font("Dialog", 1, 60));// 设置字体
}
}


public class Game4 extends JFrame implements ActionListener {


// 布局器标题
private JFrame f = new JFrame("Game-Java 实现");
// 定义面板,并设置为网格布局,3行3列,组件水平、垂直间距均为10
private JPanel jp = new JPanel(new GridLayout(4, 3, 10, 10));
// 定义按钮数
private GameButton[] gb = new GameButton[9];
// 定义位置关系,且第二维度可变化
private int[][] seatRelation = new int[9][];
// 定义一个保存游戏记录的按钮
private JButton SaveOperation = new JButton("保存游戏操作");
// 保存到文件
private File file = null;

private StringBuffer saveStr = new StringBuffer();





Game4() {

// f.setLocation(10,100);
f.setSize(600, 400);
f.getContentPane().add(jp);

for (int i = 0; i < gb.length; i++) {

gb[i] = new GameButton("-");

jp.add(gb[i]);
gb[i].addActionListener(this); // 添加按钮事件

}
gb[4].setText("+");
SaveOperation.addActionListener(this);
jp.add(SaveOperation);
f.setVisible(true);// 显示按钮,false隐藏按钮

seatRelation[0] = new int[] { 0, 1, 3, 4 };
seatRelation[1] = new int[] { 0, 1, 2 };
seatRelation[2] = new int[] { 1, 2, 4, 5 };
seatRelation[3] = new int[] { 0, 3, 6 };
seatRelation[4] = new int[] { 1, 3, 4, 5, 7 };
seatRelation[5] = new int[] { 2, 5, 8 };
seatRelation[6] = new int[] { 3, 4, 6, 7 };
seatRelation[7] = new int[] { 6, 7, 8 };
seatRelation[8] = new int[] { 4, 5, 7, 8 };


file = new File("E:/newfile.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

public static void main(String[] args) {

Game4 g = new Game4();
}

问题一、为什么补能把class GameButton extends JButton 这个类另外写到一个工具类里,我吧它弄成一个工具类,然后调用,但是我的游戏的一些内容就会缺失啊?
问题二、seatRelation[0] = new int[] { 0, 1, 3, 4 };
seatRelation[1] = new int[] { 0, 1, 2 };
seatRelation[2] = new int[] { 1, 2, 4, 5 };
seatRelation[3] = new int[] { 0, 3, 6 };
seatRelation[4] = new int[] { 1, 3, 4, 5, 7 };
seatRelation[5] = new int[] { 2, 5, 8 };
seatRelation[6] = new int[] { 3, 4, 6, 7 };
seatRelation[7] = new int[] { 6, 7, 8 };
seatRelation[8] = new int[] { 4, 5, 7, 8 };
这一部分我不能放到定义常量的下面吗?我吧它移上去就会报错,用一对{}吧它包起来才不会错。

问题三、’能不能把Game4() {
}
这一个去掉啊?
...全文
148 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pipiaiwo 2016-12-06
  • 打赏
  • 举报
回复
引用 1 楼 shijing266 的回复:
1、你的内容缺失只的是什么,缺失哪些? 2、可以放到静态代码块里面,还有,什么叫移到上面就报错,报什么错? 3、Game4应该是你的构造方法,里面实现了什么你知道吗?
缺失的内容,比如:for (int i = 0; i < gb.length; i++) { gb[i] = new GameButton("-"); jp.add(gb[i]); gb[i].addActionListener(this); // 添加按钮事件 } gb[4].setText("+"); 这部分,我是给gb循环赋值,但是弄成工具类后,“-” 不显示出来了,只有“+” 号了。 移到上面就是这样:public class Game4 extends JFrame implements ActionListener { // 布局器标题 private JFrame f = new JFrame("Game-Java 实现"); // 定义面板,并设置为网格布局,3行3列,组件水平、垂直间距均为10 private JPanel jp = new JPanel(new GridLayout(4, 3, 10, 10)); // 定义按钮数 private GameButton[] gb = new GameButton[9]; // 定义位置关系,且第二维度可变化 private int[][] seatRelation = new int[9][]; // 定义一个保存游戏记录的按钮 private JButton SaveOperation = new JButton("保存游戏操作"); // 保存到文件 private File file = null; private StringBuffer saveStr = new StringBuffer(); seatRelation[0] = new int[] { 0, 1, 3, 4 }; seatRelation[1] = new int[] { 0, 1, 2 }; seatRelation[2] = new int[] { 1, 2, 4, 5 }; seatRelation[3] = new int[] { 0, 3, 6 }; seatRelation[4] = new int[] { 1, 3, 4, 5, 7 }; seatRelation[5] = new int[] { 2, 5, 8 }; seatRelation[6] = new int[] { 3, 4, 6, 7 }; seatRelation[7] = new int[] { 6, 7, 8 }; seatRelation[8] = new int[] { 4, 5, 7, 8 }; 这样他就会要求我用那个{}把上面的赋值给包起来。 我知道Game(){}里面的语句的意思,但是 不知道为什么把他包起来。我也是初级菜鸟一枚。。。囧。
  • 打赏
  • 举报
回复
1、你的内容缺失只的是什么,缺失哪些? 2、可以放到静态代码块里面,还有,什么叫移到上面就报错,报什么错? 3、Game4应该是你的构造方法,里面实现了什么你知道吗?

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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