Java扫雷程序,初试Java

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

自定义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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航