1
package game.sl;
2
import java.awt.*;
3
import java.util.Random;
4
import java.awt.event.*;
5
import javax.swing.JOptionPane;
6
public class MyShaoLei extends Frame implements ActionListener,MouseListener
7

{
8
9
public static void main(String[] args)
10
{
11
String[] values =
{ "简单", "一般", "高级" };
12
String value = (String) JOptionPane.showInputDialog(null,
13
"请选择难度", "难度选择",JOptionPane.INFORMATION_MESSAGE, null,
14
values, values[0]);
15
MyShaoLei msl1=new MyShaoLei(value);
16
}
17
int max;
18
int numLei;
19
int min;
20
Button[][] b;
21
String[][] lei;
22
Random a=new Random();
23
int numLei1=numLei,count,leiX,leiY,leiNum;//剩余雷,步数,雷的坐标x,雷坐标y,周围雷数
24
Button b1,b2,b3;
25
TextField tf1,tf2;
26
Panel p,p1;
27
Label l1,l2,l3,l4;
28
Image imghq ;
29
public MyShaoLei(String value)
30
{
31
32
if(value.equals("简单"))
33
{
34
max=9;
35
numLei=10;
36
this.setSize(300,350);
37
}
38
else if(value.equals("一般"))
39
{
40
max=16;
41
numLei=40;
42
this.setSize(400,470);
43
}
44
else if(value.equals("高级"))
45
{
46
max=22;
47
numLei=99;
48
this.setSize(500,570);
49
}
50
this.addWindowListener(new WindowAdapter()
51
{
52
public void windowClosing(WindowEvent e)
53
{
54
dispose();//释放资源
55
System.exit(0);
56
}
57
});
58
this.max=max;
59
this.numLei=numLei;
60
b=new Button[max][max];
61
lei=new String[max][max];
62
this.setLayout(new BorderLayout(20,20));//行,列,行间,列间
63
p=new Panel();
64
p.setLayout(new GridLayout(max,max,0,0));
65
for(int i=0;i<b.length;i++)
66
for(int j=0;j<b[i].length;j++)
67
{
68
b[i][j]=new Button();
69
b[i][j].addActionListener(this);
70
b[i][j].addMouseListener(this);
71
p.add(b[i][j]);
72
}
73
p1=new Panel();
74
p1.setLayout(new GridLayout(1,9,0,0));
75
tf1=new TextField(numLei);
76
tf2=new TextField("2");
77
b1=new Button("简单");
78
b1.addActionListener(this);
79
b2=new Button("一般");
80
b2.addActionListener(this);
81
b3=new Button("困难");
82
b3.addActionListener(this);
83
l1=new Label();
84
l2=new Label();
85
l3=new Label();
86
l4=new Label();
87
p1.add(tf1);
88
p1.add(l1);
89
p1.add(b1);
90
p1.add(l2);
91
p1.add(b2);
92
p1.add(l3);
93
p1.add(b3);
94
p1.add(l4);
95
p1.add(tf2);
96
add(p,BorderLayout.CENTER);
97
add(p1,BorderLayout.SOUTH);
98
99
this.setVisible(true);
100
101
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
102
int w = getSize().width;
103
int h = getSize().height;
104
int x = (dim.width-w)/2;
105
int y = (dim.height-h)/2;
106
setLocation(x,y);
107
108
begin();
109
110
}
111
public void begin()
112
{
113
this.tf2.setText("0");
114
for(int i=0;i<b.length;i++)
115
for(int j=0;j<b[i].length;j++)
116
{
117
b[i][j].setLabel(" ");
118
b[i][j].setBackground(Color.lightGray);
119
lei[i][j]=new String("");
120
}
121
for(int i=0;i<numLei;i++)
122
{
123
leiX=a.nextInt(max);
124
leiY=a.nextInt(max);
125
if(lei[leiX][leiY].equals("雷"))
126
i--;
127
else
128
lei[leiX][leiY]="雷";
129
}
130
for(int i=0;i<lei.length;i++)
131
for(int j=0;j<lei[i].length;j++)
132
{
133
if(!lei[i][j].equals("雷"))
134
{
135
for(int r=-1;r<=1;r++)
136
for(int c=-1;c<=1;c++)
137
{
138
if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max)
139
{
140
if(lei[i+r][j+c].equals("雷"))
141
leiNum++;
142
}
143
144
}
145
if(leiNum!=0)
146
lei[i][j]=""+leiNum;
147
leiNum=0;
148
}
149
150
}
151
}
152
public void death()
153
{
154
for(int i=0;i<max;i++)
155
for(int j=0;j<max;j++)
156
{
157
b[i][j].setLabel(lei[i][j]);
158
if(!lei[i][j].equals("雷"))
159
b[i][j].setBackground(Color.white);
160
}
161
if(JOptionPane.showConfirmDialog(null,"是否重新开始?", "请选择", JOptionPane.YES_NO_OPTION)==0)
162
{
163
begin();
164
}
165
else
166
System.exit(0);
167
}
168
public void win()
169
{
170
for(int i=0;i<max;i++)
171
for(int j=0;j<max;j++)
172
{
173
if(b[i][j].getLabel().equals(" "))
174
min++;
175
}
176
if(min==numLei)
177
{
178
if(JOptionPane.showConfirmDialog(null,"请选择", "是否重新开始?", JOptionPane.YES_NO_OPTION)==0)
179
{
180
begin();
181
}
182
else
183
System.exit(0);
184
}
185
else
186
min=0;
187
}
188
public void open(int i,int j)
189
{
190
for(int r=-1;r<=1;r++)
191
for(int c=-1;c<=1;c++)
192
{
193
if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max&&b[i+r][j+c].getLabel().equals(" "))
194
{
195
b[i+r][j+c].setBackground(Color.white);
196
b[i+r][j+c].setLabel(lei[i+r][j+c]);
197
if(b[i+r][j+c].getLabel().equals(""))
198
{
199
open(i+r,j+c);
200
}
201
}
202
}
203
}
204
public void actionPerformed(ActionEvent e)
205
{
206
for(int i=0;i<max;i++)
207
for(int j=0;j<max;j++)
208
if(e.getSource()==b[i][j])
209
{
210
if(b[i][j].getLabel().equals(" "))
211
tf2.setText(""+(++count));
212
if(lei[i][j].equals("雷"))
213
{
214
death();
215
}
216
else if(lei[i][j].equals(""))
217
{
218
System.out.println("周围无雷!");
219
open(i,j);
220
}
221
else
222
{
223
b[i][j].setLabel(lei[i][j]);
224
b[i][j].setBackground(Color.white);
225
}
226
win();
227
}
228
}
229
public void mousePressed(MouseEvent e)
{}
230
public void mouseReleased(MouseEvent e)
{}
231
public void mouseEntered(MouseEvent e)
{}
232
public void mouseExited(MouseEvent e)
{}
233
public void mouseClicked(MouseEvent e)
234
{
235
switch(e.getModifiers())
236
{
237
case InputEvent.BUTTON1_MASK:
238
{
239
//左键
240
break;
241
}
242
case InputEvent.BUTTON2_MASK:
243
{
244
//中键
245
break;
246
}
247
case InputEvent.BUTTON3_MASK:
248
{
249
if(((Button)e.getSource()).getLabel()==(" "))
250
{
251
((Button)e.getSource()).setLabel("!");
252
((Button)e.getSource()).setBackground(Color.red);
253
numLei1--;
254
tf1.setText(""+numLei1);
255
}
256
else if(((Button)e.getSource()).getLabel()==("!"))
257
{
258
((Button)e.getSource()).setLabel("?");
259
((Button)e.getSource()).setBackground(Color.yellow);
260
numLei1++;
261
tf1.setText(""+numLei1);
262
}
263
else if(((Button)e.getSource()).getLabel()==("?"))
264
{
265
((Button)e.getSource()).setLabel(" ");
266
((Button)e.getSource()).setBackground(Color.lightGray);
267
}
268
//((Button)e.getSource()).setBackground(Color.white);
269
break;
270
}
271
}
272
}
273
}
package game.sl;2
import java.awt.*;3
import java.util.Random;4
import java.awt.event.*;5
import javax.swing.JOptionPane;6
public class MyShaoLei extends Frame implements ActionListener,MouseListener7


{8
9
public static void main(String[] args)10

{11

String[] values =
{ "简单", "一般", "高级" };12
String value = (String) JOptionPane.showInputDialog(null, 13
"请选择难度", "难度选择",JOptionPane.INFORMATION_MESSAGE, null,14
values, values[0]);15
MyShaoLei msl1=new MyShaoLei(value);16
}17
int max;18
int numLei;19
int min;20
Button[][] b;21
String[][] lei;22
Random a=new Random();23
int numLei1=numLei,count,leiX,leiY,leiNum;//剩余雷,步数,雷的坐标x,雷坐标y,周围雷数24
Button b1,b2,b3;25
TextField tf1,tf2;26
Panel p,p1;27
Label l1,l2,l3,l4;28
Image imghq ;29
public MyShaoLei(String value)30

{31
32
if(value.equals("简单"))33

{34
max=9;35
numLei=10;36
this.setSize(300,350);37
}38
else if(value.equals("一般"))39

{40
max=16;41
numLei=40;42
this.setSize(400,470);43
}44
else if(value.equals("高级"))45

{46
max=22;47
numLei=99;48
this.setSize(500,570);49
}50
this.addWindowListener(new WindowAdapter()51

{52
public void windowClosing(WindowEvent e)53

{54
dispose();//释放资源55
System.exit(0);56
}57
});58
this.max=max;59
this.numLei=numLei;60
b=new Button[max][max];61
lei=new String[max][max];62
this.setLayout(new BorderLayout(20,20));//行,列,行间,列间63
p=new Panel();64
p.setLayout(new GridLayout(max,max,0,0));65
for(int i=0;i<b.length;i++)66
for(int j=0;j<b[i].length;j++)67

{68
b[i][j]=new Button();69
b[i][j].addActionListener(this);70
b[i][j].addMouseListener(this);71
p.add(b[i][j]);72
}73
p1=new Panel();74
p1.setLayout(new GridLayout(1,9,0,0));75
tf1=new TextField(numLei);76
tf2=new TextField("2");77
b1=new Button("简单");78
b1.addActionListener(this);79
b2=new Button("一般");80
b2.addActionListener(this);81
b3=new Button("困难");82
b3.addActionListener(this);83
l1=new Label();84
l2=new Label();85
l3=new Label();86
l4=new Label();87
p1.add(tf1);88
p1.add(l1);89
p1.add(b1);90
p1.add(l2);91
p1.add(b2);92
p1.add(l3);93
p1.add(b3);94
p1.add(l4);95
p1.add(tf2);96
add(p,BorderLayout.CENTER);97
add(p1,BorderLayout.SOUTH);98
99
this.setVisible(true);100
101
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小102
int w = getSize().width;103
int h = getSize().height;104
int x = (dim.width-w)/2;105
int y = (dim.height-h)/2;106
setLocation(x,y);107
108
begin();109
110
}111
public void begin()112

{113
this.tf2.setText("0");114
for(int i=0;i<b.length;i++)115
for(int j=0;j<b[i].length;j++)116

{117
b[i][j].setLabel(" ");118
b[i][j].setBackground(Color.lightGray);119
lei[i][j]=new String("");120
}121
for(int i=0;i<numLei;i++)122

{123
leiX=a.nextInt(max);124
leiY=a.nextInt(max);125
if(lei[leiX][leiY].equals("雷"))126
i--;127
else128
lei[leiX][leiY]="雷";129
}130
for(int i=0;i<lei.length;i++)131
for(int j=0;j<lei[i].length;j++)132

{133
if(!lei[i][j].equals("雷"))134

{135
for(int r=-1;r<=1;r++)136
for(int c=-1;c<=1;c++)137

{138
if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max)139

{140
if(lei[i+r][j+c].equals("雷"))141
leiNum++;142
}143
144
}145
if(leiNum!=0)146
lei[i][j]=""+leiNum;147
leiNum=0;148
}149
150
}151
}152
public void death()153

{154
for(int i=0;i<max;i++)155
for(int j=0;j<max;j++)156

{157
b[i][j].setLabel(lei[i][j]);158
if(!lei[i][j].equals("雷"))159
b[i][j].setBackground(Color.white);160
}161
if(JOptionPane.showConfirmDialog(null,"是否重新开始?", "请选择", JOptionPane.YES_NO_OPTION)==0)162

{163
begin();164
}165
else166
System.exit(0);167
}168
public void win()169

{170
for(int i=0;i<max;i++)171
for(int j=0;j<max;j++)172

{173
if(b[i][j].getLabel().equals(" "))174
min++;175
}176
if(min==numLei)177

{178
if(JOptionPane.showConfirmDialog(null,"请选择", "是否重新开始?", JOptionPane.YES_NO_OPTION)==0)179

{180
begin();181
}182
else183
System.exit(0);184
}185
else186
min=0;187
}188
public void open(int i,int j)189

{190
for(int r=-1;r<=1;r++)191
for(int c=-1;c<=1;c++)192

{193
if(i+r>=0&&i+r<max&&j+c>=0&&j+c<max&&b[i+r][j+c].getLabel().equals(" "))194

{195
b[i+r][j+c].setBackground(Color.white);196
b[i+r][j+c].setLabel(lei[i+r][j+c]);197
if(b[i+r][j+c].getLabel().equals(""))198

{199
open(i+r,j+c);200
}201
}202
}203
}204
public void actionPerformed(ActionEvent e)205

{206
for(int i=0;i<max;i++)207
for(int j=0;j<max;j++)208
if(e.getSource()==b[i][j])209

{210
if(b[i][j].getLabel().equals(" "))211
tf2.setText(""+(++count));212
if(lei[i][j].equals("雷"))213

{214
death();215
}216
else if(lei[i][j].equals(""))217

{218
System.out.println("周围无雷!");219
open(i,j);220
}221
else222

{223
b[i][j].setLabel(lei[i][j]);224
b[i][j].setBackground(Color.white);225
}226
win();227
}228
}229

public void mousePressed(MouseEvent e)
{} 230

public void mouseReleased(MouseEvent e)
{} 231

public void mouseEntered(MouseEvent e)
{} 232

public void mouseExited(MouseEvent e)
{} 233
public void mouseClicked(MouseEvent e) 234

{ 235
switch(e.getModifiers()) 236

{ 237
case InputEvent.BUTTON1_MASK: 238

{ 239
//左键240
break; 241
} 242
case InputEvent.BUTTON2_MASK: 243

{ 244
//中键 245
break; 246
} 247
case InputEvent.BUTTON3_MASK: 248

{ 249
if(((Button)e.getSource()).getLabel()==(" "))250

{251
((Button)e.getSource()).setLabel("!");252
((Button)e.getSource()).setBackground(Color.red);253
numLei1--;254
tf1.setText(""+numLei1);255
}256
else if(((Button)e.getSource()).getLabel()==("!"))257

{258
((Button)e.getSource()).setLabel("?");259
((Button)e.getSource()).setBackground(Color.yellow);260
numLei1++;261
tf1.setText(""+numLei1);262
}263
else if(((Button)e.getSource()).getLabel()==("?"))264

{265
((Button)e.getSource()).setLabel(" ");266
((Button)e.getSource()).setBackground(Color.lightGray);267
}268
//((Button)e.getSource()).setBackground(Color.white);269
break; 270
} 271
}272
}273
}

1762

被折叠的 条评论
为什么被折叠?



