Java-GUI的应用:简易拼图游戏源代码附安装包

基于黑马程序员java相关课程编写

目录

游戏安装包:​​​​​​​链接:https://pan.baidu.com/s/1q_iO6IvEZ2i7Ak3hjM8_uQ 提取码:1234

CodeUtil类:实现返回验证码

gameJFrame:实现游戏界面

loginJFrame:实现登录界面

User类:标准javabean

APP类:


游戏安装包:​​​​​​​链接:https://pan.baidu.com/s/1q_iO6IvEZ2i7Ak3hjM8_uQ 
提取码:1234

CodeUtil类:实现返回验证码

import java.util.Random;

public  class CodeUtil {
    public  static String getCode(){
        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);
            }
        }
        Random r = new Random();

        String code = "";
        for (int i = 0; i < 5; i++) {
            int index = r.nextInt(52);
            code = code+arr[index];
        }

        return code;
    }
}

gameJFrame:实现游戏界面

package com.java.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;

public class gameJFrame extends  JFrame implements KeyListener , ActionListener {
    int [][] arr = new int[4][4];
    int x = 0;
    int y = 0;
    String path = "E:\\java程序\\小项目\\JigsawPuzzle\\image\\animal\\animal3\\";
    int step = 0;
    int win[][] = new int[][]{
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    //创建子菜单
    JMenuItem replayGame = new JMenuItem("重新开始游戏");
    JMenuItem reLoginGame = new JMenuItem("重新登陆");
    JMenuItem closeItem = new JMenuItem("退出游戏");
    JMenuItem girl = new JMenuItem("美女");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem sport = new JMenuItem("运动");
    JMenuItem accountItem = new JMenuItem("作者");
    JMenuItem just = new JMenuItem("本项目基于黑马程序员课程编写");
    //游戏界面:用空参构造初始化界面
    public gameJFrame(){
        //初始化界面
        initJFrame();
        //初始化菜单
        initJMenu();
        //初始化数据
        initData();
        //初始化图片
        initPicture();
        //设置界面可视化
        this.setVisible(true);
    }

    private void initData() {
        Random r = new Random();
        int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        for (int i = 0; i < tempArr.length; i++) {
            int index = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if(tempArr[i*4+j] == 0){
                    x = i;
                    y = j;
                }
                arr[i][j] = tempArr[i*4+j];
            }
        }
        System.out.println(x+" "+y);
    }

    private void initPicture() {
        this.getContentPane().removeAll();
        if(victory()){
            JLabel winjLabel = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\win.png"));
            winjLabel.setBounds(203,283, 197,73);
            this.getContentPane().add(winjLabel);
        }
        JLabel stepCount = new JLabel("步数:"+step);
        stepCount.setBounds(50,30,100,20);
        this.getContentPane().add(stepCount);
        //利用嵌套循环添加图片
        for (int i = 0; i < 4; i++) {
            //外循环代表y轴
            for (int j = 0; j < 4; j++) {
                int num = arr[i][j];
                //里循环代表的x轴
                JLabel jLabel1 = new JLabel(new ImageIcon(path+num+".jpg"));
                jLabel1.setBounds(105*j+83,105*i+134,105,105);
                //给图片添加边框
                jLabel1.setBorder(new BevelBorder(0));
                this.getContentPane().add(jLabel1);
                //每次添加完一张图片num加1
            }
        }
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\background.png"));
        background.setBounds(40,40,508,560);
        this.getContentPane().add(background);
        this.getContentPane().repaint();
    }

    private void initJMenu() {
        //创建菜单
        JMenuBar jMenuBar = new JMenuBar();
        //创建子菜单
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于");
        JMenu removePic = new JMenu("更换图片");
        //将子菜单选项添加至子菜单中
        functionJMenu.add(replayGame);
        functionJMenu.add(reLoginGame);
        functionJMenu.add(closeItem);
        functionJMenu.add(removePic);
        aboutJMenu.add(accountItem);
        aboutJMenu.add(just);
        removePic.add(girl);
        removePic.add(animal);
        removePic.add(sport);
        //给条目绑定事件
        replayGame.addActionListener(this);
        reLoginGame.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        just.addActionListener(this);
        girl.addActionListener(this);
        animal.addActionListener(this);
        sport.addActionListener(this);
        //将子菜单添加至菜单中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);
        //菜单添加至界面中
        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);
        //取消默认居中放置,取消了才会以xy轴坐标形式添加组件
        this.setLayout(null);
        //给界面添加键盘监听事件
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public 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);
            JLabel background = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\background.png"));
            background.setBounds(40,40,508,560);
            this.getContentPane().add(background);
            this.getContentPane().repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        int code = e.getKeyCode();
        if(code == 37){
            System.out.println("向左移动");
            if(y == 3){
                return;
            }
            arr[x][y] = arr[x][y+1];
            arr[x][y+1] = 0;
            y++;
            initPicture();
            step++;
        }
        else if(code == 38){
            System.out.println("向上移动");
            if(x == 3){
                return;
            }
            arr[x][y] = arr[x+1][y];
            arr[x+1][y] = 0;
            x++;
            initPicture();
            step++;

        }
        else if(code == 39){
            System.out.println("向右移动");
            if(y == 0){
                return;
            }
            arr[x][y] = arr[x][y-1];
            arr[x][y-1] = 0;
            y--;
            initPicture();
            step++;
        }
        else if(code == 40){
            System.out.println("向下移动" );
            if(x == 0){
                return;
            }
            arr[x][y] = arr[x-1][y];
            arr[x-1][y] = 0;
            x--;
            initPicture();
            step++;
        }
        else if(code == 65){
            initPicture();
        }
        else if(code == 87){
            arr = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
            };
            initPicture();
        }
    }
    public boolean victory(){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
            if(arr[i][j] != win[i][j]) {
                return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == replayGame){
            System.out.println("重新游戏");
            //清零计数器
            step = 0;
            //再次打乱二维数组的数据
            initData();
            //重新加载图片
            initPicture();


        }
        else if(source == reLoginGame){
            System.out.println("重新登陆");
            //关闭当前界面
            this.setVisible(false);
            //打开登陆界面
            new loginJFrame();
        }
        else if(source == accountItem){
            System.out.println("作者");
            JDialog create = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\微信图片_20230404185509.jpg"));
            jLabel.setBounds(0,0,800,1000);
            create.getContentPane().add(jLabel);
            create.setSize(1000,1100);
            create.setAlwaysOnTop(true);
            create.setLocationRelativeTo(null);
            create.setModal(true);
            create.setVisible(true);
        }
        else if(source == just){
            System.out.println("黑马程序员微信公众号");
            JDialog gongZhong = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\about.png"));
            jLabel.setBounds(0,0,258,258);
            //将窗口添加进弹窗中
            gongZhong.getContentPane().add(jLabel);
            //设置弹窗大小
            gongZhong.setSize(300,300);
            //设置弹窗置顶
            gongZhong.setAlwaysOnTop(true);
            //设置弹窗居中
            gongZhong.setLocationRelativeTo(null);
            //设置弹窗不关闭无法操作其他界面
            gongZhong.setModal(true);
            //设置弹窗可视化
            gongZhong.setVisible(true);
        }
        else if(source == closeItem){
            System.out.println("关闭游戏");
            //关闭游戏
            System.exit(0);
        }
        else if(source == girl){
            Random r = new Random();
            int num = r.nextInt(13)+1;
            path ="E:\\java程序\\小项目\\JigsawPuzzle\\image\\girl\\girl"+num+"\\";
            step = 0;
            initData();
            initPicture();
        }
        else if(source == animal){
            Random r = new Random();
            int num = r.nextInt(8)+1;
            path ="E:\\java程序\\小项目\\JigsawPuzzle\\image\\animal\\animal"+num+"\\";
            step = 0;
            initData();
            initPicture();
        }
        else if(source == sport){
            Random r = new Random();
            int num = r.nextInt(10+1);
            path ="E:\\java程序\\小项目\\JigsawPuzzle\\image\\sport\\sport"+num+"\\";
            step = 0;
            initData();
            initPicture();
        }
    }
}

loginJFrame:实现登录界面

package com.java.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 {
    JButton login = new JButton();
    JButton register = new JButton();
    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    JTextField code = new JTextField();
    JLabel rightCode = new JLabel();
    //创建一个集合存储正确的用户名和密码
    static ArrayList<User> list = new ArrayList<>();

    static {
        list.add(new User("zhangsan","123"));
        list.add(new User("lisi","1234"));
    }

    public loginJFrame(){
        //初始化界面
        initJFrame();
        initView();
    }

    private void initView() {
        this.getContentPane().removeAll();
        //添加用户名图片
        JLabel usernameText = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\用户名.png"));
        usernameText.setBounds(116,135,47,17);
        this.getContentPane().add(usernameText);
        //添加用户名输入框
        username.setBounds(195, 134, 200, 30);
        this.getContentPane().add(username);
        //3.添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\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("E:\\java程序\\小项目\\JigsawPuzzle\\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);
        rightCode.addMouseListener(this);
        //添加到界面
        this.getContentPane().add(rightCode);
        //5.添加登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\登录按钮.png"));
        //去除按钮的默认边框
        login.setBorderPainted(false);
        //去除按钮的默认背景
        login.setContentAreaFilled(false);
        //添加点击事件
        login.addMouseListener(this);
        this.getContentPane().add(login);
        //6.添加注册按钮
        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\注册按钮.png"));
        //去除按钮的默认边框
        register.setBorderPainted(false);
        //去除按钮的默认背景
        register.setContentAreaFilled(false);
        this.getContentPane().add(register);
        //7.添加背景图片
        JLabel background = new JLabel(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
        this.getContentPane().repaint();
    }

    private void initJFrame() {
        //设置界面宽高
        this.setSize(488,430);
        //设置界面标题
        this.setTitle("登陆界面");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式:点右上角红叉虚拟机停止运行
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中放置,取消了才会以xy轴坐标形式添加组件
        this.setLayout(null);
        //设置界面可视化
        this.setVisible(true);
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (source == login) {
            String usernameText = username.getText();
            String passwordText = password.getText();
            String codeText = code.getText();
            if (codeText.equalsIgnoreCase(rightCode.getText()) == false) {
                showJDialog("验证码不正确,请重新输入");
                String code = CodeUtil.getCode();
                rightCode.setText(code);
                return;
            }
            int  userNameCount = 0;
            for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                if (usernameText.equals(user.getName()) == false) {
                    userNameCount++;
                }
                if(userNameCount == 2){
                    showJDialog("用户名不正确!请重新输入");
                    return;
                }
            }
            int userPasswordsCount = 0;
            for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                if (passwordText.equals(user.getPasswords()) == false) {
                    userPasswordsCount++;
                }
                if(userNameCount == 2){
                    showJDialog("密码不正确!");
                    return;
                }
            }
            this.setVisible(false);
            new gameJFrame();
        }
        else if (source == rightCode) {
            String code = CodeUtil.getCode();
            rightCode.setText(code);
        }


    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object source = e.getSource();
        if(source == login){
            login.setIcon(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\登录按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if(source == login){
            login.setIcon(new ImageIcon("E:\\java程序\\小项目\\JigsawPuzzle\\image\\login\\登录按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
    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);
    }
}

User类:标准javabean

package com.java.ui;

public class User {
    private String name;
    private String passwords;

    public User() {
    }

    public User(String name, String passwords) {
        this.name = name;
        this.passwords = passwords;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswords() {
        return passwords;
    }

    public void setPasswords(String passwords) {
        this.passwords = passwords;
    }
}

APP类:


import com.java.ui.CodeUtil;
import com.java.ui.gameJFrame;
import com.java.ui.loginJFrame;
import com.java.ui.registerJFrame;

public class APP {
    public static void main(String[] args) {
       new loginJFrame();
    }
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZEALERHE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值