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

/

相关推荐
NE_STOP11 分钟前
SpringBoot--简单入门
java·spring
hqxstudying38 分钟前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
Dcs1 小时前
VSCode等多款主流 IDE 爆出安全漏洞!插件“伪装认证”可执行恶意命令!
java
保持学习ing1 小时前
day1--项目搭建and内容管理模块
java·数据库·后端·docker·虚拟机
京东云开发者1 小时前
Java的SPI机制详解
java
超级小忍2 小时前
服务端向客户端主动推送数据的几种方法(Spring Boot 环境)
java·spring boot·后端
程序无bug2 小时前
Spring IoC注解式开发无敌详细(细节丰富)
java·后端
小莫分享2 小时前
Java Lombok 入门
java
程序无bug2 小时前
Spring 对于事务上的应用的详细说明
java·后端
食亨技术团队2 小时前
被忽略的 SAAS 生命线:操作日志有多重要
java·后端