龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

java基于swing实现的连连看代码

时间:2014-11-14 11:08来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了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);
        }

精彩图集

赞助商链接