登录界面9.17
package test;
import javax.swing.*;
import java.awt.*;
public class test2 {
//创造字符串储存账号
public static String uname[] = new String[100];
//创造字符串储存密码
public static String upass[] = new String[100];
//记录当前坐标
public static int flag_user = 0;
public static void main(String[] args) {
JFrame jFrame = new JFrame("启动!");
//JFrame可以创建一个窗口,并且给窗口命名;
jFrame.setSize(900, 507);
//setSize函数用来设置生成窗口的大小;
jFrame.setLayout(null);
//布局管理器设置为null;
//添加标题
JLabel title = new JLabel("原神启动器");
//JLabel可以给生成的界面添加文字内容;
title.setForeground(new Color(0, 0, 0, 250));
//给文字上色;
title.setFont(new Font("宋体", Font.PLAIN, 50));
//规定文字的字体和大小;
title.setBounds(280, 50, 800, 100);
//规定文字生成的位置;
jFrame.add(title);
//添加用户名;
JLabel userName = new JLabel("用户名:");
userName.setForeground(new Color(0, 0, 0, 250));
userName.setFont(new Font("宋体", Font.PLAIN, 30));
userName.setBounds(200, 140, 200, 100);
jFrame.add(userName);
//添加密码;
JLabel userPassWord = new JLabel("密码:");
userPassWord.setForeground(new Color(0, 0, 0, 250));
userPassWord.setFont(new Font("宋体", Font.PLAIN, 30));
userPassWord.setBounds(200, 200, 200, 100);
jFrame.add(userPassWord);
//添加验证码
String stra = str();
JLabel setNum = new JLabel(stra);
setNum.setForeground(new Color(0, 0, 0, 250));
setNum.setFont(new Font("宋体", Font.PLAIN, 30));
setNum.setBounds(610, 290, 100, 40);
jFrame.add(setNum);
//创建一个用户名的输入栏
JTextField name = new JTextField(20);//20是输入上限;
//JTextField可以生成一个可供用户输入的输入栏;
name.setForeground(new Color(0, 0, 0, 250));
name.setFont(new Font("宋体", Font.PLAIN, 30));
name.setBounds(330, 170, 280, 40);
jFrame.add(name);
//创建一个密码的输入栏;
JTextField passWord = new JTextField(20);//20是输入上限;
passWord.setForeground(new Color(0, 0, 0, 250));
passWord.setFont(new Font("宋体", Font.PLAIN, 30));
passWord.setBounds(330, 230, 280, 40);
jFrame.add(passWord);
//创建验证码的输入栏
JTextField num = new JTextField(20);//20是输入上限;
num.setForeground(new Color(0, 0, 0, 250));
num.setFont(new Font("宋体", Font.PLAIN, 30));
num.setBounds(330, 290, 280, 40);
jFrame.add(num);
//添加注册按钮
JButton reg = new JButton("注册");
//创建可交互按钮
//设置背景颜色
reg.setForeground(new Color(0,0,0,250));
reg.setBackground(new Color(0, 0, 0, 0));
//设置字体
reg.setFont(new Font("宋体", Font.PLAIN, 20));
reg.setBorderPainted(false);//不设置边框
reg.setBounds(400,330,100,50);
jFrame.add(reg);
reg.addActionListener((e->{
if(!name.getText().isEmpty())
{
if(!passWord.getText().isEmpty())
{
if(num.getText().equals(stra))
{
uname[flag_user]=name.getText();
upass[flag_user]=passWord.getText();
flag_user++;
}
else
{
JOptionPane.showMessageDialog(jFrame,"验证码错误","提示",JOptionPane.INFORMATION_MESSAGE);
num.setText("");
}
}
else
{
JOptionPane.showMessageDialog(jFrame,"密码没输","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(jFrame,"用户名没输","提示",JOptionPane.INFORMATION_MESSAGE);
}
}));
//添加登录按钮;
JButton loadIn = new JButton("登录");
//JButton可以创建一个可供用户交互的按钮;
loadIn.setForeground(new Color(0, 0, 0, 250));
loadIn.setBackground(new Color(0, 0, 0, 0));
//设置背景颜色
loadIn.setFont(new Font("宋体", Font.PLAIN, 20));
loadIn.setBorderPainted(false);
//不设置边框;
loadIn.setBounds(500, 330, 100, 50);
jFrame.add(loadIn);
//对按钮进行处理,使其可以进行交互;
loadIn.addActionListener((e->{
if(!name.getText().isEmpty())
{
if(!passWord.getText().isEmpty())
{
if(num.getText().equals(stra))
{
//正确标志
int flag = 0;
for(int i=0;i<flag_user;i++)
{
if(name.getText().equals(uname[i])&&passWord.getText().equals(upass[i]))
{
flag = 1;
break;
}
}
if(flag==1)
{
JOptionPane.showMessageDialog(jFrame,"原神启动成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(jFrame,"账号或密码错误","提示",JOptionPane.INFORMATION_MESSAGE);
}
//登录界面设置为不可见,既消去登陆界面
jFrame.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(jFrame,"验证码错误","提示",JOptionPane.INFORMATION_MESSAGE);
num.setText("");
}
}
else
{
JOptionPane.showMessageDialog(jFrame,"密码没输","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(jFrame,"用户名没输","提示",JOptionPane.INFORMATION_MESSAGE);
}
}));
//设置相对位置:屏幕中间
jFrame.setLocationRelativeTo(null);
//确保使用窗口关闭按钮,能够正常退出,结束进程!
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//禁止对窗口大小进行缩放处理
jFrame.setResizable(false);
//设置可见
jFrame.setVisible(true);
}
public static String str()
{
//定义一个空字符串
String result = "";
//进行6次循环
for(int i = 0 ; i < 6 ; i ++)
{
//生成一个随机小写字母
char intVal_1 = (char)('a'+Math.random() * ('z' - 'a' +1));
//生成一个随机大写字母
char intVal_2 = (char)('A'+Math.random() * ('Z' - 'A' +1));
//生成一个随机数字
char intVal_3 = (char)(48+Math.random() * (57 - 48 +1));
//随机的取是大写还是小写
int ran = (int)(0+Math.random()*3);
switch(ran)
{case 0:
result = result + intVal_1;break;
case 1:
result = result + intVal_2;break;
case 2:
result = result + intVal_3;break;
}
}
//输出随机字符串
return result;
}
}