Java实现简单的登录,注册拼图游戏

您所在的位置:网站首页 用java编写一个账号和密码登录 Java实现简单的登录,注册拼图游戏

Java实现简单的登录,注册拼图游戏

2024-07-10 11:01| 来源: 网络整理| 查看: 265

文章目录 一、界面预览二、 创建环境二、代码实现1.主类2.登录页面3.登录页面逻辑实现4.用户信息6.游戏界面7.注册界面8. 作弊码及还原图片功能 三、总结

一、界面预览

1.登录页面

点击验证码可更换验证码,点击查看密码可查看输入的密码

在这里插入图片描述 2.注册界面

点击注册即可以新用户身份进入游戏 注: 初始用户信息为 用户名:hangman 密码:123456 点击重置即可重新输入密码账号重新注册

在这里插入图片描述

3.游戏界面

按下键盘上下左右即可进行移动操作并计算移动的步数.

在这里插入图片描述 功能预览:

点击更换图片即可变换图片进行游戏,重新登录回到登录页面,重新开始游戏步数清零打乱图片顺序继续游戏,点击关闭即可退出游戏

在这里插入图片描述

4.游戏胜利

游戏结束点击重新开始游戏即可继续游戏

在这里插入图片描述

二、 创建环境 文件结构

test文件夹为本人测试使用可不创建 2.资源图片结构 在这里插入图片描述

二、代码实现 1.主类

此类主要用来启动游戏

代码如下:

import com.PinTu.UI.Login; public class Main { //作为界面的启动清单 public static void main(String[] args) { new Login(); } } 2.登录页面

此类用来用户登录游戏并实现了新用户注册功能

1.主页面

public void initFrame(){ this.setSize(488, 430);//设置宽高 this.setTitle("登录账号");//设置标题 this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);//设置关闭模式 this.setLocationRelativeTo(null);//居中 this.setAlwaysOnTop(true);//置顶 this.setLayout(null);//取消内部默认布局 }

2.定义组件

JTextField UserjTextField = new JTextField();//用户名输入框 JPasswordField passwordField = new JPasswordField();//用户密码输入框 JTextField code = new JTextField();//验证码输入框 JButton loginButton = new JButton();//登录按钮 JButton enrollButton = new JButton();//注册按钮 JLabel seelabel = new JLabel(); //验证码区域 JLabel rightCode = new JLabel();

3.页面内容

public void initView() { //添加用户名标签 JLabel UserjLabel = new JLabel(new ImageIcon("......\\PinTu\\image\\login\\用户名.png")); UserjLabel.setBounds(100, 140, 47, 17); this.getContentPane().add(UserjLabel); //添加输入框 UserjTextField.setBounds(195, 134, 200, 30); this.getContentPane().add(UserjTextField); //添加密码标签 JLabel PawjLabel = new JLabel(new ImageIcon("E:\\JavaProject\\PinTu\\image\\login\\密码.png")); PawjLabel.setBounds(110, 200, 32, 16); this.getContentPane().add(PawjLabel); //添加密码输入框 passwordField.setBounds(195, 195, 200, 30); passwordField.setEchoChar('*'); this.getContentPane().add(passwordField); //密码显示 seelabel.setIcon(new ImageIcon("......\\PinTu\\image\\login\\显示密码.png")); seelabel.setBounds(400,195,50,30); seelabel.addMouseListener(this); this.getContentPane().add(seelabel); //验证码提示 JLabel codeText = new JLabel(new ImageIcon("......\\PinTu\\image\\login\\验证码.png")); codeText.setBounds(100, 256, 50, 30); this.getContentPane().add(codeText); //验证码的输入框 code.setBounds(195, 256, 100, 30); this.getContentPane().add(code); //生成5个随机验证码 String codeStr = CodeUtil.Create(5); //设置内容 rightCode.setText(codeStr); //绑定鼠标事件 rightCode.addMouseListener(this); //位置和宽高 rightCode.setBounds(300, 256, 50, 30); //添加到界面 this.getContentPane().add(rightCode); //设置登录按钮 loginButton.setBounds(123, 310, 128, 47); ImageIcon icon1 = new ImageIcon(".....\\image\\login\\登录按钮.png"); loginButton.setIcon(icon1); //去除按钮的边框 loginButton.setBorderPainted(false); //去除按钮的背景 loginButton.setContentAreaFilled(false); loginButton.addMouseListener(this); this.getContentPane().add(loginButton); //设置注册按钮 enrollButton.setBounds(256, 310, 128, 47); ImageIcon icon2 = new ImageIcon("......\\PinTu\\image\\login\\注册按钮.png"); enrollButton.setIcon(icon2); //去除按钮的边框 enrollButton.setBorderPainted(false); //去除按钮的背景 enrollButton.setContentAreaFilled(false); enrollButton.addMouseListener(this); this.getContentPane().add(enrollButton); //创建背景色 JLabel back = new JLabel(new ImageIcon("......\\PinTu\\image\\login\\background.png")); back.setBounds(0, 0, 470, 390); this.getContentPane().add(back); }

4.弹窗设置

public void showDialog(String str){ JDialog dialog = new JDialog(); dialog.setSize(180,150); //让弹框置顶 dialog.setAlwaysOnTop(true); //让弹框居中 dialog.setLocationRelativeTo(null); //弹框不关闭永远无法操作下面的界面 dialog.setModal(true); JLabel warning = new JLabel(str); //让显示的文字居中 warning.setHorizontalAlignment(JLabel.CENTER); warning.setBounds(0, 0, 200, 150); dialog.getContentPane().add(warning); //让弹框展示出来 dialog.setVisible(true); } 3.登录页面逻辑实现

鼠标点击监听

@Override public void mouseClicked(MouseEvent m) { if (m.getSource() == loginButton){ System.out.println("用户点击登录按钮"); String name = UserjTextField.getText(); String paw = passwordField.getText(); //获取验证码 String codeInput = code.getText(); UserInfo user = new UserInfo(name,paw); System.out.println("用户输入的用户名为:"+name); System.out.println("用户输入的密码为:"+paw); if (codeInput.length() == 0 && name.length() !=0 && paw.length() !=0){ System.out.println("用户验证码为空!"); showDialog("验证码不能为空!"); String code = CodeUtil.Create(5);//创建一个5位长度的随机验证码 rightCode.setText(code); }else if (name.length() ==0 || paw.length() == 0){ System.out.println( "用户名或者密码为空"); showDialog("用户名或者密码为空"); String code = CodeUtil.Create(5); rightCode.setText(code); //判断输入的验证码与生成的验证码是否相同 } else if (!codeInput.equalsIgnoreCase(rightCode.getText())) { showDialog("验证码输入错误"); String code = CodeUtil.Create(5); rightCode.setText(code); }else if(contains(user)){ System.out.println("用户输入账号正确,登录成功!"); //关闭登陆界面 this.setVisible(false); new Game(); }else { System.out.println("用户名或密码错误"); showDialog("用户名或密码错误"); String code = CodeUtil.Create(5); rightCode.setText(code); } } //更换验证码 if(m.getSource() == rightCode){ System.out.println("更换验证码!"); String code = CodeUtil.Create(5); rightCode.setText(code); } //进入注册页面 if(m.getSource() == enrollButton){ System.out.println("用户注册"); this.dispose(); new Register(); } }

鼠标长按监听

//鼠标长按 @Override public void mousePressed(MouseEvent m) { if (m.getSource() == loginButton){ loginButton.setIcon(new ImageIcon("......\\PinTu\\image\\login\\登录按下.png")); }else if (m.getSource() == enrollButton){ enrollButton.setIcon(new ImageIcon("......\\PinTu\\image\\login\\注册按下.png")); } //显示查看密码 else if (m.getSource() == seelabel){ seelabel.setIcon(new ImageIcon("......\\PinTu\\image\\login\\显示密码按下.png")); passwordField.setEchoChar((char)0); } }

鼠标松开监听

//松开按钮 @Override public void mouseReleased(MouseEvent m) { if (m.getSource() == loginButton){ loginButton.setIcon(new ImageIcon("......\\PinTu\\image\\login\\登录按钮.png")); }else if (m.getSource() == enrollButton){ enrollButton.setIcon(new ImageIcon("......\\PinTu\\image\\login\\注册按钮.png")); } else if (m.getSource() == seelabel){ seelabel.setIcon(new ImageIcon("......\\PinTu\\image\\login\\显示密码.png")); passwordField.setEchoChar('*'); } }

**

验证码生成

import java.util.Random; public class CodeUtil { public static String Create(int n){ String code = ""; Random r = new Random();//随机生成 for (int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3