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=arr[i];

arr[i]=arr[index];

arr[index]=temp;

}

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

return new String(arr);

}

//注册界面初始化方法

public void initJFrame(){

/

相关推荐
重生之我要进大厂17 分钟前
LeetCode 876
java·开发语言·数据结构·算法·leetcode
_祝你今天愉快20 分钟前
技术成神之路:设计模式(十四)享元模式
java·设计模式
小筱在线1 小时前
SpringCloud微服务实现服务熔断的实践指南
java·spring cloud·微服务
luoluoal1 小时前
java项目之基于Spring Boot智能无人仓库管理源码(springboot+vue)
java·vue.js·spring boot
ChinaRainbowSea1 小时前
十三,Spring Boot 中注入 Servlet,Filter,Listener
java·spring boot·spring·servlet·web
小游鱼KF1 小时前
Spring学习前置知识
java·学习·spring
扎克begod1 小时前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python
青灯文案11 小时前
SpringBoot 项目统一 API 响应结果封装示例
java·spring boot·后端
我就是程序猿1 小时前
tomcat的配置
java·tomcat
阳光阿盖尔2 小时前
EasyExcel的基本使用——Java导入Excel数据
java·开发语言·excel