目录
这是一个用java编写的小游戏,连连看是一种消除类益智游戏,核心要求是在规定的时间内,消除游戏界面中选中的两张相同的图案,直至完全消除所有图案。这款游戏操作简单(只需单击鼠标左键操作)、面向人群广泛,在限时操作的游戏要求下,吸引玩家自发地锻炼观察能力、判断能力和反应能力,故从推出至今颇受欢迎。
提示:以下是本篇文章正文内容,下面案例可供参考
运行界面图
代码如下(示例)
package ui; import java.awt.Choice; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Llk_2 extends JFrame { private static final long serialVersionUID = 1L; public Llk_2() { LianLianKanJPanel llk = new LianLianKanJPanel(); add(llk); } 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("C:\\Users\\zjjt\\Desktop\\sb/bjt2.jpeg"); ImageIcon aIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/1.jpg"); ImageIcon bIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/2.jpg"); ImageIcon cIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/3.gif"); ImageIcon dIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/4.gif"); ImageIcon eIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/5.jpg"); ImageIcon fIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/6.gif"); ImageIcon gIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/7.jpeg"); ImageIcon hIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/8.jpeg"); ImageIcon iIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/9.jpeg"); ImageIcon jIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/10.jpeg"); ImageIcon kIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/13.jpg"); ImageIcon lIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/11.jpeg"); ImageIcon mIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/12.jpeg"); ImageIcon nIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/5.jpg"); ImageIcon oIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/6.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); } class loudou extends J