/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*/
package system.test1;
/**** @author 张修*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class author implements ActionListener
{JFrame j=new JFrame("关于游戏");JLabel j1=new JLabel("author:zx");JLabel j2=new JLabel("email:1234");public void init(){j.setBounds(320, 100, 250, 225);j.setLayout(null);j.add(j1);j1.setBounds(40,20,200,100);j2.setBounds(40,60,200,100);j.add(j2);j.getContentPane().setBackground(Color.pink);j.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {new author().init();}}
class degree extends JFrame implements ActionListener{JFrame jf=new JFrame("模式");JButton b1=new JButton("简单");JButton b2=new JButton("困难");JButton b3=new JButton("关于游戏");public void init(){jf.setLayout(null);b1.setBounds(150,30,200,100);b1.setBackground(Color.green);b2.setBounds(150,150,200,100);b2.setBackground(Color.red);b3.setBounds(150,270,200,100);b3.setBackground(Color.pink);jf.add(b1);jf.add(b2);jf.add(b3);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(new author());jf.setBounds(280, 100, 500, 450);//jf.setBackground(Color.red);jf.getContentPane().setBackground(Color.black);jf.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == b1){jf.setVisible(false);lianliankan l=new lianliankan();l.randomBuild();l.init();}if(e.getSource()==b2){jf.setVisible(false);lianliankan2 l1=new lianliankan2();l1.randomBuild();l1.init();}}}class lianliankan extends degree implements ActionListener {static int a=8;public lianliankan(){}public lianliankan(int a){this.a=a;}JFrame mainFrame;Container thisContainer;JPanel centerPanel, southPanel, northPanel;JButton diamondsButton[][] = new JButton[a][a];JButton exitButton, resetButton, newlyButton;JLabel fractionLable = new JLabel("0");JButton firstButton, secondButton;int grid[][] = new int[a+2][a+2];static boolean pressInformation = false;int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0;int i, j, k, n;public void init() {mainFrame = new JFrame("连连看");thisContainer = mainFrame.getContentPane();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(a, a));for (int cols = 0; cols < a; cols++) {for (int rows = 0; rows < a; rows++) {diamondsButton[cols][rows] = new JButton(String.valueOf(grid[cols + 1][rows + 1]));diamondsButton[cols][rows].addActionListener(this);diamondsButton[cols][rows].setBackground(Color.YELLOW);centerPanel.add(diamondsButton[cols][rows]);}}exitButton = new JButton("退出");exitButton.addActionListener(this);resetButton = new JButton("重列");resetButton.addActionListener(this);southPanel.add(exitButton);southPanel.add(resetButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));//记录分数northPanel.add(fractionLable);mainFrame.setBounds(525, 100, 750, 750);//mainFrame.getContentPane().setBackground(Color.green);mainFrame.setVisible(true);}public void randomBuild() {int randoms, cols, rows;for (int twins = 1; twins <= a*a/2; twins++) {randoms = (int) (Math.random() * 9 + 1);for (int alike = 1; alike <= 2; alike++) {cols = (int) (Math.random() * a +1);//避免重复两次循环rows = (int) (Math.random() * a +1);while (grid[cols][rows] != 0) {//再次赋值cols = (int) (Math.random() * a+1 );rows = (int) (Math.random() * a +1);}this.grid[cols][rows] = randoms;}}}public void fraction() {//计分方法fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()) + 100));}public void reload() {//重排到每个二维数组中int save[] = new int[a*a];int n = 0, cols, rows;int grid[][] = new int[a+2][a+2];for (int i = 0; i <= a; i++) {for (int j = 0; j <= a; j++) {if (this.grid[i][j] != 0) {//循环二维数组如果成立赋值save[n] = this.grid[i][j];n++;}}}n = n - 1;this.grid = grid;while (n >= 0) {cols = (int) (Math.random() * a + 1);rows = (int) (Math.random() * a + 1);while (grid[cols][rows] != 0) {cols = (int) (Math.random() * a + 1);rows = (int) (Math.random() * a + 1);}this.grid[cols][rows] = save[n];n--;}mainFrame.setVisible(false);pressInformation = false; // 要将按钮点击信息归为初始init();for (int i = 0; i < a; i++) {for (int j = 0; j < a; j++) {if (grid[i + 1][j + 1] == 0) {diamondsButton[i][j].setVisible(false);//边界未赋值按钮设为不可见}}}}public void estimateEven(int placeX, int placeY, JButton bz) {//检测第一个按钮按了吗 估计数estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);if (pressInformation == false) {x = placeX;y = placeY;secondMsg = grid[x][y];secondButton = bz;pressInformation = true;} else {x0 = x;y0 = y;fristMsg = secondMsg;firstButton = secondButton;x = placeX;y = placeY;secondMsg = grid[x][y];secondButton = bz;if (fristMsg == secondMsg && secondButton != firstButton) {//信息相等按钮不同消去xiao();}}}public 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 < a+2; j++) {if (grid[x0][j] == 0) { // 判断第一个按钮同行哪个按钮为空if (y > j) { // 如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for (i = y - 1; i >= j; i--) { // 判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i] != 0) {k = 0;break;} else {k = 1;} // K=1说明通过了第一次验证}if (k == 1) {linePassOne();}}if (y < j) { // 如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for (i = y + 1; i <= j; i++) { // 判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i] != 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 - 1; n++) {if (grid[n][j] != 0) {k = 0;break;}if (grid[n][j] == 0 && n == x - 1) {remove();}}}if (x0 > x) {for (n = x0; n >= x + 1; n--) {if (grid[n][j] != 0) {k = 0;break;}if (grid[n][j] == 0 && n == x + 1) {remove();}}}}}for (i = 0; i < a+2; i++) { // 列if (grid[i][y0] == 0) {if (x > i) {for (j = x - 1; j >= i; j--) {if (grid[j][y] != 0) {k = 0;break;} else {k = 1;}}if (k == 1) {rowPassOne();}}if (x < i) {for (j = x + 1; j <= i; j++) {if (grid[j][y] != 0) {k = 0;break;} else {k = 1;}}if (k == 1) {rowPassOne();}}if (x == i) {rowPassOne();}}if (k == 2) {if (y0 == y) {remove();}if (y0 < y) {for (n = y0; n <= y - 1; n++) {if (grid[i][n] != 0) {k = 0;break;}if (grid[i][n] == 0 && n == y - 1) {remove();}}}if (y0 > y) {for (n = y0; n >= y + 1; n--) {if (grid[i][n] != 0) {k = 0;break;}if (grid[i][n] == 0 && n == y + 1) {remove();}}}}}}}public void linePassOne() {if (y0 > j) { // 第一按钮同行空按钮在左边for (i = y0 - 1; i >= j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮if (grid[x0][i] != 0) {k = 0;break;} else {k = 2;} // K=2说明通过了第二次验证}}if (y0 < j) { // 第一按钮同行空按钮在与第二按钮之间for (i = y0 + 1; i <= j; i++) {if (grid[x0][i] != 0) {k = 0;break;} else {k = 2;}}}}public void rowPassOne() {if (x0 > i) {for (j = x0 - 1; j >= i; j--) {if (grid[j][y0] != 0) {k = 0;break;} else {k = 2;}}}if (x0 < i) {for (j = x0 + 1; j <= i; j++) {if (grid[j][y0] != 0) {k = 0;break;} else {k = 2;}}}}public void remove() {//设置两个按钮不可见 加分firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation = false;k = 0;grid[x0][y0] = 0;grid[x][y] = 0;}public void actionPerformed(ActionEvent e) {if (e.getSource() == exitButton) {System.exit(0);}if (e.getSource() == resetButton) {reload();}for (int cols = 0; cols < a; cols++) {for (int rows = 0; rows < a; rows++) {if (e.getSource() == diamondsButton[cols][rows]) {estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);}}}}
}
class lianliankan2 extends lianliankan
{public void randomBuild() {int randoms, cols, rows;for (int twins = 1; twins <= a*a/2; twins++) {randoms = (int) (Math.random() * 25 + 1);for (int alike = 1; alike <= 2; alike++) {cols = (int) (Math.random() * a +1);rows = (int) (Math.random() * a +1);while (grid[cols][rows] != 0) {cols = (int) (Math.random() * a+1 );rows = (int) (Math.random() * a +1);}this.grid[cols][rows] = randoms;}}}
}public class lianliankantest{public static void main(String[] args) {// TODO code application logic herenew degree().init();//lianliankan l=new lianliankan();//l.randomBuild();//l.init();}
}

java连连看(GUI)相关推荐

  1. java连连看(GUI有进度条加背景音乐)

          <连连看>,看起来本来就傻乎乎的游戏,复习周的我,想不到手痒痒,竟然也连续玩了几天.玩的时候才发觉,原来看起来傻傻的游戏,要玩好,还是很困难的.(承认我眼神不好吧),话说平兄啊 ...

  2. 【java毕业设计】基于java+swing+GUI的连连看游戏设计与实现(毕业论文+程序源码)——连连看游戏

    基于java+swing+GUI的连连看游戏设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+swing+GUI的连连看游戏设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦 ...

  3. java 中gui的作用_java学习中最应该注意的Java GUI用户界面以何为基础

    java中的GUI编程(Graphic User Interface,图形用户接口),是在它的笼统窗(Abstract Window Toolkit,AWT)上完成的,java.awt是AWT的东西类 ...

  4. 9.JAVA之GUI编程列出指定目录内容

    代码如下: /*列出指定目录内容*/ import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import ...

  5. java中GUI的awt和Swing的知识点

    刚刚学习了java的GUI,写了几个程序,基本熟悉了awt和Swing,下面和大家分享一下知识点 1.JFrame的层次结构 参考:http://tieba.baidu.com/p/200421612 ...

  6. java事件绑定,Java编程GUI中的事件绑定代码示例

    程序绑定的概念: 绑定指的是一个方法的调用与方法所在的类(方法主体)关联起来.对java来说,绑定分为静态绑定和动态绑定:或者叫做前期绑定和后期绑定 静态绑定: 在程序执行前方法已经被绑定,此时由编译 ...

  7. java获取界面输入数字_通过JAVA设计 GUI 界面的计算器程序,用户可以通过鼠标依次输入参加计算的数值,进行加、减、乘、...

    通过JAVA设计 GUI 界面的计算器程序,用户可以通过鼠标依次输入参加计算的数值,进行加.减.乘. 2016-08-22 0 0 0 4.0 分 其他 1 积分下载 如何获取积分? 通过JAVA设计 ...

  8. 【Java】浅谈Java的GUI开发

    GUI是什么? 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面. 图形用户界面是一种人与计算机通信的界面显示格 ...

  9. java gui容器_[Java教程]GUI Panel 容器以及布局管理器

    [Java教程]GUI Panel 容器以及布局管理器 0 2016-11-09 07:04:32 一.Panel是AWT中的另一个典型的容器,它代表不能独立存在.必须放在其他容器中使用. 1.可作为 ...

最新文章

  1. 老板来了:人脸识别 + 手机推送,老板来了你立刻知道!
  2. 富文本编辑_博客的后台富文本编辑和阅读计数
  3. 34岁,外企倒闭成功上岸大厂,50K,附面试秘籍
  4. 【Java 虚拟机原理】栈帧 | 动态链接 | 方法区 | 字节码文件二进制分析
  5. CSS position绝对定位absolute relative
  6. windows变量延迟_Windows 10的2018年10月更新可能推迟到11月(这就是原因)
  7. Java 9中的进程处理
  8. C#文件夹操作-Directory类
  9. Theano 中文文档 0.9 - 5.1 Ubuntu安装说明
  10. HDU2041 超级楼梯【递推+水题】
  11. 吴锦华/明鑫: 用户态文件系统(FUSE)框架分析和实战
  12. Vue 之 下载本地资源文件
  13. windows定制ISO-可安装
  14. 如何用管理员权限打开CMD(快捷键)
  15. android dbm模式判断,手机信号强弱判断即【dBm/asu】知识普及
  16. 写给XJTU计算机系大一大二的童鞋
  17. 蓝湖访问显示“加载遇到问题”
  18. 桌面扫码点餐系统(小程序+Java后台)
  19. “探月计划”来袭,美国米德天文望远镜助孩子观月赏月
  20. 强制双休!传腾讯光子调整加班机制,21 点前必须离开工位

热门文章

  1. nfs原理及安装配置
  2. 清华学霸花了三年时间对java理解: Java分布式架构
  3. 服务器可视化_系统管理员不可错过的6款服务器监控工具
  4. java环境变量javac不能成功 win7_Java开发:Java环境搭建
  5. HashMap面试深入详解jdk1.8
  6. carbon 一天的开始 结束_情话文案:朋友圈背景图丨2020国庆最后一天朋友圈的说说文案 修炼七天的你,是否准备就绪?...
  7. gdal java shp_【GDAL/OGR】利用GDAL/OGR读取shp文件并转换为json文件(Java版)
  8. 高考计算机算分么,高考分数是怎么算出来的
  9. python进程间通信 listener_python进程间通信之Queue
  10. python中sorted函数逆序_Python中sorted函数的用法(转)