📄 gametest.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.net.URL;
public class GameTest{
public static void main(String args[]){
GameFrame gameFrm=new GameFrame();
}
}
class GameFrame extends JFrame{
private Timer clock;
private int panelX,panelY,mine_Count,mine_AllCount,showTime=0;//宽,高,雷数
private JLabel mineLabelBai,mineLabelShi,mineLabelGe,timeH,timeM,timeL;
private boolean gameStart,pass;//游戏开始
private JPanel panelScore,panelBody,panelMine,panelTime;
private JButton btnSmile;
private ImageIcon ico,imgSmile;
private MyButton btn[][];
//死亡
private void gameOver(int dx,int dy){
clock.stop();
for(int i=0;i<panelX;i++){
for(int j=0;j<panelY;j++){
if (btn[i][j].isMine()&&(!btn[i][j].isFlag())){
btn[i][j].setBorder(BorderFactory.createEmptyBorder());
URL url = GameTest.class.getResource("images/mine.gif");
btn[i][j].setIcon(new ImageIcon(url));
}
//错误标记
if (btn[i][j].isFlag()&&(!btn[i][j].isMine())){
URL url = GameTest.class.getResource("images/error.gif");
btn[i][j].setIcon(new ImageIcon(url));
}
//取消鼠标监听
MouseListener ml[]=btn[i][j].getMouseListeners();
for(int m=0;m<ml.length;m++){
btn[i][j].removeMouseListener(ml[m]);
}
}
URL url = GameTest.class.getResource("images/blood.gif");
btn[dx][dy].setIcon(new ImageIcon(url));
}
URL url = GameTest.class.getResource("images/face3.gif");
btnSmile.setIcon(new ImageIcon(url));
}
//过关
private void pass(){
clock.stop();
this.pass=true;
for(int i=0;i<panelX;i++){
for(int j=0;j<panelY;j++){
if (btn[i][j].isMine()){
URL url = GameTest.class.getResource("images/flag.gif");
btn[i][j].setIcon(new ImageIcon(url));
}
MouseListener ml[]=btn[i][j].getMouseListeners();
for(int m=0;m<ml.length;m++){
btn[i][j].removeMouseListener(ml[m]);
}
}
showMineCount(0);
URL url = GameTest.class.getResource("images/face4.gif");
btnSmile.setIcon(new ImageIcon(url));
}
}
private void showMineCount(int Count){
if (Count>=0){
changeScoreLabel(mineLabelBai,Count/100);
changeScoreLabel(mineLabelShi,Count/10%10);
changeScoreLabel(mineLabelGe,Count%10);
}
else
{
changeScoreLabel(mineLabelBai,10);
changeScoreLabel(mineLabelShi,Math.abs(Count)/10%10);
changeScoreLabel(mineLabelGe,Math.abs(Count)%10);
}
}
private void showTime(int Count){
if (Count<=999){
changeScoreLabel(timeH,Count/100);
changeScoreLabel(timeM,Count/10%10);
changeScoreLabel(timeL,Count%10);
}
}
private void changeScoreLabel(JLabel lb,int sc){
switch(sc){
case 0:{
URL url = GameTest.class.getResource("images/d0.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 1:{
URL url = GameTest.class.getResource("images/d1.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 2:{
URL url = GameTest.class.getResource("images/d2.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 3:{
URL url = GameTest.class.getResource("images/d3.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 4:{
URL url = GameTest.class.getResource("images/d4.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 5:{
URL url = GameTest.class.getResource("images/d5.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 6:{
URL url = GameTest.class.getResource("images/d6.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 7:{
URL url = GameTest.class.getResource("images/d7.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 8:{
URL url = GameTest.class.getResource("images/d8.gif");
lb.setIcon(new ImageIcon(url));
break;
}
case 9:{
URL url = GameTest.class.getResource("images/d9.gif");
lb.setIcon(new ImageIcon(url));
break;
}
default:{
URL url = GameTest.class.getResource("images/d10.gif");
lb.setIcon(new ImageIcon(url));
break;
}
}
}
public GameFrame(){
this.initialFrame();
}
//内部类:自定义设置小窗口
class DefineFrame extends JDialog{
private int panX,panY,mineCount,locationX,locationY;
private JFormattedTextField heightField,widthField,mineField;
private JPanel panelNorth,panelSouth,panelWest,panelEast;
private void initialFrame(){
this.setResizable(false);
this.setTitle("自定义雷区");
this.setLocation(locationX,locationY);
panelNorth=new JPanel();
panelSouth=new JPanel();
panelWest=new JPanel(new FlowLayout());
panelEast=new JPanel(new FlowLayout());
//设置面板属性
panelNorth.setPreferredSize(new Dimension(200,20));
panelSouth.setPreferredSize(new Dimension(200,20));
panelWest.setPreferredSize(new Dimension(120,75));
panelEast.setPreferredSize(new Dimension(80,75));
Container cont=this.getContentPane();
cont.add("North",panelNorth);
cont.add("South",panelSouth);
cont.add("West",panelWest);
cont.add("East",panelEast);
//添加文本框
heightField=new JFormattedTextField(NumberFormat.getIntegerInstance());
widthField=new JFormattedTextField(NumberFormat.getIntegerInstance());
mineField=new JFormattedTextField(NumberFormat.getIntegerInstance());
heightField.setPreferredSize(new Dimension(40,20));
widthField.setPreferredSize(new Dimension(40,20));
mineField.setPreferredSize(new Dimension(40,20));
heightField.setBorder(new BevelBorder(BevelBorder.LOWERED));
widthField.setBorder(new BevelBorder(BevelBorder.LOWERED));
mineField.setBorder(new BevelBorder(BevelBorder.LOWERED));
class MyLabel extends JLabel{
public MyLabel(String Text){
super(Text);
setFont(new Font("宋体",Font.PLAIN,12));
}
}
panelWest.add(new MyLabel("高度(H):"));
heightField.setText(String.valueOf(panelY));
widthField.setText(String.valueOf(panelX));
mineField.setText("10");
panelWest.add(heightField);
panelWest.add(new MyLabel("宽度(W):"));
panelWest.add(widthField);
panelWest.add(new MyLabel("雷数(M):"));
panelWest.add(mineField);
//添加按钮;
JButton btnEnter=new JButton("确定");
JButton btnCancel=new JButton("取消");
btnEnter.setFont(new Font("宋体",Font.PLAIN,12));
btnCancel.setFont(new Font("宋体",Font.PLAIN,12));
btnEnter.setPreferredSize(new Dimension(60,25));
btnCancel.setPreferredSize(new Dimension(60,25));
btnEnter.setBorder(new BevelBorder(BevelBorder.RAISED));
btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED));
//设置按钮事件;
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
panX=Integer.parseInt(widthField.getText());
panY=Integer.parseInt(heightField.getText());
mineCount=Integer.parseInt(mineField.getText());
if (panX>=30){
panX=30;
}
if (panY>=24){
panY=24;
}
if (mineCount>=panX*panY){
mineCount=panX*panY/2;
}
if (mineCount<10){
mineCount=10;
}
}
catch(Exception ex){
panX=9;
panY=9;
mineCount=10;
}
setLevel(panX,panY,mineCount);
dispose();
}
});
panelEast.add(btnEnter);
panelEast.add(btnCancel);
this.pack();
btnEnter.requestFocus(true);
this.setVisible(true);
};
public DefineFrame(Frame owner,boolean modal,int locateX,int locateY){
super(owner,modal);
locationX=locateX;
locationY=locateY+20;
initialFrame();
}
}
//自定义按钮
class MyButton extends JButton {
private int locationX,locationY,roundMineCount;//坐标X,坐标Y,周围雷数
private boolean isMine;//是否为雷
private boolean isOpen;
private boolean isFlag,isAsk;
public int both=0;
private void roundUp(){
if (this.NorthEast()!=null){
if (!((MyButton)(this.NorthEast())).isFlag()&&(!((MyButton)(this.NorthEast())).isOpen())){
((MyButton)(this.NorthEast())).setUp();
}
}
if (this.NorthWest()!=null){
if (!((MyButton)(this.NorthWest())).isFlag()&&(!((MyButton)(this.NorthWest())).isOpen())){
((MyButton)(this.NorthWest())).setUp();
}
}
if (this.North()!=null){
if (!((MyButton)(this.North())).isFlag()&&(!((MyButton)(this.North())).isOpen())){
((MyButton)(this.North())).setUp();
}
}
if (this.East()!=null){
if (!((MyButton)(this.East())).isFlag()&&(!((MyButton)(this.East())).isOpen())){
((MyButton)(this.East())).setUp();
}
}
if (this.West()!=null){
if (!((MyButton)(this.West())).isFlag()&&(!((MyButton)(this.West())).isOpen())){
((MyButton)(this.West())).setUp();
}
}
if (this.South()!=null){
if (!((MyButton)(this.South())).isFlag()&&(!((MyButton)(this.South())).isOpen())){
((MyButton)(this.South())).setUp();
}
}
if (this.SouthEast()!=null){
if (!((MyButton)(this.SouthEast())).isFlag()&&(!((MyButton)(this.SouthEast())).isOpen())){
((MyButton)(this.SouthEast())).setUp();
}
}
if (this.SouthWest()!=null){
if (!((MyButton)(this.SouthWest())).isFlag()&&(!((MyButton)(this.SouthWest())).isOpen())){
((MyButton)(this.SouthWest())).setUp();
}
}
}
private void setDown(){
this.setBorder(null);
URL url = GameTest.class.getResource("images/0.gif");
this.setIcon(new ImageIcon(url));
}
private void setUp(){
this.setBorder(BorderFactory.createRaisedBevelBorder());
URL url = GameTest.class.getResource("images/blank.gif");
this.setIcon(new ImageIcon(url));
}
private void roundDown(){
if (this.NorthEast()!=null){
if (!((MyButton)(this.NorthEast())).isFlag()&&(!((MyButton)(this.NorthEast())).isOpen())){
((MyButton)(this.NorthEast())).setDown();
}
}
if (this.NorthWest()!=null){
if (!((MyButton)(this.NorthWest())).isFlag()&&(!((MyButton)(this.NorthWest())).isOpen())){
((MyButton)(this.NorthWest())).setDown();
}
}
if (this.North()!=null){
if (!((MyButton)(this.North())).isFlag()&&(!((MyButton)(this.North())).isOpen())){
((MyButton)(this.North())).setDown();
}
}
if (this.East()!=null){
if (!((MyButton)(this.East())).isFlag()&&(!((MyButton)(this.East())).isOpen())){
((MyButton)(this.East())).setDown();
}
}
if (this.West()!=null){
if (!((MyButton)(this.West())).isFlag()&&(!((MyButton)(this.West())).isOpen())){
((MyButton)(this.West())).setDown();
}
}
if (this.South()!=null){
if (!((MyButton)(this.South())).isFlag()&&(!((MyButton)(this.South())).isOpen())){
((MyButton)(this.South())).setDown();
}
}
if (this.SouthEast()!=null){
if (!((MyButton)(this.SouthEast())).isFlag()&&(!((MyButton)(this.SouthEast())).isOpen())){
((MyButton)(this.SouthEast())).setDown();
}
}
if (this.SouthWest()!=null){
if (!((MyButton)(this.SouthWest())).isFlag()&&(!((MyButton)(this.SouthWest())).isOpen())){
((MyButton)(this.SouthWest())).setDown();
}
}
}
//双击迅速开区域
private void doubleQuick(){
int count=0;
if (this.NorthEast()!=null){
if (((MyButton)(this.NorthEast())).isFlag()){
count++;
}
}
if (this.NorthWest()!=null){
if (((MyButton)(this.NorthWest())).isFlag()){
count++;
}
}
if (this.North()!=null){
if (((MyButton)(this.North())).isFlag()){
count++;
}
}
if (this.East()!=null){
if (((MyButton)(this.East())).isFlag()){
count++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -