自定义jbutton子类:
//extendbutton.java
package ly.java;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class extendbutton extends jbutton
{
private int button_pos;
private boolean button_status;
private boolean button_visited;
public int setpostion(int pos)
{
this.button_pos = (pos >= 0 && pos <= 81) ? pos : 0;
return this.button_pos ;
}
public int getpostion()
{
return this.button_pos;
}
public boolean setstatus(boolean sta)
{
this.button_status = sta;
return this.button_status;
}
public boolean getstatus()
{
return this.button_status;
}
public boolean visited()
{
return this.button_visited;
}
public boolean setvisited(boolean vis)
{
this.button_visited = vis;
return this.button_visited;
}
}
游戏类
//game.java
package ly.java.game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ly.java.extendbutton;
public class game extends jframe implements actionlistener{
private container mycontainer;
private gridlayout mylayout;
extendbutton[] mybutton = new extendbutton[81];
private game()
{
this.settitle("game");
this.setsize( 500,500 );
this.initbutton();
}
private void initbutton()
{
mycontainer = getcontentpane();
mylayout = new gridlayout( 9, 9, 1, 1 );
mycontainer.setlayout( mylayout );
for(int i=0; i < 81; i++)
{
mybutton[i] = new extendbutton();
mybutton[i].setpostion(i);
mycontainer.add(mybutton[i]);
mybutton[i].addactionlistener( this );
}
system.gc();
this.setbomb(13);
show();
}
private void setbomb(int count)
{
int counter = 0;
int tempint;
while(counter != count)
{
tempint = ( int )(math.random() * 81);
if(!mybutton[tempint].getstatus())
{
mybutton[tempint].setstatus(true);
counter++;
}
}
}
private void showbomb()
{
for(int i = 0; i < 81; i++)
{
if(mybutton[i].getstatus())
{
mybutton[i].setbackground( new color(0,0,0) );
}
}
}
private void checkbutton(extendbutton tempbutton)
{
if(tempbutton.getstatus())
{
joptionpane.showmessagedialog( null, "you failed!", "game",joptionpane.information_message );
this.showbomb();
return;
}
int[] circlenum = new int[8];
int temp = 0;
if(!tempbutton.visited())
{
circlenum[0] = tempbutton.getpostion() – 9;
circlenum[0] = (circlenum[0] < 0) ? -1 : circlenum[0];
circlenum[1] = tempbutton.getpostion() – 8;
circlenum[1] = (circlenum[1] < 0 || circlenum[1] % 9 == 0) ? -1 : circlenum[1];
circlenum[2] = tempbutton.getpostion() + 1;
circlenum[2] = (circlenum[2] % 9 == 0) ? -1 : circlenum[2];
circlenum[3] = tempbutton.getpostion() + 10;
circlenum[3] = (circlenum[3] > 80 || circlenum[3] % 9 == 0) ? -1 : circlenum[3];
circlenum[4] = tempbutton.getpostion() + 9;
circlenum[4] = (circlenum[4] > 80) ? -1 : circlenum[4];
circlenum[5] = tempbutton.getpostion() + 8;
circlenum[5] = (circlenum[5] > 80 || circlenum[5] % 8 == 0) ? -1 : circlenum[5];
circlenum[6] = tempbutton.getpostion() – 1;
circlenum[6] = (circlenum[6] % 8 == 0) ? -1 : circlenum[6];
circlenum[7] = tempbutton.getpostion() – 10;
circlenum[7] = (circlenum[7] < 0 || circlenum[7] % 8 == 0) ? -1 : circlenum[7];
for(int i = 0; i < 8; i++)
{
if(circlenum[i] != -1)
{
if(mybutton[circlenum[i]].getstatus() && !mybutton[circlenum[i]].visited()) temp++;
}
}
if(temp > 0)
{
tempbutton.setvisited( true );
tempbutton.settext( string.valueof(temp) );
temp = 0;
}
else if(temp == 0)
{
tempbutton.setvisited( true );
tempbutton.setbackground(new color( 125,152,0 ));
for(int i = 0; i < 8; i++)
{
if(circlenum[i] != -1 && !mybutton[circlenum[i]].visited())
{
checkbutton(mybutton[circlenum[i]]);
}
}
}
}
}
public void actionperformed(actionevent e)
{
checkbutton((extendbutton)e.getsource());
}
public static void main(string[] args)
{
game newgame = new game();
newgame.addwindowlistener(
new windowadapter(){
public void windowclosing( windowevent e )
{
system.exit(0);
}
}
);
}
}