文件Frame_Main.java
============================
package jbomb;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Frame_Main extends JFrame{
private JPanel ContentPane = (JPanel)this.getContentPane();
private JPanel jP_bomb = new JPanel();
private JPanel jP_btn = new JPanel();
private JButton jbtn_about = new JButton();
private JLabel jlb_bomb_num = new JLabel();
private JLabel jlb_time = new JLabel();
private JLabel jlb_lv = new JLabel();
private JLabel jlb_cblv = new JLabel();
private JComboBox jcb_lv = new JComboBox();
private JButton jbtn_restart = new JButton();
private Frame_Main_Listener fml = new Frame_Main_Listener(this);
private ImageIcon ico = new ImageIcon("F:\\10.ico");
private javax.swing.Timer timer;
private Color EnteredColor = new Color(192, 192, 255);
private Color LeftPressedColor = new Color(255, 255, 192);
private Color RightPressedColor = new Color(192, 255, 192);
private Color DefaultColor = Color.darkGray;
private Color ShowColor = new Color(192, 192, 192);
private boolean isbothdown = false;
private boolean isFail = false;
private boolean iswin = false; //用来限制只能弹出一个过关窗口
private int ismd; //判断鼠标按下状态
private int intzs; //左上角位置
private int intys; //右上角位置
private int intzx; //左下角位置
private int intyx; //右下角位置
private JLabel[] JlbList;
private int[] IntList;
private int[] OOList;
private int count; //方块总数
private int lnum; //列数;
private int hnum; //行数;
private int ln = 0; //雷总数;
public String nowlv = "初级";
public int thetime = 0;
public Frame_Main(){
jbtn_restart_ActionListener jra = new jbtn_restart_ActionListener(this);
jcl_lv_ItemListener jli = new jcl_lv_ItemListener(this);
timer_ActionListener ta = new timer_ActionListener(this);
jbtn_about_ActionListener jaa = new jbtn_about_ActionListener(this);
timer = new javax.swing.Timer(1000,ta);
this.setTitle("Java版扫雷");
this.setBounds(100,100,300,208);
this.setResizable(false);
this.setVisible(true);
this.setIconImage(ico.getImage());
this.addWindowListener(fml);
jbtn_about.setBounds(42,4,60,18);
jbtn_about.setText("关于");
jbtn_about.addActionListener(jaa);
jlb_time.setBounds(8,30,88,24);
jlb_time.setText("000");
jlb_time.setBorder(BorderFactory.createLineBorder(Color.black));
jlb_time.setHorizontalAlignment(SwingConstants.CENTER);
jlb_bomb_num.setBounds(8,60,88,24);
jlb_bomb_num.setText("000");
jlb_bomb_num.setBorder(BorderFactory.createLineBorder(Color.black));
jlb_bomb_num.setHorizontalAlignment(SwingConstants.CENTER);
jlb_lv.setBounds(8,90,36,24);
jlb_lv.setText("难度:");
jlb_cblv.setBounds(40,90,56,24);
jlb_cblv.setBorder(BorderFactory.createLineBorder(Color.black));
jlb_cblv.setHorizontalAlignment(SwingConstants.CENTER);
jlb_cblv.setVisible(false);
jcb_lv.setBounds(40,90,56,24);
jcb_lv.addItem("初级");
jcb_lv.addItem("中级");
jcb_lv.addItem("高级");
jcb_lv.addItemListener(jli);
jbtn_restart.setBounds(8,120,88,30);
jbtn_restart.setText("重新开始");
jbtn_restart.addActionListener(jra);
jP_bomb.setBounds(8,8,156,156);
jP_bomb.setBorder(BorderFactory.createLineBorder(Color.black));
jP_bomb.setLayout(null);
jP_btn.setBounds(176,8,104,156);
jP_btn.setBorder(BorderFactory.createLineBorder(Color.black));
jP_btn.setLayout(null);
jP_btn.add(jbtn_about);
jP_btn.add(jlb_time);
jP_btn.add(jlb_bomb_num);
jP_btn.add(jlb_lv);
jP_btn.add(jcb_lv);
jP_btn.add(jlb_cblv);
jP_btn.add(jbtn_restart);
ContentPane.setLayout(null);
ContentPane.add(jP_bomb);
ContentPane.add(jP_btn);
chushihua();
}
public void chushihua(){
int i = 0;
int j = 0;
int tmpnum = 0;
int zwln = 0;
boolean[] iscz;
ismd = 0;
isbothdown = false;
isFail = false;
thetime = 0;
iswin = false;
jP_bomb.removeAll();
jbtn_restart.setEnabled(false);
jlb_cblv.setVisible(false);
jcb_lv.setVisible(true);
jlb_time.setText("000");
timer.stop();
Random rnd = new Random();
JLabel jlb;
jlb_MouseListener jml = new jlb_MouseListener(this);
if(nowlv.equals("初级")){
lnum = 9;
hnum = 9;
intzs = 0;
intys = 8;
intzx = 72;
intyx = 80;
ln = 10;
this.setSize(300,208);
jP_bomb.setSize(156,156);
jP_btn.setLocation(176,8);
}
else if(nowlv.equals("中级")){
lnum = 16;
hnum = 16;
intzs = 0;
intys = 15;
intzx = 240;
intyx = 255;
ln = 40;
this.setSize(419,327);
jP_bomb.setSize(275,275);
jP_btn.setLocation(42,4);
jP_btn.setLocation(295,8);
}
else if(nowlv.equals("高级")){
lnum = 30;
hnum = 16;
intzs = 0;
intys = 29;
intzx = 450;
intyx = 479;
ln = 99;
this.setSize(659,327);
jP_bomb.setSize(513,275);
jP_btn.setLocation(535,8);
}
jlb_bomb_num.setText("0"+ln);
count = lnum * hnum;
JlbList = new JLabel[count];
IntList = new int[count];
OOList = new int[count];
iscz = new boolean[count];
for(i = 0;i < hnum;i++){
for(j = 0;j < lnum;j++ ){
jlb = new JLabel();
jlb.setBorder(BorderFactory.createLineBorder(Color.black));
jlb.setBounds(17*j+1,17*i+1,18,18);
jlb.setOpaque(true);
jlb.setBackground(DefaultColor);
jlb.setHorizontalAlignment(SwingConstants.CENTER);
jlb.addMouseListener(jml);
JlbList[tmpnum] = jlb;
IntList[tmpnum] = 0;
OOList[tmpnum] = 0;
iscz[tmpnum] = false;
jP_bomb.add(jlb);
tmpnum ++;
}
}
this.repaint();
for(int n = 0;n < ln;n++){
int rndint = (int)(rnd.nextInt(count));
if(iscz[rndint] == false){
IntList[rndint] = 9;
iscz[rndint] = true;
}
else{
n--;
}
}
for(i = 0;i < count;i++){
if(IntList[i] == 0){
if(i == intzs){
if(IntList[i+1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
if(IntList[i+lnum+1] == 9) zwln++;
}
else if(i == intys){
if(IntList[i-1] == 9) zwln++;
if(IntList[i+lnum-1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
}
else if(i == intzx){
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-lnum+1] == 9) zwln++;
if(IntList[i+1] == 9) zwln++;
}
else if(i == intyx){
if(IntList[i-lnum-1] == 9) zwln++;
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-1] == 9) zwln++;
}
else if(i > intzs && i < intys){
if(IntList[i-1] == 9) zwln++;
if(IntList[i+1] == 9) zwln++;
if(IntList[i+lnum-1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
if(IntList[i+lnum+1] == 9) zwln++;
}
else if(i > intzx && i < intyx){
if(IntList[i-lnum-1] == 9) zwln++;
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-lnum+1] == 9) zwln++;
if(IntList[i-1] == 9) zwln++;
if(IntList[i+1] == 9) zwln++;
}
else if(i > intzs && i < intzx && i % lnum == 0){
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-lnum+1] == 9) zwln++;
if(IntList[i+1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
if(IntList[i+lnum+1] == 9) zwln++;
}
else if(i > intys && i < intyx && (i+1) % lnum == 0){
if(IntList[i-lnum-1] == 9) zwln++;
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-1] == 9) zwln++;
if(IntList[i+lnum-1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
}
else if(i < intyx - lnum){
if(IntList[i-lnum-1] == 9) zwln++;
if(IntList[i-lnum] == 9) zwln++;
if(IntList[i-lnum+1] == 9) zwln++;
if(IntList[i-1] == 9) zwln++;
if(IntList[i+1] == 9) zwln++;
if(IntList[i+lnum-1] == 9) zwln++;
if(IntList[i+lnum] == 9) zwln++;
if(IntList[i+lnum+1] == 9) zwln++;
}
IntList[i] = zwln;
zwln = 0;
}
}
}
public void jlb_mouseExited(MouseEvent e){
if(isFail == false){
if(ismd == 0){
JLabel jlb = (JLabel)e.getSource();
int n = (int)(jlb.getX() / 17) + (int)(jlb.getY() / 17) * lnum;
if(IntList[n] == 10){
jlb.setBackground(ShowColor);
}
else{
jlb.setBackground(DefaultColor);
}
}
}
}
public void jlb_mouseEntered(MouseEvent e){
if(isFail == false){
if(ismd == 0){
JLabel jlb = (JLabel)e.getSource();
jlb.setBackground(EnteredColor);
}
}
}
public void jlb_mousePressed(MouseEvent e){
if(isFail == false){
ismd++;
jbtn_restart.setEnabled(true);
jcb_lv.setVisible(false);
jlb_cblv.setVisible(true);
jlb_cblv.setText(nowlv+"");
if(ismd == 2) isbothdown = true;
JLabel jlb = (JLabel)e.getSource();
int n = (int)(jlb.getX() / 17) + (int)(jlb.getY() / 17) * lnum;
int l = 0;
JLabel tmpjlb = new JLabel();
int m;
timer.start();
if(isbothdown == true){
jlb.setBackground(LeftPressedColor);
if(n == intzs){
for(m = 0;m <= 2;m++){
switch (m){
case 0 :
l = n + 1;
break;
case 1 :
l= n + lnum;
break;
case 2 :
l = n + lnum + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n == intys){
for(m = 0;m <= 2;m++){
switch (m){
case 0 :
l = n - 1;
break;
case 1 :
l = n + lnum- 1;
break;
case 2 :
l = n + lnum;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n == intzx){
for(m = 0;m <= 2;m++){
switch (m){
case 0 :
l = n - lnum;
break;
case 1 :
l = n - lnum + 1;
break;
case 2 :
l = n + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n == intyx){
for(m = 0;m <= 2;m++){
switch (m){
case 0 :
l = n - lnum - 1;
break;
case 1 :
l = n - lnum;
break;
case 2 :
l = n - 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n > intzs && n < intys){
for(m = 0;m <= 4;m++){
switch (m){
case 0 :
l = n - 1;
break;
case 1 :
l = n + 1;
break;
case 2 :
l = n + lnum - 1;
break;
case 3 :
l = n + lnum;
break;
case 4 :
l = n + lnum + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n > intzx && n < intyx){
for(m = 0;m <= 4;m++){
switch (m){
case 0 :
l = n - lnum - 1;
break;
case 1 :
l = n - lnum;
break;
case 2 :
l = n - lnum + 1;
break;
case 3 :
l = n - 1;
break;
case 4 :
l = n + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n > intzs && n < intzx && n % lnum == 0){
for(m = 0;m <= 4;m++){
switch (m){
case 0 :
l = n - lnum;
break;
case 1 :
l = n - lnum + 1;
break;
case 2 :
l = n + 1;
break;
case 3 :
l = n + lnum;
break;
case 4 :
l = n + lnum + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n > intys && n < intyx && (n+1) % lnum == 0){
for(m = 0;m <= 4;m++){
switch (m){
case 0 :
l = n - lnum - 1;
break;
case 1 :
l = n - lnum;
break;
case 2 :
l = n - 1;
break;
case 3 :
l = n + lnum - 1;
break;
case 4 :
l = n + lnum;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
else if(n < intyx){
for(m = 0;m <= 7;m++){
switch (m){
case 0 :
l = n - lnum - 1;
break;
case 1 :
l = n - lnum;
break;
case 2 :
l = n - lnum + 1;
break;
case 3 :
l = n - 1;
break;
case 4 :
l = n + 1;
break;
case 5 :
l = n + lnum - 1;
break;
case 6 :
l = n + lnum;
break;
case 7 :
l = n + lnum + 1;
break;
}
tmpjlb = (JLabel)JlbList[l];
if(tmpjlb.getBackground() == DefaultColor && OOList[l] == 0)
tmpjlb.setBackground(LeftPressedColor);
}
}
}
else if(e.getButton() == MouseEvent.BUTTON1){
jlb.setBackground(LeftPressedColor);
}
else if(e.getButton() == MouseEvent.BUTTON3){
if(IntList[n] != 10){
jlb.setBackground(RightPressedColor);
if(OOList[n] == 0){
OOList[n] = 2;
jlb.setText("△");
ln--;
if(ln >= 0){
jlb_bomb_num.setText(ln+"");
while (jlb_bomb_num.getText().length() < 3){
jlb_bomb_num.setText("0"+jlb_bomb_num.getText());
}
}
}
else if(OOList[n] == 2){
OOList[n] = 3;
jlb.setText("?");
}
else if(OOList[n] == 3){
OOList[n] = 0;
jlb.setText("");
ln++;
if(ln >= 0){
jlb_bomb_num.setText(ln+"");
while (jlb_bomb_num.getText().length() < 3){
jlb_bomb_num.setText("0"+jlb_bomb_num.getText());
}
}
}
}
if(ln == 0) Iswin();
}
}
}
(这个文件未完续下)