




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java基于swing实现的连连看代码这篇文章主要介绍了java基于swing实现的连连看代码,包含了游戏中涉及的事件处理与逻辑功能,需要的朋友可以参考下本文实例讲述了java基于swing实现连连看代码。分享给大家供大家参考。 主要功能代码如下: 复制代码 代码如下: package llkan; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* * 连连看游戏 * author administrator *2014年10月17日 */ public class maingame implements
2、actionlistener jframe mainframe; / 主面板 container thiscontainer; jpanel centerpanel, southpanel, northpanel; / 子面板 jbutton diamondsbutton = new jbutton65;/ 游戏按钮数组 jbutton exitbutton, resetbutton, newlybutton; / 退出,重列,重新开始按钮 jlabel fractionlable = new jlabel(0); / 分数标签 jbutton firstbutton, secondbutto
3、n; / 分别记录两次被选中的按钮 int grid = new int87;/ 储存游戏按钮位置 static boolean pressinformation = false; / 判断是否有按钮被选中 int x0 = 0, y0 = 0, x = 0, y = 0, fristmsg = 0, secondmsg = 0, validatelv; / 游戏按钮的位置坐标 int i, j, k, n;/ 消除方法控制 public void init() mainframe = new jframe(连连看游戏); thiscontainer = mainframe.getconten
4、tpane(); thiscontainer.setlayout(new borderlayout(); centerpanel = new jpanel(); southpanel = new jpanel(); northpanel = new jpanel(); thiscontainer.add(centerpanel, center); thiscontainer.add(southpanel, south); thiscontainer.add(northpanel, north); centerpanel.setlayout(new gridlayout(6, 5); for (
5、int cols = 0; cols 6; cols+) for (int rows = 0; rows 5; rows+) diamondsbuttoncolsrows = new jbutton( string.valueof(gridcols + 1rows + 1); diamondsbuttoncolsrows.addactionlistener(this); centerpanel.add(diamondsbuttoncolsrows); exitbutton = new jbutton(退出); exitbutton.addactionlistener(this); resetb
6、utton = new jbutton(重列); resetbutton.addactionlistener(this); newlybutton = new jbutton(再来一局); newlybutton.addactionlistener(this); southpanel.add(exitbutton); southpanel.add(resetbutton); southpanel.add(newlybutton); fractionlable.settext(string.valueof(integer.parseint(fractionlable .gettext(); no
7、rthpanel.add(fractionlable); mainframe.setbounds(280, 100, 500, 450); mainframe.setvisible(true); public void randombuild() int randoms, cols, rows; for (int twins = 1; twins = 15; twins+) randoms = (int) (math.random() * 25 + 1); for (int alike = 1; alike = 2; alike+) cols = (int) (math.random() *
8、6 + 1); rows = (int) (math.random() * 5 + 1); while (gridcolsrows != 0) cols = (int) (math.random() * 6 + 1); rows = (int) (math.random() * 5 + 1); this.gridcolsrows = randoms; public void fraction() fractionlable.settext(string.valueof(integer.parseint(fractionlable .gettext() + 100); public void r
9、eload() int save = new int30; int n = 0, cols, rows; int grid = new int87; for (int i = 0; i = 6; i+) for (int j = 0; j = 0) cols = (int) (math.random() * 6 + 1); rows = (int) (math.random() * 5 + 1); while (gridcolsrows != 0) cols = (int) (math.random() * 6 + 1); rows = (int) (math.random() * 5 + 1
10、); this.gridcolsrows = saven; n-; mainframe.setvisible(false); pressinformation = false; / 这里一定要将按钮点击信息归为初始 init(); for (int i = 0; i 6; i+) for (int j = 0; j 5; j+) if (gridi + 1j + 1 = 0) diamondsbuttonij.setvisible(false); public void estimateeven(int placex, int placey, jbutton bz) if (pressinfo
11、rmation = false) x = placex; y = placey; secondmsg = gridxy; secondbutton = bz; pressinformation = true; else x0 = x; y0 = y; fristmsg = secondmsg; firstbutton = button; x = placex; y = placey; secondmsg = gridxy; secondbutton = bz; if (fristmsg = secondmsg & secondbutton != firstbutton) xiao(); pub
12、lic void xiao() / 相同的情况下能不能消去。仔细分析,不一条条注释 if (x0 = x & (y0 = y + 1 | y0 = y - 1) | (x0 = x + 1 | x0 = x - 1) & (y0 = y) / 判断是否相邻 remove(); else for (j = 0; j j) / 如果第二个按钮的y坐标大于空按钮的y坐标说明第一按钮在第二按钮左边 for (i = y - 1; i = j; i-) / 判断第二按钮左侧直到第一按钮中间有没有按钮 if (gridxi != 0) k = 0; break; else k = 1; / k=1说明通过
13、了第一次验证 if (k = 1) linepassone(); if (y j) / 如果第二个按钮的y坐标小于空按钮的y坐标说明第一按钮在第二按钮右边 for (i = y + 1; i = j; i+) / 判断第二按钮左侧直到第一按钮中间有没有按钮 if (gridxi != 0) k = 0; break; else k = 1; if (k = 1) linepassone(); if (y = j) linepassone(); if (k = 2) if (x0 = x) remove(); if (x0 x) for (n = x0; n x) for (n = x0; n
14、= x + 1; n-) if (gridnj != 0) k = 0; break; if (gridnj = 0 & n = x + 1) remove(); for (i = 0; i i) for (j = x - 1; j = i; j-) if (gridjy != 0) k = 0; break; else k = 1; if (k = 1) rowpassone(); if (x i) for (j = x + 1; j = i; j+) if (gridjy != 0) k = 0; break; else k = 1; if (k = 1) rowpassone(); if
15、 (x = i) rowpassone(); if (k = 2) if (y0 = y) remove(); if (y0 y) for (n = y0; n y) for (n = y0; n = y + 1; n-) if (gridin != 0) k = 0; break; if (gridin = 0 & n = y + 1) remove(); public void linepassone() if (y0 j) / 第一按钮同行空按钮在左边 for (i = y0 - 1; i = j; i-) / 判断第一按钮同左侧空按钮之间有没按钮 if (gridx0i != 0) k
16、 = 0; break; else k = 2; / k=2说明通过了第二次验证 if (y0 j) / 第一按钮同行空按钮在与第二按钮之间 for (i = y0 + 1; i i) for (j = x0 - 1; j = i; j-) if (gridjy0 != 0) k = 0; break; else k = 2; if (x0 i) for (j = x0 + 1; j = i; j+) if (gridjy0 != 0) k = 0; break; else k = 2; public void remove() firstbutton.setvisible(false); s
17、econdbutton.setvisible(false); fraction(); pressinformation = false; k = 0; gridx0y0 = 0; gridxy = 0; public void actionperformed(actionevent e) if (e.getsource() = newlybutton) int grid = new int87; this.grid = grid; randombuild(); mainframe.setvisible(false); pressinformation = false; init(); if (e.getsource() = exitbutton) system.exit(0); if (e.getsource() = resetbutton) relo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 北京镁合金项目可行性研究报告
- 以计算思维为导向:高中程序设计课程的创新与实践
- 药市场深度评估与发展趋势预测报告(定制版)
- 2025年中国工艺流程隔膜泵行业市场发展前景及发展趋势与投资战略研究报告
- 年回收5000吨废线路板项目立项报告书-可行性研究报告
- 中国氢燃料电池乘用车行业市场竞争格局及发展趋势预测报告
- 2025年中国水上豪华度假船行业市场发展前景及发展趋势与投资战略研究报告
- 2025年中国护脐包行业市场发展前景及发展趋势与投资战略研究报告
- 防碱封底漆项目投资可行性研究分析报告(2024-2030版)
- 中国APP手机软件市场全面调研及行业投资潜力预测报告
- 青海省西宁市(2024年-2025年小学四年级语文)人教版质量测试(下学期)试卷及答案
- 2024足球竞赛规则全套解读
- SMAW和GTAW焊接作业焊接工艺
- 大沥中学初一分班考试卷
- 正式委托代理合同范本
- 苏教版2023-2024学年小学五年级下册数学暑假练习(假期作业)合集21份
- 生物-湖南省邵阳市2023-2024学年高二下学期7月期末联考试题和答案
- 西师版三年级下册数学练习册思维拓展解决问题
- 软件开发项目进度管理
- CNAS-CL01-2018内审检查记录表
- 2024年中级经济师考试题库含答案(a卷)
评论
0/150
提交评论