?? gamemain.java
字號:
package com.xinxi.mine;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
/**
* 游戲主框架的程序?qū)崿F(xiàn).
* @author Administrator
*
*/
public class GameMain extends JFrame implements ActionListener,MouseListener{
private JButton but[][];//存放方塊按鈕的一個二維數(shù)組
private char thunder[][];//存放方塊數(shù)值的一個二維數(shù)組
private int count;//雷的個數(shù)
private int remainCount;//剩余雷的數(shù)量.
private int remainDiamondCount;//剩余方塊的數(shù)量,用于求成功的條件.
private int lengthx;//方塊的橫長
private int lengthy;//方塊的縱長
private JTextField tf1;
private JTextField tf2;
private boolean flag=false;
int x, y;//鼠標點的方塊的數(shù)組坐標
public GameMain(){
}
public GameMain(int count, int lengthx, int lengthy){
this.count = count;
this.remainCount = count;
this.remainDiamondCount = lengthx*lengthy;
this.lengthx = lengthx;
this.lengthy = lengthy;
but = new JButton[lengthx+1][lengthy+1];
for(int i=1; i<lengthx+1; i++){//把所有方塊加入到所在的方框內(nèi),并加監(jiān)聽.
for(int j=1; j<lengthy+1; j++){
but[i][j]=new JButton("");
but[i][j].addActionListener(this);
but[i][j].addMouseListener(this);
}
}
}
public void start(){
this.setLocation(300, 300);
if(lengthx*45<300){
this.setSize(300, lengthy*40);
}else {
this.setSize(lengthx*45, lengthy*40);//設(shè)定程序大小與方塊個數(shù)有關(guān).
}
//以下用于求方塊中雷的分布.
Distributing distributing = new Distributing(count, lengthx, lengthy);//對Distributing進行實例化
thunder = distributing.getThunder2();
this.addWindowListener(new MyWindowEvent());//程序的關(guān)閉實現(xiàn).
JLabel b1 = new JLabel("剩余雷數(shù)");//設(shè)定雷區(qū)上面的顯示信息
JLabel b2 = new JLabel("所用時間");
tf1 = new JTextField(5);
tf1.setText(String.valueOf(remainCount));//顯示剩余雷的數(shù)量
tf1.setEditable(false);
tf2 = new JTextField(5);
tf2.setEditable(false);
//b1.setSize();
JPanel p1 = new JPanel();//存放雷區(qū)上面的顯示信息
JPanel p2 = new JPanel();//存放雷區(qū)中所有方塊的信息.
p1.add(b1);
p1.add(tf1);
p1.add(b2);
p1.add(tf2);
p2.setLayout(new GridLayout(lengthx, lengthy));
for(int i=1; i<lengthx+1; i++){//把所有的方塊加入到p2中.
for(int j=1; j<lengthy+1; j++){
p2.add(but[i][j]);
}
}
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
this.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
JButton b = (JButton)e.getSource();
x=0;//初使化它們?yōu)榱嗽谶@層for循環(huán)中使用break;
y=0;
for(int i=1; i<but.length; i++){//為了找到是哪個按鈕被點擊了.
for(int j=1; j<but[0].length; j++){
if(but[i][j]==b){
x=i;
y=j;
break;
}
}
if(x!=0||y!=0){
break;
}
}
if(e.getButton()==MouseEvent.BUTTON1&&!but[x][y].getText().equals("*")){//左鍵單擊------------
if(but[x][y].getText()==""){
remainDiamondCount--;
if(thunder[x][y]=='*'){//挖到雷,退出程序
//把所有雷都顯示出來????????????????????????????????????
for(int i=1; i<but.length; i++){
for(int j=1; j<but[0].length; j++){
if(but[i][j].getText()==""&&thunder[i][j]=='*'){
but[i][j].setText("*");
}
}
}
Fail f = new Fail();
f.start();
} else{
but[x][y].setBackground(Color.lightGray);
but[x][y].setForeground(Color.RED);
if(thunder[x][y]!='0'){
but[x][y].setText(String.valueOf(thunder[x][y]));
} else{
//把周圍沒有雷的方塊都顯示出來.
this.openAround(x, y);
}
but[x][y].setEnabled(false);
}
}
}
else if(e.getButton()==MouseEvent.BUTTON3){//右鍵單擊--------------------
if(but[x][y].getText().equals("*")){
remainCount++;
remainDiamondCount++;
tf1.setText(String.valueOf(remainCount));
but[x][y].setText("");
} else if(but[x][y].getText()==""){
remainDiamondCount--;
remainCount--;
tf1.setText(String.valueOf(remainCount));
but[x][y].setText("*");
}
}
if(remainDiamondCount==remainCount){
for(int i=1; i<but.length; i++){
for(int j=1; j<but[0].length; j++){
if(but[i][j].getText()==""&&thunder[i][j]=='*'){
but[i][j].setText("*");
}
}
}
Success success = new Success();//如果成功挖出所有雷的話,彈出成功話面.
success.start();
}
tf2.setText(String.valueOf(this.remainDiamondCount));
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void actionPerformed(ActionEvent e) {
}
public void openAround(int a, int b){
but[a][b].setText(" ");
but[a][b].setEnabled(false);
for(int i=a-1; i<a+2; i++){
for(int j=b-1; j<b+2; j++){
if(i>0&&i<lengthx+1&&j>0&&j<lengthy+1&&but[i][j].getText().equals("")){
//只有在but上面沒有value值時(即:沒被點擊過),才執(zhí)行
this.remainDiamondCount--;
but[i][j].setBackground(Color.lightGray);
but[i][j].setForeground(Color.RED);
if(thunder[i][j]!='0'){
//this.remainDiamondCount--;
but[i][j].setText(String.valueOf(thunder[i][j]));
but[i][j].setEnabled(false);
} else{//當挖到的方塊周圍沒有雷的話,把所有它周圍沒有雷的顯示出來.
this.openAround(i, j);
}
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -