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 ActionListener {
        JFrame mainFrame; // 主面板
        Container thisContainer;
        JPanel centerPanel, southPanel, northPanel; // 子面板
        JButton diamondsButton[][] = new JButton[6][5];// 游戏按钮数组
        JButton exitButton, resetButton, newlyButton; // 退出,重列,重新开始按钮
        JLabel fractionLable = new JLabel("0"); // 分数标签
        JButton firstButton, secondButton; // 分别记录两次被选中的按钮
        int grid[][] = new int[8][7];// 储存游戏按钮位置
        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.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(6, 5));
                for (int cols = 0; cols < 6; cols++) {
                        for (int rows = 0; rows < 5; rows++) {
                                diamondsButton[cols][rows] = new JButton(
                                                String.valueOf(grid[cols + 1][rows + 1]));
                                diamondsButton[cols][rows].addActionListener(this);
                                centerPanel.add(diamondsButton[cols][rows]);
                        }
                }
                exitButton = new JButton("退出");
                exitButton.addActionListener(this);
                resetButton = 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())));
                northPanel.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() * 6 + 1);
                                rows = (int) (Math.random() * 5 + 1);
                                while (grid[cols][rows] != 0) {
                                        cols = (int) (Math.random() * 6 + 1);
                                        rows = (int) (Math.random() * 5 + 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[30];
                int n = 0, cols, rows;
                int grid[][] = new int[8][7];
                for (int i = 0; i <= 6; i++) {
                        for (int j = 0; j <= 5; 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() * 6 + 1);
                        rows = (int) (Math.random() * 5 + 1);
                        while (grid[cols][rows] != 0) {
                                cols = (int) (Math.random() * 6 + 1);
                                rows = (int) (Math.random() * 5 + 1);
                        }
                        this.grid[cols][rows] = save[n];
                        n--;
                }
                mainFrame.setVisible(false);
                pressInformation = false; // 这里一定要将按钮点击信息归为初始
                init();
                for (int i = 0; i < 6; i++) {
                        for (int j = 0; j < 5; j++) {
                                if (grid[i + 1][j + 1] == 0)
                                        diamondsButton[i][j].setVisible(false);
                        }
                }
        }

public void estimateEven(int placeX, int placeY, JButton bz) {
                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 < 7; 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 < 8; 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() == newlyButton) {
                        int grid[][] = new int[8][7];
                        this.grid = grid;
                        randomBuild();
                        mainFrame.setVisible(false);
                        pressInformation = false;
                        init();
                }
                if (e.getSource() == exitButton)
                        System.exit(0);
                if (e.getSource() == resetButton)
                        reload();
                for (int cols = 0; cols < 6; cols++) {
                        for (int rows = 0; rows < 5; rows++) {
                                if (e.getSource() == diamondsButton[cols][rows])
                                        estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
                        }
                }
        }

public static void main(String[] args) {
                MainGame mg = new MainGame();
                mg.randomBuild();
                mg.init();
        }
}

希望本文所述对大家的Java程序设计有所帮助。

(0)

相关推荐

  • Java Swing中的JButton、JComboBox、JList和JColorChooser组件使用案例

    Swing 的组件与AWT 组件相似,但又为每一个组件增添了新的方法,并提供了更多的高级组件. Swing 的基本组件: 1.按钮(JButton): Swing 中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且还可以将多个图像指定给一个按钮. (1).JButton 常用的构造方法. JButton(String text):按钮上显示字符. JButton(Icon icon) :按钮上显示图标. JButton(String text, Icon icon):按钮上既显示图标

  • Java Swing中的工具栏(JToolBar)和分割面版(JSplitPane)组件使用案例

    一:工具栏(JToolBar) 代码示例: 复制代码 代码如下: import javax.swing.*; //工具栏的使用案例 public class JToolBarDemo2_jigloo extends javax.swing.JFrame { private JToolBar myJToolBar;  private JButton jB_file;  private JButton jB_edit;  private JButton jB_tools;  private JBut

  • Swing常用组件之文本框和文本区

    一.JTextField(文本框)的使用 JTextField是一个轻量级的组件,可以编辑单行文本,实现剪切,复制,粘贴,快捷键等工作,如果文本的长度超出显示范围,会自动滚动文本,JTextField类的构造方法 1.JTextField的常用构造方法:   JTextField() 构造一个新的 TextField.  JTextField(int columns) 构造一个具有指定列数的新的空 TextField.   JTextField(String text) 构造一个用指定文本初始化

  • java swing中实现拖拽功能示例

    java实现拖拽示例 Swing中实现拖拽功能,代码很简单,都有注释,自己看,运行效果如下图: 复制代码 代码如下: package com; import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetAdapter;import java.awt.dn

  • jquery缓动swing liner控制动画过程不同时刻的速度

    jQuery效果函数(slideUp().fadeIn()等)和animation()函数都接收另一个用来控制动画过程的速度的参数,这就是缓动(easing),它确定了动画过程不同时刻的速度.例如,将一个元素移过页面的时候,可能让这个元素缓慢地开始移动,然后变得很快,最后随着动画的完成再次慢下来.给动画添加缓动,使得动画在视觉上更有趣且更有动态感. jQuery只包含了两个缓动方法:swing和linear.linear方法提供了一个稳定的动画,以使得动画的每个步骤都是相同的(例如,如果要让一个

  • java基于swing实现的五子棋游戏代码

    本文实例讲述了java基于swing实现的五子棋游戏代码.分享给大家供大家参考. 主要功能代码如下: 复制代码 代码如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Main extends JFrame implements ActionListener{         private static final long serialVersionUID = 1L;      

  • Java基于swing实现的弹球游戏代码

    本文实例讲述了Java基于swing实现的弹球游戏代码.分享给大家供大家参考. 主要功能代码如下: 复制代码 代码如下: package Game; import java.awt.Graphics; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random;

  • Java Swing中的下拉式菜单(menu)、弹出式菜单(JPopupMenu)、选项卡窗体(JTabbedPane)组件使用案例

    菜单是GUI中最常用的组件,菜单不是Component类的子类,不能放置在普通容器中,不受布局管理器的约束,只能放置在菜单栏中. 菜单组件由菜单栏 (MenuBar).菜单(Menu)和菜单项(MenuItem)三部分组成. 一个菜单栏由若干个菜单组成,一个菜单又由若干个菜单项组成.一般菜单栏放 Frame 窗口中,只要调用 Frame 类的 setMenuBar()方法即可. 常用的菜单有:下拉式菜单和弹出式菜单(独立显示,可出现在任意地方). 一:下拉式菜单的创建步骤: 1.创建一个菜单栏.

  • Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例

    一:JTextField的(文本框)使用: JTextField 是一个轻量级组件,它允许编辑单行文本. 1.JTextField的常用构造方法: JTextField() 构造一个新的 TextField. JTextField(int columns) 构造一个具有指定列数的新的空 TextField. JTextField(String text) 构造一个用指定文本初始化的新TextField. JTextField(String text, int columns) 构造一个用指定文本

  • Java Swing中的表格(JTable)和树(JTree)组件使用实例

    一:表格(JTable): 1.基本概念: 表格(JTable)是Swing 新增加的组件,主要是为了将数据以表格的形式显示.给显示大块数据提供了简单的机制. 2.常用构造方法: * JTable():使用系统默认的模型创建一个JTable 实例.  * JTable(int numRows,int numColumns):创建一个使用DefaultTableModel 指定行.列的空表格.  * JTable(Object[ ][ ] rowData,Object[ ][ ] columnNa

随机推荐