用java实现了图形化登录界面
import javax.swing.*;标签:setBounds,30,9.15,add,new,JLabel,Font From: https://www.cnblogs.com/dddjm/p/17706347.html
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
public class Login {
public static void main(String[] args) {
Random random=new Random();
String s="123456789qwertyuiopasdfghjklzxcvbnm";
StringBuilder sb=new StringBuilder();
for(int i=0;i<6;i++){
int r=random.nextInt(0,35);
sb.append(s.charAt(r));
}
String sma=sb.toString();
ArrayList<number> arr=new ArrayList<>();
number n=new number("20224052","307812737");
arr.add(n);
JButton login=new JButton("登录");
JButton create=new JButton("注册");
login.setBounds(60,190,100,30);
create.setBounds(250,190,100,30);
create.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFrame jf1=new JFrame("注册界面");
jf1.setSize(350,300);
JButton c2=new JButton("确认");
c2.setBounds(130,200,80,30);
JTextField name=new JTextField();
JLabel key1=new JLabel("密码:");
JTextField key3=new JTextField();
JTextField key4=new JTextField();
key1.setFont(new Font("宋体",Font.PLAIN,14));
JLabel key2=new JLabel("密码确认:");
key2.setFont(new Font("宋体",Font.PLAIN,14));
JLabel name1=new JLabel("账号:");
name1.setFont(new Font("宋体",Font.PLAIN,14));
Container c=jf1.getContentPane();
name.setBounds(100,40,190,30);
name1.setBounds(50,40,70,30);
key1.setBounds(50,80,70,30);
key2.setBounds(20,120,90,30);
key3.setBounds(100,80,190,30);
key4.setBounds(100,120,190,30);
c.add(c2);
c.add(name1);
c.add(key1);
c.add(key2);
c.add(name);
c.add(key3);
c.add(key4);
jf1.setDefaultCloseOperation(2);
jf1.setLocationRelativeTo(null);
jf1.setLayout(null);
jf1.setVisible(true);
c2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int flagname=0;
String n=name.getText().trim();
String k1=key3.getText().trim();
String k2=key4.getText().trim();
for(int i=0;i< arr.size();i++){
if(arr.get(i).getName().equals(n)){
flagname=1;
}
}
if(flagname==1){
JOptionPane.showMessageDialog(null,"该账户已注册");
return;
}else{
if(!k1.equals(k2)){
JOptionPane.showMessageDialog(null,"两次密码输入不一致");
return;
}else{
JOptionPane.showMessageDialog(null,"注册成功");
number x=new number(n,k1);
arr.add(x);
}
}
}
});
}
});
JLabel ma=new JLabel("验证码:"+sma);
JLabel yama=new JLabel("验证码:");
yama.setFont(new Font("宋体",Font.PLAIN,14));
ma.setFont(new Font("宋体",Font.PLAIN,14));
JLabel key=new JLabel("密码:");
key.setFont(new Font("宋体",Font.PLAIN,14));
JLabel name=new JLabel("账号:");
name.setFont(new Font("宋体",Font.PLAIN,14));
JFrame jf=new JFrame("登录界面");
jf.add(name);
JTextField textma=new JTextField();
JTextField textname=new JTextField();
JPasswordField textkey=new JPasswordField();
login.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
int flagname=0,flagkey=0,index=0;
String name=textname.getText().trim();
String key=new String(textkey.getPassword()).trim();
String ma=textma.getText().trim();
if(name.equals("")){
JOptionPane.showMessageDialog(null,"请输入账号");
return;
}
for(int i=0;i< arr.size();i++){
if(arr.get(i).getName().equals(name)){
flagname=1;
index=i;
}
}
if(flagname==0){
JOptionPane.showMessageDialog(null,"该账号尚未注册");
return;
}else{
if(!arr.get(index).getKey().equals(key)){
JOptionPane.showMessageDialog(null,"密码错误");
return;
}else{
if(ma.equals(sma)) {
JOptionPane.showMessageDialog(null, "登录成功");
}else{
JOptionPane.showMessageDialog(null,"验证码错误");
}
}
}
}
});
textname.setBounds(120,40,210,30);
textkey.setBounds(120,90,210,30);
name.setBounds(70,40,100,30);
key.setBounds(70,90,100,30);
ma.setBounds(240,140,100,30);
yama.setBounds(50,140,80,30);
textma.setBounds(100,140,100,30);
Container c=jf.getContentPane();
c.add(create);
c.add(textname);
c.add(textkey);
c.add(name);
c.add(login);
c.add(key);
c.add(ma);
c.add(yama);
c.add(textma);
jf.setSize(400,300);
jf.setLocationRelativeTo(null);
jf.setLayout(null);
jf.setDefaultCloseOperation(2);
jf.setVisible(true);
}
}