Java扫雷程序,初试Java
2008-01-05 09:05:07 来源:WEB开发网核心提示:自定义JButton子类://ExtendButton.javapackage ly.java;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ExtendButton extends JButton{ PRivat
自定义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)
更多精彩
赞助商链接