双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

pipiaiwo pipiaiwo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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() {
}
这一个去掉啊?
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
pipiaiwo
Java 51,407 成员 86,134 内容