创建登入界面(有验证码)

创建登入界面(有验证码)


//信1805-1王正帅20183544
package textpass;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LognIn extends JFrame implements ActionListener {
private JLabel jl2 = new JLabel("");
private JLabel jlArray1=new JLabel("验证码");
private String code=new String(randomNumber());
private JButton jbA =new JButton(code);
private JButton jbD=new JButton("登陆");
private JButton jbZ=new JButton("注册");
private JButton jbM=new JButton("找密码");
private JLabel jlY=new JLabel("用户名");
private JLabel jlMB=new JLabel("密 码");
private Font f1=new Font("斜体",Font.BOLD,13);
private JTextField jtxtName =new JTextField();
private JTextField jtxt =new JTextField();//++
private JButton jb1 =new JButton("手机号或邮箱");
private JPasswordField jtxtPassword= new JPasswordField();
private JPanel jp=new JPanel();//面板
private int f2=0;
public void name() {
jlY.setBounds(30, 10, 80, 26);
jp.add(jlY);
}
public void PasswordB() {
jlMB.setBounds(30, 60, 80, 26);
jp.add(jlMB);

}
public void Logn() {
jbD.setBounds(50, 180, 60,26);
jp.add(jbD);
jbD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int f=0;
if(jtxtName.getText().length()>=3&&jtxtName.getText().length()<=11) {//登入名大于3位小于11位。
f++;
}
else if(jtxtName.getText().length()==0) {
JOptionPane.showMessageDialog(null,"用户名为空");
}
else{
JOptionPane.showMessageDialog(null,"用户名输入有误");
}
if(String.valueOf(jtxtPassword.getPassword()).length()>=3
&&String.valueOf(jtxtPassword.getPassword()).length()<=12) {//登密码大于3位小于12位。获取文本框的内容
f++;
}
else if(String.valueOf(jtxtPassword.getPassword()).length()==0) {
JOptionPane.showMessageDialog(null,"密码为空");
}
else{
JOptionPane.showMessageDialog(null,"密码输入有误");
}
if(String.valueOf(jtxt.getText()).equals(code)) {

f++;
}

else if(String.valueOf(jtxt.getText()).length()==0) {
JOptionPane.showMessageDialog(null,"验证码为空!");
}
else{
JOptionPane.showMessageDialog(null,"验证码输入有误!");
}
if(f==3) {
JOptionPane.showMessageDialog(null,"登入成功!");
jl2.setText("成功");
}
else {
jl2.setText("失败");
jl2.setFont(f1);
jl2.setForeground(Color.WHITE);//设置文字的颜色
}

}

});
}
public void Registration() {
jbZ.setBounds(120, 180, 60,26);
jp.add(jbZ);
//监听
jbZ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbZ){ //按注册键
jtxtName.requestFocus();
LognIn l2=new LognIn();
l2.setTitle("请注册");
l2.RegistrationE();
if(e.getSource()==jbZ) {
if(jtxtName.getText().equals("17734103685")&&String.valueOf(jtxtPassword.getPassword()).equals("lxy208751")) {
JOptionPane.showMessageDialog(null,"注册成功");
}
}
}

}

});
}
public void Password() {
jbM.setBounds(190, 180, 80,26);
jp.add(jbM);
jbM.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(e.getSource()==jbM){ //按找密码键
f2++;
//Empty();
jtxtName.requestFocus();
LognIn l3=new LognIn();
l3.setTitle("找密码");
l3.PasswordE();
if(e.getSource()==jbM&&jtxtName.getText().equals("17734103685")) {
JOptionPane.showMessageDialog(null,"密码是:lxy208751");

}
}


}

});
}
public void RegistrationE() {
jbD.setVisible(false);
jbM.setVisible(false);
jbZ.setBounds(80, 150, 100,26);
this.setBounds(1000, 200, 300, 250);//设置界面出现在屏幕上的位置
}
public void PasswordE() {
jbD.setVisible(false);
jbZ.setVisible(false);
jtxtPassword.setVisible(false);
jlMB.setVisible(false);
jlArray1.setBounds(30, 90, 80,26);
jtxt.setBounds(80,90,80,30);
jbA.setBounds(180, 90, 100, 26);
this.setBounds(600, 200, 300, 250);//设置界面出现在屏幕上的位置
}
public LognIn(){
jp.setLayout(null); //满换行
jp.setBackground(Color.ORANGE);
randonA();
jlArray1.setBounds(30, 110, 80, 26);//设置文字30据左,120上下
jp.add(jlArray1);//显示
jb1.setBounds(80,10,130,30);
jp.add(jb1);
jb1.addActionListener(this);
jb1.setBackground(Color.WHITE);
PasswordB();name();
Logn();Registration();Password();
jtxtName.setBounds(80,10,180,30);//设置文本框80左右10上下(位置)180长30宽
jp.add(jtxtName);
jtxtName.addActionListener(this);
jtxt.setBounds(80,110,80,30);
jp.add(jtxt);
jtxt.addActionListener(this);
jtxtPassword.setBounds(80,60,180,30);
jp.add(jtxtPassword);
jtxtPassword.setEchoChar('*');
jtxtPassword.addActionListener(this);
jl2.setBounds(10, 180, 300, 30);
jp.add(jl2);
this.add(jp);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setResizable(false);
this.setBounds(800, 200, 300, 250);//设置界面出现在屏幕上的位置
this.setVisible(true);

}

public String randomNumber() {//生成验证码
String code=new String("");
for(int i=1;i<=6;i++){
Random random=new Random();
int character=random.nextInt(26)+97;//随机产生97-123之间的数

code=code+(char)character;//将产生的数转化为char类型为小写字母
}
return code;
}
public void Empty() {
jl2.setText("");
jlArray1.setText("");
jtxt.setText("");
jtxtName.setText("");
jtxtPassword.setText("");
}
public void randonA() {
System.out.println(code);
jbA.setBounds(180, 110, 100, 26);//设置文字30据左,120上下
jbA.addActionListener(this);
Font f=new Font("斜体",Font.BOLD,13);
jbA.setFont(f);
jbA.setBackground(Color.orange);
jp.add(jbA);//显示
}


public void actionPerformed(ActionEvent e){

if(e.getSource()==jtxtName){
jtxtPassword.requestFocus();
}
else if(e.getSource()==jbA) {
code=randomNumber();
jbA.setVisible(false);
jbA =new JButton(code);
randonA();
}

else if(e.getSource()==jb1) {
jb1.setVisible(false);
}

}
public static void main(String[] args) {
LognIn l1=new LognIn();
l1.setTitle("请登陆");

}
}

运行测试:

 

 

 

原文地址:https://www.cnblogs.com/20183544-wangzhengshuai/p/11537689.html