开公司先建设网站,个人网页模板关于爱国,wordpress 5.2.2,全国外贸公司联系方式1. 扫雷游戏的构思#xff1a; 设计为初级#xff0c;中级#xff0c;高级三个级别。 因此不妨设置为如下规格#xff1a; 9x9 16x15和30x16 #xff08;行#xff0c;列#xff09;规格不同地雷的数量也不同#xff0c;分别为 10#xff0c;40 #xff0c;99 2.在…1. 扫雷游戏的构思 设计为初级中级高级三个级别。 因此不妨设置为如下规格 9x9 16x15和30x16 行列规格不同地雷的数量也不同分别为 1040 99 2.在这个过程遇到了按钮大小调整调用这些函数 1 btn.setBounds(x,y,width,height);//设置大小并定位
2 或者
3 btn.setSize(width,height);//设置大小
4 btn.setLocation(x,y);//定位
5
6 父容器的layout要设置为null】/*1。按钮之间的设置问题如何解决调用函数 public void setMargin(Insets m)设置按钮边框和标签之间的空白。将该空白设置为 null 会造成按钮使用默认空白。按钮的默认 Border 对象将使用该值来创建适当的空白。不过如果在按钮上设置非默认边框则由 Border 对象负责创建适当的空白否则此属性将被忽略。
参数m - 边框和标签之间的间隔 制作到这默认框架已经搭建好了并附上劣质代码来加以显示 代码如下 竹类 1 package Scan_boobs;
2
3 public class Main {
4 static public void main(String args[] )
5 {
6 new window_scan();
7 }
8 } Main 框架类 1 /*扫雷默认为9x9的方格2 * */3 package Scan_boobs;4 import java.awt.*;5 import javax.swing.*;6 7 import java.awt.BorderLayout;8 import java.awt.FlowLayout ;9 import java.awt.Container ;10 import java.awt.Font;11 import java.awt.Insets;12 import java.awt.event.ActionEvent;13 import java.awt.event.ActionListener;14 15 import javax.swing.JPanel ;16 public class window_scan extends JFrame implements ActionListener 17 { 18 19 private static final long serialVersionUID 1L; 20 private final int row 9;21 private final int cow 9;22 JFrame wind ; 23 JMenuBar myMenubar; //菜单条24 JMenu Menu []; //菜单25 JMenuItem [] Submenu ; //子菜单26 JButton mybutton; //开始按钮27 JButton [] grid_button ; //扫雷里面的按钮28 Container mycontainer;29 /*30 * 添加到容器中的组件放在一个列表中。列表的顺序31 * 将定义组件在容器内的正向堆栈顺序。如果将组件32 * 添加到容器中时未指定索引则该索引将被添加到列表尾部33 * 此后它位于堆栈顺序的底部。 34 * */35 JPanel jpanel,jpanel1,jpanel2,jpamel3; /*JPanel 是一般轻量级容器。*/36 /*37 * 设置三个等级初级中级高级 38 */39 window_scan()40 { 41 setTitle(扫雷);42 setVisible(true); //设置窗口是否可见43 setResizable(false); //大小不可更改44 setBounds(400,100,400,500); //初始的位置400,100大小(400,500)45 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 46 //初始 47 mycontainergetContentPane(); //这个获取目前的 48 init();49 }50 void init() {51 52 Submenu new JMenuItem [3];53 Submenu[0]new JMenuItem(初级);54 Submenu[1]new JMenuItem(中级);55 Submenu[2]new JMenuItem(高级);56 Menu new JMenu [2];57 Menu[0] new JMenu(设置);58 for(int i0;i3;i)59 {60 Submenu[i].addActionListener(this); //监听61 Menu[0].add(Submenu[i]);62 }63 mybuttonnew JButton(开始); //开始按钮64 mybutton.addActionListener(this); 65 Menu[1] new JMenu(帮助);66 Menu[1].addActionListener(this);67 myMenubar new JMenuBar(); 68 //setJMenuBar(myMenubar); //设置此窗体的菜单栏69 jpanel new JPanel(); 70 jpanel.add(myMenubar);71 myMenubar.add(Menu[0]);72 myMenubar.add(Menu[1]);73 jpanel1 new JPanel();74 jpanel1.setLayout(new BorderLayout()); //设置布局 75 jpanel1.add(jpanel,BorderLayout.NORTH);76 jpanel1.add(mybutton,BorderLayout.CENTER);77 //设置雷区域78 jpanel2 new JPanel();79 jpanel2.setLayout(new GridLayout(row,cow,0,0)); //网格布局80 grid_button new JButton [row*cow];81 for(int i0; irow*cow ; i)82 {83 grid_button[i] new JButton();84 grid_button[i].setMargin(new Insets(0, 0, 0, 0)); 85 /*86 *象是容器边界的表示形式。它指定容器必须在87 *其各个边缘留出的空间。这个空间可以是边界、88 *空白空间或标题。 89 * */90 grid_button[i].setFont(new Font(null,Font.BOLD,25));//设置此容器的字体91 grid_button[i].addActionListener(this); //行为监视容器92 grid_button[i].addMouseListener(new MyMouseEvent());93 jpanel2.add(grid_button[i]);94 }95 mycontainer.add(jpanel1,BorderLayout.NORTH);96 mycontainer.add(jpanel2,BorderLayout.CENTER);97 98 }99 Override
100 public void actionPerformed(ActionEvent e) {
101 // TODO Auto-generated method stub
102
103 }
104 }
105 Window_scan 消息映射类 代码 1 package Scan_boobs;2 3 import java.awt.event.MouseEvent;4 import java.awt.event.MouseListener;5 6 public class MyMouseEvent implements MouseListener {7 8 Override9 public void mouseClicked(MouseEvent e) {
10 // TODO Auto-generated method stub
11
12 }
13
14 Override
15 public void mouseEntered(MouseEvent e) {
16 // TODO Auto-generated method stub
17
18 }
19
20 Override
21 public void mouseExited(MouseEvent e) {
22 // TODO Auto-generated method stub
23
24 }
25
26 Override
27 public void mousePressed(MouseEvent e) {
28 // TODO Auto-generated method stub
29
30 }
31
32 Override
33 public void mouseReleased(MouseEvent e) {
34 // TODO Auto-generated method stub
35
36 }
37
38 } MyMouseEvent 下面开始导入算法部分 1算法一 布雷的思路: (2 )算法二 挖雷的思路 设置为5中状态 01234 抽象出来就是打开一个格子如果里边的数字是0周围没有雷的话就打开周围的8个格子并继续搜索打开的8个格子中是否有0如果有就重复...直到打开一片为0的区域如果存在的话。学习知识点注意一点 java中 1无法等同与true Math.random()产生一个[01)之间的随机数。暂且做到这个地方代码 1 package Scan_boobs;
2
3 public class Main
4 {
5 static public void main(String args[] )
6 {
7 new window_scan();
8 }
9 } Main 1 /*扫雷默认为9x9的方格2 * */3 package Scan_boobs;4 import java.awt.*;5 import javax.swing.*;6 7 import java.awt.BorderLayout;8 import java.awt.FlowLayout ;9 import java.awt.Container ;10 import java.awt.Font;11 import java.awt.Insets;12 import java.awt.event.ActionEvent;13 import java.awt.event.ActionListener;14 import javax.swing.JPanel ;15 public class window_scan extends JFrame implements ActionListener 16 { 17 18 private static final long serialVersionUID 1L; 19 public static int row 9;20 public static int cow 9;21 JFrame wind ; 22 JMenuBar myMenubar; //菜单条23 JMenu Menu []; //菜单24 JMenuItem [] Submenu ; //子菜单25 JButton mybutton; //开始按钮26 public static JButton [] grid_button ; //扫雷里面的按钮27 Container mycontainer;28 public static int [][] array; //相应格子的状态29 public static int [] boombs; //炸弹的位置30 public static int boombs_number ; //炸弹的数量31 32 /*33 * 添加到容器中的组件放在一个列表中。列表的顺序34 * 将定义组件在容器内的正向堆栈顺序。如果将组件35 * 添加到容器中时未指定索引则该索引将被添加到列表尾部36 * 此后它位于堆栈顺序的底部。 37 * */38 39 JPanel jpanel,jpanel1,jpanel2,jpamel3; /*JPanel 是一般轻量级容器。*/40 41 /*42 * 设置三个等级初级中级高级 43 */44 45 window_scan()46 { 47 setTitle(扫雷);48 setVisible(true); //设置窗口是否可见49 setResizable(false); //大小不可更改50 setBounds(400,100,400,500); //初始的位置400,100大小(400,500)51 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 52 //初始 53 mycontainergetContentPane(); //这个获取目前的 54 wind_init();55 }56 57 void wind_init() {58 59 Submenu new JMenuItem [3];60 Submenu[0]new JMenuItem(初级);61 Submenu[1]new JMenuItem(中级);62 Submenu[2]new JMenuItem(高级);63 Menu new JMenu [2];64 Menu[0] new JMenu(设置);65 for(int i0;i3;i)66 {67 Submenu[i].addActionListener(this); //监听68 Menu[0].add(Submenu[i]);69 }70 mybuttonnew JButton(开始); //开始按钮71 mybutton.addActionListener(this); 72 Menu[1] new JMenu(帮助);73 Menu[1].addActionListener(this);74 myMenubar new JMenuBar(); 75 //setJMenuBar(myMenubar); //设置此窗体的菜单栏76 jpanel new JPanel(); 77 jpanel.add(myMenubar);78 myMenubar.add(Menu[0]);79 myMenubar.add(Menu[1]);80 jpanel1 new JPanel();81 jpanel1.setLayout(new BorderLayout()); //设置布局 82 jpanel1.add(jpanel,BorderLayout.NORTH);83 jpanel1.add(mybutton,BorderLayout.CENTER);84 grid_button(9,9,0); //初始炸弹设置为〇 85 mycontainer.add(jpanel1,BorderLayout.NORTH);86 }87 88 Override89 public void actionPerformed(ActionEvent e) {90 // TODO Auto-generated method stub91 if(e.getActionCommand()初级)92 {93 rowcow9;94 array new int [row][cow] ; //设置数组大小95 //撤销原先的按钮分布使其失效96 this.remove(jpanel2); 97 grid_button(row,cow,10);98 this.pack();99 //初始化状态
100 this.init(row,cow);
101 }
102 else if(e.getActionCommand()中级)
103 {
104 row16;
105 cow15;
106 array new int [row][cow];
107 this.remove(jpanel2);
108 grid_button(row,cow,40);
109 this.pack();
110 //初始化状态
111 this.init(row,cow);
112 }
113 else if(e.getActionCommand()高级)
114 {
115 row30;
116 cow16;
117 arraynew int [row2][cow2];
118 this.remove(jpanel2);
119 grid_button(row,cow,40);
120 this.pack();
121 //初始化状态
122 this.init(row,cow);
123 }
124 if(e.getSource()mybutton) //点击了开始按钮
125 {
126
127
128 this.init(row,cow);
129 //设置炸弹随机安放----这里需要考虑的是产生不同数字
130
131 boolean [] check_position new boolean [row*cow3];
132 for(int i0; iboombs_number; i)
133 {
134 while(true)
135 {
136 int temp_val(int)(Math.random()*(row*cow)); //[0,1]*(row*cow);
137 if(check_position[temp_val]false)
138 {
139 check_position[temp_val]true;
140 boombs[i]temp_val;
141 break;
142 }
143 }
144 }
145 //安装炸弹哇咔咔果然想想都很有趣...........
146 //首先转化为x,y的坐标
147 int x,y;
148 for(int i0;iboombs_number; i){
149 xboombs[i]/row;
150 yboombs[i]%row;
151 array[x][y]50; //将这个定义为炸弹的状态
152 }
153 // for(int i0;irow2;i){
154 // for(int j0;jcow2;j){
155 // if(i0||j0||irow1||jcow1){
156 // array[i][j]0;
157 // }
158 // }
159 // }
160
161 //参考别人的代码实在是有点想不到这个算法怎么弄思密达
162 for(int i1;irow;i)
163 {
164 for(int j1;jcow;j)
165 {
166 if(array[i][j]!50)
167 {
168 for(int lj-1;lj1;l)
169 {
170 if(array[i-1][l]50) array[i][j];
171 if(array[i1][l]50) array[i][j];
172 }
173 if(array[i][j-1]50) array[i][j];
174 if(array[i][j1]50) array[i][j];
175 }
176 }
177 }
178 }
179 for(int i0;icow*row;i){
180 if(grid_button[i].getText()!★)
181 {
182 int xi/cow1;
183 int yi%cow1;
184 if(e.getSource()grid_button[i]array[x][y]100){
185 grid_button[i].setText(★);
186 grid_button[i].setEnabled(false);
187 array[x][y]10;
188 for(int k0;kcow*row;k){
189 int m1k/cow1;
190 int n1k%cow1;
191 if(array[m1][n1]!10grid_button[k].getText()★){
192 grid_button[k].setText(*o*);
193 }
194 }
195 for(int j0;jcow*row;j){
196 int mj/cow1;
197 int nj%cow1;
198 if(array[m][n]100){
199 grid_button[j].setText(★);
200 grid_button[j].setEnabled(false);
201 }
202 grid_button[j].setEnabled(false);
203 array[m][n]10;
204 }
205 }
206 else if(e.getSource()grid_button[i]){
207 if(array[x][y]0){
208 wa_lei(array,grid_button,e,i,x,y);
209 array[x][y]10;
210 grid_button[i].setEnabled(false);
211 }
212 if(array[x][y]!0array[x][y]!10){
213 grid_button[i].setText(array[x][y]);
214 grid_button[i].setEnabled(false);
215 array[x][y]10;
216 }
217 }
218 }else if(grid_button[i].getText()★){
219 }
220 }
221 }
222 /*************布置按钮方法**************************/
223 public void grid_button(int row ,int cow ,int boombs_number)
224 {
225 //设置雷区域
226 jpanel2 new JPanel();
227 jpanel2.setLayout(new GridLayout(row,cow,0,0)); //网格布局
228 grid_button new JButton [row*cow];
229 boombs new int [boombs_number];
230 for(int i0; irow*cow ; i)
231 {
232 grid_button[i] new JButton( ); //有点空格将按钮弄大些
233 grid_button[i].setMargin(new Insets(0, 0, 0, 0));
234 /*
235 *象是容器边界的表示形式。它指定容器必须在
236 *其各个边缘留出的空间。这个空间可以是边界、
237 *空白空间或标题。
238 * */
239 grid_button[i].setFont(new Font(null,Font.BOLD,25));//设置此容器的字体
240 grid_button[i].addActionListener(this); //行为监视容器
241 grid_button[i].addMouseListener(new MyMouseEvent());
242 // grid_button[i].setText();
243 // grid_button[i].setEnabled(true);
244 jpanel2.add(grid_button[i]);
245 }
246 mycontainer.add(jpanel2,BorderLayout.CENTER);
247 }
248 /***************初始化状态****************/
249 public void init(int row,int cow)
250 {
251 for(int i0;irow2;i){
252 for(int j0;jcow2;j){
253 array[i][j]0;
254 }
255 }
256 for(int i0;icow*row;i){
257 grid_button[i].setText();
258 grid_button[i].setEnabled(true);
259 }
260 }
261 public void wa_lei(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){
262 int p1;
263 if(a[x][y]0){
264 a[x][y]10;
265 btns[i].setEnabled(false); //33
266 for(int ly-1;ly1;l){
267 int mx-1-1;
268 int nl-1;
269 p1;
270 System.out.println(a[1][2]);
271 if(n-1ncowm-1mrow)
272 {
273 for(int q0;qrowp1;q){//cow--row;
274 if(((ncow*q)(m*cow))((ncow*q)(m1)*cow)){
275 if(a[x-1][l]!0a[x-1][l]!10){
276 btns[ncow*q].setText(a[x-1][l]);
277 a[x-1][l]10;
278 btns[ncow*q].setEnabled(false);
279 }
280 else if(a[x-1][l]0){
281 //a[x-1][l]10;
282 btns[ncow*q].setEnabled(false);
283 wa_lei(a,btns,e,ncow*q,x-1,l); ////55
284 a[x-1][l]10;
285 btns[ncow*q].setEnabled(false);
286 }
287 p0;
288
289 }
290 }
291 }
292 p1;
293 mx;
294 if(n-1ncowm-1mcow)
295 {
296 for(int q0;qrowp1;q){
297 if(((ncow*q)(m*cow))((ncow*q)(m1)*cow)){
298 if(a[x1][l]!0a[x1][l]!10){
299 btns[ncow*q].setText(a[x1][l]);
300 a[x1][l]10;
301 btns[ncow*q].setEnabled(false);
302 }
303 else if(a[x1][l]0){
304
305 wa_lei(a,btns,e,ncow*q,x1,l);///55
306 a[x1][l]10;
307 btns[ncow*q].setEnabled(false);
308 }
309 p0;
310 }
311 }
312
313 }
314 }
315 int mx-1;
316 int ny-1-1;
317 p1;
318 if(n-1ncowm-1mcow)
319 {
320 for(int q0;qrowp1;q){
321 if(((ncow*q)(m*cow))((ncow*q)(m1)*cow)){
322 if(a[x][y-1]!0a[x][y-1]!10){
323 btns[ncow*q].setText(a[x][y-1]);
324 a[x][y-1]10;
325 btns[ncow*q].setEnabled(false);
326 }
327 else if(a[x][y-1]0){
328
329
330 wa_lei(a,btns,e,ncow*q,x,y-1);
331
332 a[x][y-1]10;
333 btns[ncow*q].setEnabled(false);
334 }
335 p0;
336 }
337 }
338 }
339 p1;
340 mx-1;
341 ny1-1;
342 if(n-1ncowm-1mcow)
343 {
344 for(int q0;qrowp1;q){
345 if(((ncow*q)(m*cow))((ncow*q)(m1)*cow)){
346 if(a[x][y1]!0a[x][y1]!10){
347 btns[ncow*q].setText(a[x][y1]);
348 a[x][y1]10;
349 btns[ncow*q].setEnabled(false);
350 }
351 else if(a[x][y1]0){
352 wa_lei(a,btns,e,ncow*q,x,y1);
353 a[x][y1]10;
354 btns[ncow*q].setEnabled(false);
355 }
356 p0;
357 }
358 }
359 }
360 }
361
362 }
363
364 } window_scan 1 package Scan_boobs;2 3 import java.awt.event.MouseEvent;4 import java.awt.event.MouseListener;5 6 public class MyMouseEvent implements MouseListener {7 Override8 public void mouseClicked(MouseEvent e) {9 // TODO Auto-generated method stub
10 for(int i0;iwindow_scan.cow*window_scan.row;i){
11 int x1i/window_scan.cow1;
12 int y1i%window_scan.cow1;
13 if(e.getSource()window_scan.grid_button[i]window_scan.grid_button[i].getText()!★window_scan.array[x1][y1]!10)
14 {
15 if(e.getButton()MouseEvent.BUTTON3){
16 window_scan.grid_button[i].setText(★);
17 window_scan.boombs_number--;
18 if(window_scan.boombs_number0){
19 int flag0;
20 for(int j0;jwindow_scan.cow*window_scan.row;j){
21 int xj/window_scan.cow;
22 int yj%window_scan.cow;
23 if(window_scan.array[x][y]100window_scan.grid_button[j].getText()★){
24 flag;
25 }
26 }
27 }
28 }
29 }
30
31 else
32 if(e.getSource()window_scan.grid_button[i]window_scan.grid_button[i].getText()★window_scan.array[x1][y1]!-1){
33 if(e.getButton()MouseEvent.BUTTON3){
34 window_scan.grid_button[i].setText();
35 window_scan.boombs_number;
36 window_scan.grid_button[i].setEnabled(true);
37 }
38 }
39 }
40
41 }
42
43 Override
44 public void mouseEntered(MouseEvent e) {
45 // TODO Auto-generated method stub
46
47 }
48
49 Override
50 public void mouseExited(MouseEvent e) {
51 // TODO Auto-generated method stub
52
53 }
54
55 Override
56 public void mousePressed(MouseEvent e) {
57 // TODO Auto-generated method stub
58
59 }
60
61 Override
62 public void mouseReleased(MouseEvent e) {
63 // TODO Auto-generated method stub
64
65 }
66
67 } 手标监视 转载于:https://www.cnblogs.com/gongxijun/p/3855378.html