基于Swing的连连看小游戏

 

class LianLianKanJPanel extends JPanel implements ActionListener,ItemListener {

	private static final long serialVersionUID = 1L;//序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
		private int[][] map = new int[8][8];//8*8的正方形
		private int kind, randomx, randomy, randomx1, randomy1; // 种类,随机x
		private int coordinatex, coordinatey, coordinatex1, coordinatey1; // 坐标X
		private Point lineStart = new Point(0, 0);
		private int clicktimes;
		private int jishushengyu;//计数剩余
		private int Kinds = 4;
		private int score;
		private int guanshu;//关数

		
		loudou ld = new loudou();// 漏斗
		

		JButton BlockButton[][] = new JButton[8][8];//
		Choice difficultChoice = new Choice();
		JButton newgameButton = new JButton("重新开始");
		JButton reLoad = new JButton("刷新");

		ImageIcon ii = new ImageIcon("src/im/bk.jpg");

		ImageIcon aIcon = new ImageIcon("src/im/1.gif");
		ImageIcon bIcon = new ImageIcon("src/im/2.gif");
		ImageIcon cIcon = new ImageIcon("src/im/3.gif");
		ImageIcon dIcon = new ImageIcon("src/im/4.gif");
		ImageIcon eIcon = new ImageIcon("src/im/5.gif");
		ImageIcon fIcon = new ImageIcon("src/im/6.gif");
		ImageIcon gIcon = new ImageIcon("src/im/7.gif");
		ImageIcon hIcon = new ImageIcon("src/im/8.gif");
		ImageIcon iIcon = new ImageIcon("src/im/9.gif");
		ImageIcon jIcon = new ImageIcon("src/im/10.gif");
		ImageIcon kIcon = new ImageIcon("src/im/11.gif");
		ImageIcon lIcon = new ImageIcon("src/im/12.gif");
		ImageIcon mIcon = new ImageIcon("src/im/13.gif");
		ImageIcon nIcon = new ImageIcon("src/im/14.gif");
		ImageIcon oIcon = new ImageIcon("src/im/15.gif");

		public LianLianKanJPanel() {

			this.setLayout(null);

			newMap();
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					BlockButton[i][j] = new JButton();
					add(BlockButton[i][j]);
					BlockButton[i][j].addActionListener(this);//监听器
					BlockButton[i][j].setBounds(30 + j * 40, 30 + i * 40, 31,34);
				//	BlockButton[i][j].setBorderPainted(false);
				//  BlockButton[i][j].setVisible(true);
				}
			}
			difficultChoice.add("简单");
			difficultChoice.add("中等");
			difficultChoice.add("困难");
			difficultChoice.add("变态");

			newgameButton.setBounds(map[0].length * 40 + 80, 40, 100, 20);
			newgameButton.setBackground(Color.white);
			newgameButton.setBorderPainted(false); //去边框
			reLoad.setBounds(map[0].length * 40 + 100, 80, 60, 20);
			reLoad.setBackground(Color.white);
			reLoad.setBorderPainted(false);
			difficultChoice.setBounds(map[0].length * 40 + 100, 120, 60, 20);
			difficultChoice.addItemListener(this);
			newgameButton.addActionListener(this);
			reLoad.addActionListener(this);

			this.add(newgameButton);
			this.add(reLoad);
			this.add(difficultChoice);

			// /-------------------------漏斗
			ld.setBounds(map[0].length * 40 + 100, 200, 70, 150);// 漏斗
			ld.setBackground(Color.black);
			this.add(ld);
	

		}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 Java Swing 实现字母连连看小游戏的代码示例: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class LetterLink extends JFrame implements ActionListener { private JButton[][] board; private JButton selected; private int numRows, numCols; private char[][] letters; private Random random; public LetterLink(int rows, int cols) { super("Letter Link"); numRows = rows; numCols = cols; letters = new char[numRows][numCols]; board = new JButton[numRows][numCols]; random = new Random(); // 初始化字母矩阵 for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { letters[i][j] = (char) ('A' + random.nextInt(26)); } } // 创建游戏面板 JPanel gamePanel = new JPanel(new GridLayout(numRows, numCols)); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { board[i][j] = new JButton("" + letters[i][j]); board[i][j].addActionListener(this); gamePanel.add(board[i][j]); } } // 添加游戏面板到窗口 add(gamePanel); // 设置窗口参数 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(numCols * 50, numRows * 50); setLocationRelativeTo(null); setVisible(true); } // 处理按钮点击事件 public void actionPerformed(ActionEvent e) { JButton btn = (JButton) e.getSource(); if (selected == null) { selected = btn; selected.setEnabled(false); } else if (btn == selected) { selected.setEnabled(true); selected = null; } else if (btn.getText().equals(selected.getText())) { btn.setEnabled(false); selected.setEnabled(false); btn.setText(""); selected.setText(""); selected = null; } else { selected.setEnabled(true); selected = btn; selected.setEnabled(false); } } public static void main(String[] args) { new LetterLink(6, 6); } } ``` 这个程序创建了一个大小为 6x6 的字母连连游戏面板。每个按钮对应矩阵中的一个字母,当两个相同的字母被点击时,它们会消失。 你可以根据需要修改行数和列数,或者自定义字母矩阵的生成方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值