JAVA—图形化“登录,注册”界面

提笔了无痕 2024-06-27 11:35:03 阅读 61

前言:学习了一段时间JAVA的swing组件,心血来潮写了一个登录,注册界面。

           知道大伙喜欢美女,所以把用户登录界面背景设置成了beauty!

所用知识基本上都属于swing组件:

        javax.siwng.JFrame; //窗体类

        javax.swing.JButton; //按钮类

        javax.swing.JLabel; //标签类

        javax.swing.JTextField; //文本框类

        javax.swing.JPasswordField; //密码框类

        javax.swing.ImageIcon; //图标类

        集合ArrayList;

        MouseListener鼠标监听器;

        JOptionPane.showMessageDialog(Component parentComponent, Object message)方法

        该方法是弹出一个消息框,代码中使用了多次。效果如图:

 代码实现如下:

//  个人觉得main函数里面的内容越精简越好

public class GameWin {

        public static void main(String[] args) {

                //创建了一个Register_LogIn类的对象register_logIn,并调用了空参构造

                Register_LogIn register_logIn=new Register_LogIn();        

        }

}

看到这里还是一脸蒙吧,别急,往下看。

下面这个就是创建的Register_LogIn类,他的空参构造在下面也会详细讲解。

//需要用到的类

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

import java.util.Random;

//创建登录——注册类

public class Register_LogIn extends JFrame {

//创建集合用来存放账户和密码信息。

static ArrayList<String> users=new ArrayList<>();

//下面的代码都在这个类里面

// //构造setCode方法来创建一个随机的验证码

String setCode(){

Random r=new Random();

char[] c=new char[52]; //在数组中存放52个英文字母

String result="";

for (int i = 0; i < c.length; i++) {

if(i<26){

c[i]=(char)(65+i);//存放a~z

} else {

c[i]=(char)(97+i-26);//存放A~Z

}

}

for (int i = 0; i < 4; i++) {

int number=r.nextInt(c.length);

result=result+c[number];

}

int figure=r.nextInt(10);

result=result+figure;

//将字符串变为数组

char[] crr=result.toCharArray();

char t;

//将数组打乱

for (int i = 0; i < crr.length; i++) {

int rom=r.nextInt(5);

t=crr[i];

crr[i]=crr[rom];

crr[rom]=t;

}

//得到验证码

return String.valueOf(crr);

}

//这里就是空参构造了,用来构建用户窗口

public Register_LogIn(){

//创建开始界面

JFrame jFrame=new JFrame();

jFrame.setTitle("用户登录界面");

jFrame.setLocationRelativeTo(null);

jFrame.setSize(800,480);

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setLayout(null);

//添加背景图片

ImageIcon icon=new ImageIcon("D:\\壁纸\\small074417O9mVb1695253457.jpg");

JLabel jLabel=new JLabel(icon);

jLabel.setBounds(0,0,800,440);

//设置界面元素

JLabel account=new JLabel("账号");

JLabel password=new JLabel("密码");

JLabel code=new JLabel("验证码");

JButton renew=new JButton(setCode());

//将验证码的值赋给renew按钮

renew.setText(setCode());

JButton register=new JButton("注册");

JButton logIn=new JButton("登录");

                //这三个是输入框,下面图片中灰色的框就是

JTextField account_=new JTextField(80);

JTextField password_=new JTextField(80);

JTextField code_=new JTextField(70);

//按钮的背景

register.setIcon(new ImageIcon("C:\\Users\\21299\\Pictures\\Camera Roll\\1-图片\\imgs\\AE26B098-CB4C-46d5-A6D8-AAE4330AAFCB.png"));

logIn.setIcon(new ImageIcon("C:\\Users\\21299\\Pictures\\Camera Roll\\1-图片\\imgs\\46B4E49A-7D7D-4dfb-916F-0881A4634E9A.png"));

//设置位置

register.setBounds(200,380,140,45);

logIn.setBounds(450,380,140,45);

renew.setBounds(410,340,90,30);

account.setBounds(250,250,80,50);

password.setBounds(250,290,80,50);

code.setBounds(235,330,80,50);

account_.setBounds(300,260,200,30);

password_.setBounds(300,300,200,30);

code_.setBounds(300,340,100,30);

//设置按钮字体的颜色和字样

account.setForeground(Color.GREEN);

password.setForeground(Color.GREEN);

code.setForeground(Color.GREEN);

account.setFont(new Font("楷体",Font.BOLD,20));

password.setFont(new Font("楷体",Font.BOLD,20));

code.setFont(new Font("楷体",Font.BOLD,20));

//输入框的颜色

account_.setBackground(Color.lightGray);

password_.setBackground(Color.lightGray);

code_.setBackground(Color.lightGray);

//将元素添加进窗口

jFrame.add(account);

jFrame.add(account_);

jFrame.add(password);

jFrame.add(password_);

jFrame.add(code);

jFrame.add(code_);

jFrame.add(register);

jFrame.add(logIn);

jFrame.add(renew);

jFrame.add(jLabel);

jFrame.setVisible(true);

        //写到这里就可以呈现出下面的窗口画面(1)了

 图(1):

//给注册,登录按钮添加鼠标监听器

register.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

super.mouseClicked(e);

if(e.getButton()==1){

//如果左键点击注册,跳转进Register窗口

new Register();

}

}

});

logIn.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

super.mouseClicked(e);

if(e.getButton()==1){

if(account_.getText().isEmpty() ||password_.getText().isEmpty()){

JOptionPane.showMessageDialog(null,"请输入完整信息再登录");

} else if (!renew.getText().equals(code_.getText())) {

JOptionPane.showMessageDialog(null,"验证码输入错误");

} else{

//输入的账户,密码是否正确的依据

boolean flag = false;

for(int i=0;i<users.size();i++){

if(account_.getText().equals(users.get(i))&&password_.getText().equals(users.get(i+1))){

flag=true;//如果无误,则true

break;

}

}

if(flag){

JOptionPane.showMessageDialog(null,"登录成功!");

}else{

JOptionPane.showMessageDialog(null,"用户不存在");

}

}

}

}

});

}

}

 下面是上面代码中写的登录时会出现的几种情况,运行出来如下:

下面的是注册账户和密码的窗口,登录的前提是创建的有,所以注册的处理也想当重要;

和上面的用户登录窗口大差小不差,所以我没有添加背景什么的,看上去就是一个简朴的窗口,有兴趣的话可以自己设置添加一些元素,让这个窗口看上去更好看。

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

public class Register extends JFrame {

boolean flag;//flag为账户是否已经存在的标准

public Register(){

//注册界面的窗口设置

this.setTitle("注册界面");

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

this.setSize(500,300);

this.setLocationRelativeTo(null);

this.setLayout(null);

this.setVisible(true);

//设置窗口元素

JLabel account=new JLabel("注册账户");

account.setBounds(50,50,100,50);

account.setFont(new Font("楷体",Font.BOLD,20));

JLabel password=new JLabel("注册密码");

password.setBounds(50,100,100,50);

password.setFont(new Font("楷体",Font.BOLD,20));

JLabel nextpassword=new JLabel("再次输入密码");

nextpassword.setBounds(50,150,150,50);

nextpassword.setFont(new Font("楷体",Font.BOLD,20));

JTextField account_=new JTextField(30);

JTextField password_=new JTextField(30);

JTextField nextpassword_=new JTextField(30);

account_.setBounds(150,60,250,30);

password_.setBounds(150,110,250,30);

nextpassword_.setBounds(200,160,200,30);

JButton jButton=new JButton("注册");

jButton.setBounds(120,200,80,40);

this.add(jButton);

JButton jButton2=new JButton("退出");

jButton2.setBounds(240,200,80,40);

this.add(jButton2);

//添加入窗口

this.add(account);

this.add(account_);

this.add(password);

this.add(password_);

this.add(nextpassword);

this.add(nextpassword_);

//代码敲到这里就可以实现这个窗口的基本布局了,如下图(2)

//给注册和退出添加鼠标监听器

jButton.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

super.mouseClicked(e);

if(e.getButton()==1){

//for—each循环遍历,判断当前注册的账户是否已经存在

for(String str:Register_LogIn.users){

String accountStr=account_.getText();

if(str.equals(accountStr)) {

flag = true;

break;

}

}

if(!flag){

//如果可以注册,接着判断是否为空

if(account_.getText().isEmpty()

                        ||password_.getText().isEmpty()){

JOptionPane.showMessageDialog(null,"账户或密码不能为空");

}else{

//判断两次密码是否一致

String passwordStr=password_.getText();

String nextpasswordStr=nextpassword_.getText();

if(passwordStr.equals(nextpasswordStr)){

//将账户和密码添加到集合中

String accountStr=account_.getText();

Register_LogIn.users.add(accountStr);

Register_LogIn.users.add(passwordStr);

JOptionPane.showMessageDialog(null,"注册成功");

flag=false;

}

else{

JOptionPane.showMessageDialog(null,"两次密码输入不一致");

}

}

}else{

JOptionPane.showMessageDialog(null, "该用户已存在!");

}

}

}

});

jButton2.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

super.mouseClicked(e);

if(e.getButton()==1){

//点击按钮“退出”后关闭此界面

dispose();

}

}

});

}

}

图(2):

 下面是代码中写的注册新账户时会出现的几种情况,运行出来如下:

写完这一切代码后,恭喜你,已经完成了图形化“登录,注册”界面!

如果各位老板有什么不懂或者比我方法更好的,欢迎在评论区留言~



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。