Java拼图小游戏

文章目录

main方法

登录界面

游戏主界面

User类

图片素材获取方式

基于Java实现的拼图小游戏,代码与图片素材来自b站up主黑马程序员的Java教学视频。

main方法

import ui.LoginJframe;

public class App {

public static void main(String\[\] args) {

new LoginJframe(); //创建登陆界面

}

}

1

2

3

4

5

6

7

8

登录界面

package ui;

import javax.swing.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.ArrayList;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

//游戏登录界面

public class LoginJframe extends JFrame implements MouseListener {

//创建全集一个用户集合用于登录判断

static ArrayList<User> list=new ArrayList<>();

static String userName;

//添加用户集合成员,用于登录操作判断

static{

list.add(new User("wuhu","yahu"));

}

//切换验证码按键

JButton Switch_Captcha=new JButton();

//设置注册按钮

JButton enrollJButton=new JButton();

//设置登录按钮

JButton loginJButton = new JButton();

//getCaptcha为验证码获取方法,传递的数据为需要返回的验证码的长度

String str_Captcha=getCaptcha(5);

/*登录与注册按钮的图片文件位置*/

String Image_enroll="untitled/image/login/注册按钮.png";

String Image_login="untitled/image/login/登录按钮.png";

//显示用户名输入框

JTextField NameFrame=new JTextField();

//添加验证码文本框

JTextField FrameCaptcha=new JTextField();

//密码输入显示框

JTextField PasswordFrame=new JTextField();

//创建一个弹框对象

JDialog Error;

//登录界面的空参构造

public LoginJframe(){

initJFrame(); //界面初始化项目

initView(); //界面元素添加

//绑定监听

enrollJButton.addMouseListener(this);

loginJButton.addMouseListener(this);

Switch_Captcha.addMouseListener(this);

//将界面状态设置为显示

this.setVisible(true);

}

//界面初始化类

public void initView(){

//清空界面

this.getContentPane().removeAll();

//显示用户名

JLabel UsrtName = new JLabel(new ImageIcon("untitled/image/login/用户名.png"));

UsrtName.setBounds(100,105,47,17);

this.getContentPane().add(UsrtName);

//配置用户名输入框

NameFrame.setBounds(160,100,200,30);

this.getContentPane().add(NameFrame);

//显示密码

JLabel UsrtPassword=new JLabel(new ImageIcon("untitled/image/login/密码.png"));

UsrtPassword.setBounds(100,155,47,17);

this.getContentPane().add(UsrtPassword);

//配置密码框参数

PasswordFrame.setBounds(160,150,200,30);

this.getContentPane().add(PasswordFrame);

//添加验证码

JLabel CaptchaTxT=new JLabel(new ImageIcon("untitled/image/login/验证码.png"));

CaptchaTxT.setBounds(100,215,47,17);

this.getContentPane().add(CaptchaTxT);

//配置验证码文本框参数

FrameCaptcha.setBounds(160,210,100,30);

this.getContentPane().add(FrameCaptcha);

//验证码显示

System.out.println(str_Captcha);

JLabel Captcha=new JLabel(str_Captcha);

Captcha.setBounds(280,215,50,20);

this.getContentPane().add(Captcha);

//设置验证码按钮

Switch_Captcha.setBounds(275,215,50,20);

Switch_Captcha.setBorderPainted(false);

Switch_Captcha.setContentAreaFilled(false);

this.getContentPane().add(Switch_Captcha);

//配置登录按钮

loginJButton.setIcon(new ImageIcon(Image_login));

loginJButton.setBorderPainted(false); //去除按钮的默认边框

loginJButton.setContentAreaFilled(false); //去除按钮的默认背景

loginJButton.setBounds(123,310,128,47); //设置位置与大小

this.getContentPane().add(loginJButton);

//配置注册按钮

enrollJButton.setIcon(new ImageIcon(Image_enroll));

enrollJButton.setBorderPainted(false); //去除按钮的默认边框

enrollJButton.setContentAreaFilled(false); //去除按钮的默认背景

enrollJButton.setBounds(270,310,128,47); //设置位置与大小

this.getContentPane().add(enrollJButton);

// 添加背景图片

//设置一个JLabel对象进行管理图片

JLabel background_JLabel=new JLabel(new ImageIcon("untitled/image/register/background.png"));

//设置图片的位置与宽高

background_JLabel.setBounds(0,0,470,390);

//将容器添加到界面当中

this.getContentPane().add(background_JLabel);

//刷新界面

this.getContentPane().repaint();

}

//获取验证码方法 n 为需要返回的验证码长度

public static String getCaptcha(int len){

Random r=new Random(); //设置随机方法种子

StringBuilder str=new StringBuilder();

for (int i = 0; i < len-1; i++) {

int num=r.nextInt(58)+65; //字母范围的数值

//跳过ascii表中的其他字符

if(num > 90 && num < 97){

num+=7;

}

str.append((char)num); //将数值强制转换为字母并添加到字符串中

}

//将数字添加到字符串中

int number=r.nextInt(10);

str.append(number);

//转换为字符数组更好进行乱序的操作

char \[\]arr= str.toString().toCharArray();

//将其数组元素顺序打乱

for (int i = 0; i < arr.length; i++) {

int index=r.nextInt(arr.length);

char temp=arri;

arri=arrindex;

arrindex=temp;

}

//将完成的验证码转换为字符串将其返回

return new String(arr);

}

//注册界面初始化方法

public void initJFrame(){

/

相关推荐
AI人工智能+电脑小能手4 小时前
【大白话说Java面试题 第87题】【Mysql篇】第17题:分布式事务的实现原理?
java·数据库·分布式·mysql·面试
来杯@Java5 小时前
图书管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·mybatis·课程设计
卷毛的技术笔记6 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥6 小时前
匿名函数 lambda + 高阶函数
java·python·算法
東雪木6 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
adrninistrat0r6 小时前
Java调用链MCP分析工具
java·python·ai编程
噜噜噜阿鲁~6 小时前
python学习笔记 | 11.3、面向对象高级编程-多重继承
java·开发语言
春生野草7 小时前
反射、Tomcat执行
java·开发语言
_日拱一卒7 小时前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
飞翔中文网8 小时前
Java学习笔记之抽象类与接口(设计思想)
java·笔记·学习