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

pipiaiwo 2016-12-05 09:42:09
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() {
}
这一个去掉啊?
...全文
198 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊孩子开学喽 2016-12-08
  • 打赏
  • 举报
回复
这个是安卓开发吧.
舉杯邀明月 2016-12-06
  • 打赏
  • 举报
回复
这是Java的东西,跑到 VB版块来发问,你这是几个意思!!!
赵4老师 2016-12-06
  • 打赏
  • 举报
回复
百度搜相关关键字。
舉杯邀明月 2016-12-06
  • 打赏
  • 举报
回复
乱投医?   我看也够乱的……………… Java与VB之间差异太大,语法、架构上几乎没有共通性, 这论坛不是有个“Java版块”吗!建议你把帖子移过去。
pipiaiwo 2016-12-06
  • 打赏
  • 举报
回复
疾病乱投医,

50,523

社区成员

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

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