Java拼图小游戏

文章目录

main方法

登录界面

游戏主界面

User类

图片素材获取方式

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

main方法

import ui.LoginJframe;

public class App {

public static void main(String[] args) {

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

}

}

登录界面

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(){

相关推荐
唐宋元明清218816 小时前
.NET 磁盘管理-技术方案选型
windows·c#·存储
毕设源码-郭学长17 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
梨落秋霜17 小时前
Python入门篇【文件处理】
android·java·python
Java 码农17 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
桌面运维家17 小时前
vDisk VOI/IDV Windows本地运行异常怎么办?快速排查恢复
windows
哈库纳玛塔塔17 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
S***q37718 小时前
Spring Boot管理用户数据
java·spring boot·后端
天“码”行空18 小时前
java面向对象的三大特性之一多态
java·开发语言·jvm
毕设源码-郭学长18 小时前
【开题答辩全过程】以 基于SpringBoot框架的民俗文化交流与交易平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
张登杰踩18 小时前
VIA标注格式转Labelme标注格式
python