Java编写的扫雷小程序
整
个 程 序 使 用 了 4 个 类
Game 用于启动程序,界面的布置,各种行列的算法。 Exte ndButt on 继承了 Jbutton 用于新建butt on 的各种属性 ShowNum 监听到某个butt on 后,计算出此butt on 周围有几个雷,
不同的雷对应不通的图片,
得到butt on 上应加载的图片地址
ViewReSet
此类用于新建一个窗口, 用户输入自己想要的行、 列、雷数,然后用新的行列雷数
一个新的Game
0 saolei
i 曰”;峥STC
I , J T __
□田 com.dhLraolei
!
' ! lil Jj Exte nd Button java
S- .jj Game Java J
I
,
| FF- ,r RunGameJs^ I IT 』Showl'JuTnjavs
-犀 mag* ;画 biaose.jpg
I dise.Jpg :T
disel jpg
]恤2 .jpg 回d 彌Jpg
1 dise4.jpg
也| dise5jpg 歸
dise6jpg dise7.jpg jjl ctiseBJpg 迥
lei.jp g S leibjjpg
El n JR.E Systerr Library 0dKl,6,O_10] L ”G bin
dise8
biaose dise2 dise3
ise5
dise6 dise7
类.
new
10
biaoji
private int numBomb , countTime , leaveBomb private int myRows , myColumns ;
private int xis , yis J numEnd ;
/**************************************************************** 运行类创建游戏主窗体
*/
package com.dhl.saolei;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.Timer;
private Timer timer
ActionListener{ public class Game extends JFrame implements
private Container myContainer J
private GridLayout myLayout ;
private JPanel myPanelMain ;
private JPanel myPanel ;
private JToolBar myTool ;
private JLabel time ;
private JLabel labelTime ;
private JLabel bombNum ;
private JLabel labelBombNum ;
private JMenuBar menuBar ;
private JMenu menuE , menuH ;
private JMenuItem menuItemCZ , menuItemSZ
// 初始化窗体 private void InitView() { // 菜单初始化
this . menuBar = new JMenuBar(); this . menuE = new JMenu( " 菜单 (E)" ); this .
menuH = new JMenu( " 帮助 (H)" );
this . menuItemCZ = new JMenuItem( " 重置 " );
this . menuItemSZ = new JMenuItem( " 设置 " );
this . myPanelMain = new JPanel(borderLayout); this . labelTime = new JLabel( this . time = new JLabel();
this . time .setForeground(Color. this .
time .setFont( new Font( this .
private ExtendButton[]
myButton null
this . menuItemCZ .setAccelerator(KeyStroke. _C, InputEvent. CTRL_MASK ));
this . menuItemCZ .addActionListener( this . menuE .add( this . menuItemCZ );
getKeyStroke this );
(KeyEvent. VK
this . menuItemCZ .setAccelerator(KeyStroke. _S, InputEvent. CTRL_MASK ));
this . menuItemSZ .addActionListener( this . menuE .add( this . menuItemSZ ); getKeyStroke this );
(KeyEvent. VK
this . menuBar .add( this . menuE ); this . menuBar setJMenuBar( .add( this . menuH ); this . menuBar );
// 时间信息初始化 this . myTool new JToolBar();
this . myTool .setLayout( this . myTool .setFloatable( BorderL
ayout borderLayout =
new GridLayout(1, 4)); false );
new BorderLayout(); " 时间: " ); RED); "font" , Font. PLAIN , 20));
this . myTool .add( this . labelTime ); this . myTool .add( this . time ); this . myTool .add( this . labelBombNum ); this . myTool .add( this . bombNum );
this . myPanelMain .add( this . myTool , borderLayout.
NORTH);
this . bombNum .setForeground(Color. RED);
this . bombNum .setFont( new Font( "font" , Font. PLAIN , 20));
labelBombNum = new JLabel( " 雷数:" );
this . bombNum = new JLabel();
this . bombNum.setText(String. valueOf ( this . numBomb ));
// 初始化 myButton
private void InitButton() {
BorderLayout borderLayout = new BorderLayout(); this . leaveBomb = this . numBomb ;
this . numEnd = ( this . myRows * this . myColumns ) - this . leaveBomb this . myContainer = this .getContentPane();
this . myLayout = new GridLayout( this . myRows , this . myColumns , 1,
1);
this . myPanel = new JPanel();
this . myPanel .setVisible( this . myPanel .setLayout( . myPanel , borderLayout. CENTER); . myPanelMain ); this . myColumns )];
this . myRows * this . myColumns ); i++) { [i] = new
ExtendButton(); [i].SetPostion(i);
[i].setIcon(( new ShowNum(10)).getImageIcon()); [i].setButton_num(0); [i].SetVisited( false ); [i].SetStatus( false );
public void mouseClicked(MouseEvent evt) { if ((evt.getModifiers() == InputEvent.
int n =
((ExtendButton)evt.getSource()).getButton_num();
if (n == 1) {
if (!((ExtendButton)evt.getSource()).GetVisited()) { ((ExtendButton)evt.getSource()).setButton_num(0); ((ExtendButton)evt.getSource()).setIcon(( new ShowNum(10)).getImageIcon());
leaveBomb ++;
bombNum.setText(String. valueOf ( leaveBomb ));
}
} else {
true );
myLayout );
this . myPanelMain .add( this this . myContainer .add( this
this . myButton
new ExtendButton[( this . myRows *
for ( int i = 0; i < ( this . myButton this . myButton this . myButton this . myButton this . myButton this . myButton // 设置右键监听标
记雷
this . myButton
[i].addMouseListener( new MouseAdapter() {
BUTTON3_MASK))
if (!((ExtendButton)evt.getSource()).GetVisited()) { ((Exte ndButt on )evt.getSource()).setButt on_nu m(1); ((Exte ndButt on) evt.getSource()).setIc on(( ShowNum(11)).getlmagelco n());
leaveBomb
bombNum.setText(Stri ng.
} }
}
});
this . myPanel .add( this . myButton [i]); this . myButton [i].addActionListener( this );
}
System. gc ();
this .SetBomb( this . numBomb); show();
//时钟设置
int delay = 1000;
// 创建一个监听事件
Action Liste ner drawClock = new Actio nListe ner(){ public void action Performed(Act ionEvent evt){ coun tTime ++;
time .setText(String. valueOf ( countTime ));
repai nt();
} };
//
创建一个时间计数器,每一秒触发一次 this . timer = new Timer(delay,drawClock); this . timer .start(); }
//重新初始化方法
(int
i = 0; i < ( this . m yRows * this . myColumns ); i++) { this .myButton [i].SetVisited( false ); this .myButton [i].SetStatus( false ); this .myButton [i].setText( n ull ); this
.myButton [i].setIco n(( new ShowNum(10)).getImageIcon())
private void SetExte ndButto n() { for new
valueOf (leaveBomb )); }