阅读thinking in  java 很多天,一直没有什么收获,直到今天才得到点感觉,写了一个很流行的“对对碰”的小game ,希望java  的前辈门帮小弟完善一下,也作为和我一样初涉java者的经验交流,真的感到“独学无友”,真诚的希望大家把自己的学习经历和我分享一下,不胜感激!
下面的程序已经通过测试,只是我不知道怎么打包成*.exe或*.jar,望不吝赐教!谢谢!//pengpengdui2.java
//编写一个按钮的程序,
//@author:zhoujianliang
//@version:v1.0
//@date:2007.3.3
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;public class  Pengpengdui2 extends JApplet implements ActionListener{
static String static_s1="",static_s2="";
    static int static_i1=-1,static_i2=-1;
static int static_k=0,static_count=0;//鼠标单击计数器,当为单数时给static_i1,static_s1赋值,分别记录电击的按钮和显示的字串
String s,s1;//中间变量
Icon icon=new ImageIcon("001.gif");
static String[] ss={"一","二","三","四","五","六","七","八","一","二","三","四","五","六","七","八"};
private static Icon[] pic;

String svalue[]=new String[16];
Icon faces[]=new Icon[16]; JButton[] btn=new JButton[16]; public void init(){
pic=new Icon[]{
    new ImageIcon(getClass().getResource("face0.gif")),
new ImageIcon(getClass().getResource("face1.gif")),
new ImageIcon(getClass().getResource("face2.gif")),
new ImageIcon(getClass().getResource("face3.gif")),
new ImageIcon(getClass().getResource("face4.gif")),
    new ImageIcon(getClass().getResource("face5.gif")),
new ImageIcon(getClass().getResource("face6.gif")),
new ImageIcon(getClass().getResource("face7.gif")),
    new ImageIcon(getClass().getResource("face0.gif")),
new ImageIcon(getClass().getResource("face1.gif")),
new ImageIcon(getClass().getResource("face2.gif")),
new ImageIcon(getClass().getResource("face3.gif")),
new ImageIcon(getClass().getResource("face4.gif")),
    new ImageIcon(getClass().getResource("face5.gif")),
new ImageIcon(getClass().getResource("face6.gif")),
new ImageIcon(getClass().getResource("face7.gif")), };
Random rand=new Random();
int[] ilist=new int[16];
boolean flag;
for(int i=0;i<16;i++){
ilist[i]=-1;
}
    for(int i=0;i<16;i++){
flag=true;
int ii=rand.nextInt(16);
for(int j=0;j<i;j++)
if(ilist[j]==ii){
   flag=false;
}//end if
if(flag==true)
ilist[i]=ii;
else 
i=i-1;
};//ilist[i]中存放的是0-15这几个数,这几个数是毫无规律的,只是不重复而已
for(int i=0;i<16;i++){
svalue[i]=ss[ilist[i]];
faces[i]=pic[ilist[i]];//将svalue[]中存放配对 的字符串,
} /***********************svalue[i]从0到15是随机分配的配对值**********/ Container cp=getContentPane();//创建一个容器,存放栅格按钮在cp里
setLayout(new GridLayout(4,4));//设置栅格为4行4列,16个格子
for(int i=0;i<16;i++){
btn[i]=new JButton("button",icon);//创建JButton的对象,将其存放到btn[]数组中
cp.add(btn[i]);//利用for语句对16个格子进行填充.填充的对象为按钮.
btn[i].setRolloverEnabled(true);
btn[i].setEnabled(true); }//结束for循环
for(int i=0;i<16;i++){
btn[i].setLabel("my button");//重新设置按钮标签值
btn[i].addActionListener(this);
}
for(int i=0;i<16;i++){
s1=(new String()).valueOf(i);
  btn[i].setActionCommand(s1);
}

}//end  init()

public void actionPerformed(ActionEvent e){
s=e.getActionCommand();//把点击鼠标所取得的字符串命令赋给s
static_k++;
for(int i=0;i<16;i++){
s1=(new String()).valueOf(i);//将在变量i转化成字符串
if(s.equals(s1)&&(static_k%2==1)){//奇数次点击,对应偶数次点击
btn[i].setLabel(svalue[i]);
btn[i].setIcon(faces[i]);
static_i1=i;
static_s1=svalue[i];
}//end if
if(s.equals(s1)&&(static_k%2==0)){//偶数次点击,对应奇数次点击
btn[i].setLabel(svalue[i]);
                btn[i].setIcon(faces[i]);
static_i2=i;
static_s2=svalue[i];
if(static_s1.equals(static_s2)&&(static_i2!=static_i1)){
btn[i].setLabel(static_s2);
//btn[static_i1].disable();
//btn[static_i2].disable();
btn[static_i1].setActionCommand("666");
btn[static_i2].setActionCommand("666");
static_count++;
/*if(static_count==8){
static_s2=static_s2+"you are win";
btn[static_i2].setLabel(static_s2);
//System.out.println("you are win");
}*/
}
else{
btn[static_i1].setLabel("my button");
btn[static_i2].setLabel("my button");
btn[static_i1].setIcon(icon);
btn[static_i2].setIcon(icon);
}
}
}
}

public static void main(String[] args) 
{
JApplet applet=new Pengpengdui2();
JFrame frame=new JFrame("S&T-碰碰对");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(400,400);
applet.init();
applet.start();

frame.setVisible(true);
}
}