最近在做的一个小游戏,遇到一些书写问题。贴上原代码,望大神解惑
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() {
}
这一个去掉啊?
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() {
}
这一个去掉啊?
...全文
相关推荐
旅行商问题 最近插入法
旅行商问题 最近插入法 TSP nearest insertion DEV C++ 29个城市节点
最近,帮一个学生做了一个毕业设计
NULL 博文链接:https://fish198398.iteye.com/blog/367265
实现最近点对问题源的代码(C语言)
解决最近点对问题的源代码,只有一个源文件,可以在Dev C++下直接运行.
分治法求最近点对问题
分治法求最近点对问题,要求:1. 对于平面上给定的N个点,给出所有点对的最短距离,即,输入是平面上的N个点,输出是N点中具有最短距离的两点。 2. 要求随机生成N个点的平面坐标,应用蛮力法编程计算出所有点对的最短距离。 3. 要求随机生成N个点的平面坐标,应用分治法编程计算出所有点对的最短距离。 4. 分别对N=100,1000,10000,100000,统计算法运行时间,比较理论效率与实测效率的差异,同时对蛮力法和分治法的算法效率进行分析和比较。 5. 如果能将算法执行过程利用图形界面输出,可获加分。
最近微信很火的html小游戏安卓版【按一秒】
最近微信上很火的一个小游戏,你的一秒有多长安卓版,完美代码. 代码有点大是因为我加了字体特效。还没有做压缩处理,不过可以根据自己需求修改



