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播客!
java·后端·面试
唐青枫21 分钟前
Java Gradle 实战指南:从 Wrapper、Kotlin DSL 到 Spring Boot 多模块构建
java·kotlin·gradle
wuqingshun31415923 分钟前
如何理解Spring Boot中的starter?
java·spring boot·后端
带刺的坐椅23 分钟前
Solon TeamAgent 协作协议:从 SEQUENTIAL 流水线到 HIERARCHICAL 主管团队
java·ai·llm·agent·solon
wuqingshun31415932 分钟前
SpringBoot是如何实现自动配置的
java·spring boot·后端
用户0537040314335 分钟前
我的学习记录泛型
java
zhouhui00137 分钟前
AI帮我写了个Spring Boot校验,线上漏掉了这组边界条件
java·spring boot·redis·ai编程
JRedisX44 分钟前
Java从零手写企业级内存数据库
java
萧瑟余晖1 小时前
JDK 20 新特性详解
java·开发语言
hdsoft_huge1 小时前
SpringBoot系列17:日志体系Logback配置,日志分割、脱敏、异常堆栈收集规范(生产级落地)
java·spring boot·logback