《通过一个游戏编程熟悉事件处理》http://blog.csdn.net/flyuniverse_shell/archive/2009/10/23/4719939.aspx
已经完成了目标一,今天我们学习了文件操作,那么我们可以接着完成目标二。代码如下:
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.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class GameButton extends JButton
{
GameButton(String str)
{
super(str);
super.setSize(10, 10);
super.setBackground(Color.WHITE);
super.setFont(new java.awt.Font("Dialog",1,60));//设置字体
}
}
public class MyGame extends JFrame implements ActionListener
{
private JFrame f = new JFrame("Game-Java 实现");
private JPanel jp = new JPanel(new GridLayout(4,3,10,10));
private GameButton [] gb = new GameButton[9];
private int[][] relationShips = new int[9][];
private JButton JBSaveOperation = new JButton("保存操作记录");
File fileSave = null; //游戏记录文件
StringBuffer tempSaveStrB=new StringBuffer(); //暂时保存操作,最终会保存到fileSave中去
MyGame()
{
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("+");
JBSaveOperation.addActionListener(this);
jp.add(JBSaveOperation);
f.setVisible(true);
relationShips[0] = new int[] { 0, 1, 3, 4 };
relationShips[1] = new int[] { 0, 1, 2 };
relationShips[2] = new int[] { 1, 2, 4, 5 };
relationShips[3] = new int[] { 0, 3, 6 };
relationShips[4] = new int[] { 1, 3, 4, 5, 7 };
relationShips[5] = new int[] { 2, 5, 8 };
relationShips[6] = new int[] { 3, 4, 6, 7 };
relationShips[7] = new int[] { 6, 7, 8 };
relationShips[8] = new int[] { 4, 5, 7, 8 };
fileSave = new File("game.txt");
if(!fileSave.exists())
{
try
{
fileSave.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
MyGame g=new MyGame();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(JBSaveOperation))//如果点击的是“保存操作”按钮
{
BufferedWriter writer;
try
{ //记录游戏结果 写进游戏记录文档
writer = new BufferedWriter(new FileWriter("game.txt"));
writer.write(tempSaveStrB.toString());
writer.newLine();
writer.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
for(int i=0;i<9;i++)
{
if(e.getSource().equals(gb[i])) //如果点中第i个
{
if(gb[i].getText().equals("-"))
{
JOptionPane.showMessageDialog(null,"你应该点“+”的");
return;
}
ButtonChange(i);
tempSaveStrB.append(i+",");
if(isWin())
{JOptionPane.showMessageDialog(null,"你输了!");}
if(isFail())
{JOptionPane.showMessageDialog(null,"你赢了!");}
}
}
}
private boolean isFail()
{
int count=0;
for(int i=0;i<gb.length;i++)
{
if(gb[i].getText().equals("-"))
count++;
}
if(9==count)
return true;
else
return false;
}
private boolean isWin() //遍历所有Button
{
int count=0;
for(int i=0;i<gb.length;i++)
{
if(gb[i].getText().equals("+"))
count++;
}
if(8==count && gb[4].getText().equals("-"))
return true;
else
return false;
}
private void ButtonChange(int i)
{
for(int j=0;j<relationShips[i].length;j++)
{
if(gb[relationShips[i][j]].getText().equals("+"))
{
gb[relationShips[i][j]].setText("-");
}
else
{
gb[relationShips[i][j]].setText("+");
}
}
}
}