注释:
基于IDEA,创建窗体进行游戏
默认的用户名和密码为:zhangsan,123
lisi,1234
App界面
package marchwho.ui;public class App {public static void main(String[] args) {//登录的窗体new LogInJFrame();//注册的窗体// new RegisterJFrame();//游戏主界面的窗体// new GameJFrame();}
}
游戏主界面
package marchwho.ui;import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;public class GameJFrame extends JFrame implements KeyListener, ActionListener {//游戏主界面//创建胜利的二位数组int[][] win = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//创建一个二维数组//用于打乱图片int[][] data = new int[4][4];//用于空白图片的移动int x = 0;int y = 0;//加载图片的路径String path = "../puzzleagame/image/girl/girl1/";//统计步数int step = 0;//创建选项下面的条目JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem codeItem = new JMenuItem("作者");//创建更换图片下的条目JMenuItem GirlItem = new JMenuItem("美女");JMenuItem AnimalItem = new JMenuItem("动物");JMenuItem SportItem = new JMenuItem("运动");//创建帮助下面的条目JMenuItem AItem = new JMenuItem("按A查看完整图片");public GameJFrame() {//初始化界面initJFrame();//初始化菜单initJMenuBar();//打乱图片initData();//初始化图片initImage();//让界面可以显示出来this.setVisible(true);}private void initData() {int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};Random r = new Random();//将一维数组打乱for (int i = 0; i < arr.length; i++) {int index = r.nextInt(arr.length);int temp = arr[i];arr[i] = arr[index];arr[index] = temp;}//将打乱的一维数组依次放入二维数组中for (int i = 0; i < arr.length; i++) {//获得空白图片所对应的位置if (arr[i] == 0) {x = i / 4;y = i % 4;}data[i / 4][i % 4] = arr[i];}}private void initImage() {//清空已经出现的所有图片this.getContentPane().removeAll();//添加胜利的图标if (victory()) {ImageIcon winicon = new ImageIcon("../puzzleagame/image/win.png");JLabel winJLabel = new JLabel(winicon);winJLabel.setBounds(203, 283, 197, 73);this.getContentPane().add(winJLabel);}JLabel Step = new JLabel("步数" + step);Step.setBounds(50, 30, 100, 20);this.getContentPane().add(Step);for (int j = 0; j < 4; j++) {//外循环---将内循环执行四次,即添加四行图片for (int i = 0; i < 4; i++) {//内循环---将一行的图片进行添加//将打乱的二维数组所对应的数字将图片进行打乱int number = data[j][i];//创建一个图片ImageIcon对象ImageIcon icon = new ImageIcon(path + number + ".jpg");//创建一个JLabel对象(管理容器)JLabel jLabel = new JLabel(icon);//指定图片位置jLabel.setBounds(105 * i + 83, 105 * j + 134, 105, 105);//添加图片边框//0---凸出来//1---凹出来jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//获取隐藏窗口,通过XY轴设置图片的位置与图片的大小this.getContentPane().add(jLabel);}}//添加背景图片ImageIcon background = new ImageIcon("../puzzleagame/image/background.png");JLabel BackGround = new JLabel(background);BackGround.setBounds(40, 40, 508, 560);this.getContentPane().add(BackGround);//刷新界面this.getContentPane().repaint();}private void initJMenuBar() {//创建菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单里面的选项对象(功能 关于我们)JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");JMenu helpJMenu = new JMenu("帮助");JMenu changeJMenu = new JMenu("更换图片");//将条目添加到选项中functionJMenu.add(changeJMenu);changeJMenu.add(GirlItem);changeJMenu.add(AnimalItem);changeJMenu.add(SportItem);helpJMenu.add(AItem);functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);aboutJMenu.add(codeItem);//给条目绑定事件replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);codeItem.addActionListener(this);GirlItem.addActionListener(this);AnimalItem.addActionListener(this);SportItem.addActionListener(this);//将选项添加到菜单中jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);jMenuBar.add(helpJMenu);//给整个界面设置菜单this.setJMenuBar(jMenuBar);}private void initJFrame() {//设置界面的宽高this.setSize(603, 680);//设置界面的标题this.setTitle("拼图游戏 V1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中,处于屏幕中间this.setLocationRelativeTo(null);//设置关闭模式//关闭之后虚拟机也会停止运行this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//创建键盘监听this.addKeyListener(this);//取消内部默认居中格式this.setLayout(null);}@Overridepublic void keyTyped(KeyEvent e) {}//按住不松@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if (code == 65) {//将界面中的所有图片删除this.getContentPane().removeAll();//加载完整的图片JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));all.setBounds(83, 134, 420, 420);this.getContentPane().add(all);//添加背景图片ImageIcon background = new ImageIcon("../puzzleagame/image/background.png");JLabel BackGround = new JLabel(background);BackGround.setBounds(40, 40, 508, 560);this.getContentPane().add(BackGround);//刷新界面this.getContentPane().repaint();}}@Overridepublic void keyReleased(KeyEvent e) {//判断如果胜利则将不能移动if (victory()) {return;}//上:38 下:40 左:37 右:39int code = e.getKeyCode();//上移if (code == 38) {if (x == 0) {return;}System.out.println("向左移动");data[x][y] = data[x - 1][y];data[x - 1][y] = 0;x--;initImage();step++;}//下移else if (code == 40) {if (x == 3) {return;}System.out.println("向右移动");data[x][y] = data[x + 1][y];data[x + 1][y] = 0;x++;initImage();step++;}//左移else if (code == 37) {if (y == 0) {return;}System.out.println("向上移动");data[x][y] = data[x][y - 1];data[x][y - 1] = 0;y--;initImage();step++;}//右移else if (code == 39) {if (y == 3) {return;}System.out.println("向下移动");data[x][y] = data[x][y + 1];data[x][y + 1] = 0;y++;initImage();step++;} else if (code == 65) {initImage();} else if (code == 81) {data = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};initImage();}}public boolean victory() {for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[i].length; j++) {if (data[i][j] != win[i][j]) {//只要有一个不一样,就返回falsereturn false;}}}return true;}@Overridepublic void actionPerformed(ActionEvent e) {Object source = e.getSource();if (source == replayItem) {System.out.println("重新游戏");//步数清零step = 0;//重新打乱二维数组initData();//重新加载图片initImage();} else if (source == reLoginItem) {System.out.println("重新登录");//关闭游戏界面this.setVisible(false);//返回登录new LogInJFrame();} else if (source == closeItem) {System.out.println("关闭游戏");//关闭虚拟机System.exit(0);} else if (source == codeItem) {System.out.println("作者");//创建弹框对象JDialog jDialog = new JDialog();//创建一个管理容器的对象ImageIcon icon = new ImageIcon();JLabel jLabel = new JLabel(icon);jLabel.setBounds(0, 0, 258, 258);//将图片放到弹框中jDialog.getContentPane().add(jLabel);//设置弹框大小jDialog.setSize(344, 344);//设置弹框置顶jDialog.setAlwaysOnTop(true);//设置弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭无法再进行操作jDialog.setModal(true);//显示弹框jDialog.setVisible(true);} else if (source == GirlItem) {System.out.println("美女");//获得随机的索引代表不同的美女图片Random r =new Random();int index = r.nextInt(13);//加载图片的路径path = "../puzzleagame/image/girl/girl"+index+"/";//步数清零step = 0;//打乱图片initImage();}else if (source == AnimalItem) {System.out.println("动物");//获得随机的索引代表不同的动物图片Random r =new Random();int index = r.nextInt(8);//加载图片的路径path = "../puzzleagame/image/animal/animal"+index+"/";//步数清零step = 0;//打乱图片initImage();}else if (source == SportItem) {System.out.println("运动");//获得随机的索引代表不同的运动图片Random r =new Random();int index = r.nextInt(10);//加载图片的路径path = "../puzzleagame/image/sport/sport"+index+"/";//步数清零step = 0;//打乱图片initImage();}}
}
登录界面
package marchwho.ui;import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;public class LogInJFrame extends JFrame implements MouseListener {//登录界面//创建一个集合存储正确的用户名和密码static ArrayList<User> list = new ArrayList<>();static {list.add(new User("zhangsan", "123"));list.add(new User("lisi", "1234"));}//2.添加用户名输入框JTextField username = new JTextField();//4.密码输入框JPasswordField password = new JPasswordField();//验证码的输入框JTextField code = new JTextField();//5.添加登录按钮JButton login = new JButton();//6.添加注册按钮JButton register = new JButton();//生成的验证码JLabel rightCode = new JLabel();public LogInJFrame() {//初始化界面initJFrame();//在这个界面中添加内容initView();//让当前界面显示出来this.setVisible(true);}public void initView() {//1. 添加用户名文字JLabel usernameText = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\用户名.png"));usernameText.setBounds(116, 135, 47, 17);this.getContentPane().add(usernameText);//2.添加用户名输入框username.setBounds(195, 134, 200, 30);this.getContentPane().add(username);//3.添加密码文字JLabel passwordText = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\密码.png"));passwordText.setBounds(130, 195, 32, 16);this.getContentPane().add(passwordText);//4.密码输入框password.setBounds(195, 195, 200, 30);this.getContentPane().add(password);//验证码提示JLabel codeText = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\验证码.png"));codeText.setBounds(133, 256, 50, 30);this.getContentPane().add(codeText);//验证码的输入框code.setBounds(195, 256, 100, 30);this.getContentPane().add(code);String codeStr = CodeUtil.getCode();//设置内容rightCode.setText(codeStr);//位置和宽高rightCode.setBounds(300, 256, 50, 30);//添加到界面this.getContentPane().add(rightCode);//5.添加登录按钮login.setBounds(123, 310, 128, 47);login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按钮.png"));//去除按钮的默认边框login.setBorderPainted(false);//去除按钮的默认背景login.setContentAreaFilled(false);this.getContentPane().add(login);//6.添加注册按钮register.setBounds(256, 310, 128, 47);register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按钮.png"));//去除按钮的默认边框register.setBorderPainted(false);//去除按钮的默认背景register.setContentAreaFilled(false);this.getContentPane().add(register);//7.添加背景图片JLabel background = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\background.png"));background.setBounds(0, 0, 470, 390);this.getContentPane().add(background);//设置注册、登录、验证码的鼠标监听register.addMouseListener(this);login.addMouseListener(this);rightCode.addMouseListener(this);}public void initJFrame() {//设置界面的宽高this.setSize(488, 430);//设置界面的标题this.setTitle("拼图游戏 登录");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中,处于屏幕中间this.setLocationRelativeTo(null);//设置关闭模式//关闭之后虚拟机也会停止运行this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//让界面可以显示出来this.setVisible(true);}//创建弹窗显示//要展示用户名或密码错误public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}//点击@Overridepublic void mouseClicked(MouseEvent e) {Object source = e.getSource();if (source == login) {System.out.println("点击登录按钮");//先获取用户输入的用户名、密码、验证码String usernameText = username.getText();String passwordText = password.getText();String codeText = code.getText();//先判断输入是否为空if (usernameText.length() == 0 || passwordText.length() == 0) {showJDialog("用户名或密码不能为空");} else if (codeText.length() == 0) {showJDialog("验证码不能为空");}//不为空,开始判断else {if (compare(list, usernameText, passwordText)) {if (codeText.equals(rightCode.getText())) {showJDialog("登录成功");//关闭登录界面this.setVisible(false);//打开游戏主界面new GameJFrame();} else {showJDialog("验证码输入错误");}} else {showJDialog("用户名或密码输入错误");}}} else if (source == register) {System.out.println("点击注册按钮");} else if (source == rightCode) {System.out.println("点击验证码,重新获取验证码");String codeStr = CodeUtil.getCode();rightCode.setText(codeStr);}}//判断用户名是否存在public static boolean compare(ArrayList<User> list, String usernameText, String passwordText) {for (int i = 0; i < list.size(); i++) {User user = list.get(i);if (user.getusername().equals(usernameText) && user.getpassword().equals(passwordText)) {return true;}}return false;}//按下不松@Overridepublic void mousePressed(MouseEvent e) {Object source = e.getSource();if (source == register) {//设置点击注册之后的颜色加深//相当于换一个图片register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按下.png"));} else if (source == login) {//设置点击登录之后的颜色加深//相当于换一个图片login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按下.png"));}}//松开@Overridepublic void mouseReleased(MouseEvent e) {Object source = e.getSource();if (source == register) {//变回原来的图片register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按钮.png"));} else if (source == login) {//变回原来的图片login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按钮.png"));}}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}}
登录界面所需要的User类
package marchwho.ui;public class User {private String username;private String password;public User() {}public User(String username, String password) {this.username = username;this.password = password;}public String getusername() {return username;}public void setusername(String username) {this.username = username;}public String getpassword() {return password;}public void setpassword(String password) {this.password = password;}
}
生成验证码的工具类
package marchwho.ui;import java.util.Random;public class CodeUtil {//工具类//生成验证码public CodeUtil() {}//生成一个随机的验证码//四个字母+一个数字//数字位置随机//将生成的字符数组内容放到一个字符串里面public static String getCode() {String result = changeLetter();return result;}public static String changeLetter() {char[] code = getArr();String getcode = "";for (int i = 0; i < code.length; i++) {getcode = getcode + code[i];}return getcode;}public static char[] getArr() {//创建一个字符串数组,长度为5char[] arr = new char[5];Random r = new Random();//获得随机的一个数字int number = r.nextInt(9);//获得随机的一个字母数组char[] letter = getLetter();String result = "";//先将数字放在最后一位for (int i = 0; i < arr.length - 1; i++) {int letterNumber = r.nextInt(52);result = result + letter[letterNumber];}result = result + number;//将数字索引进行打乱int numberIndex = r.nextInt(arr.length - 1);//将结果变成一个字符串数组for (int i = 0; i < result.length(); i++) {char c = result.charAt(i);arr[i] = c;}//将最后一个索引的内容与前面交换char temp = arr[arr.length - 1];arr[arr.length - 1] = arr[numberIndex];arr[numberIndex] = temp;return arr;}//获得随机的一个字母数组public static char[] getLetter() {char[] arr = new char[52];for (int i = 0; i < arr.length; i++) {if (i <= 25) {arr[i] = (char) (97 + i);} else {arr[i] = (char) (65 + i - 26);}}return arr;}
}