java登录界面

 设计思路:

读取用户输入的信息,包括账号密码,随机产生一个6位的字符串。
验证用户输入的验证码是不是和产生的随机字符串一样。
如果不是一样的话就提示并且回到重新输入的界面。
如果一样的话就提示登陆成功,并且刷新验证码,以方便下一个用户的使用。

流程图:

原程序代码:

//孙丙海 信1605-2
package 登陆界面;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.sun.javafx.tk.Toolkit;

//登录窗体类
public class Login extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
private String base = "abcdefghijklmnopqrstuvwxyz";
private StringBuffer sb;
protected JLabel lblPersonal, lblUserName, lblPassword,str;
protected JTextField txtUserName;
protected JPasswordField txtPassword;
protected JTextField txtPersonal;
protected JButton btnLogin, btnRegister;
protected static Thread thread = null;

public static void main(String[] args) {
Data.init();
Login frmLogin = new Login();
thread = new Thread(frmLogin);
thread.start();
}

public Login() {
super("欢迎使用本软件");

initComponent();
}

//初始化控件
public void initComponent()
{
lblUserName = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
// lblPersonal = new JLabel("验证码:");

txtUserName = new JTextField(10);
txtPassword = new JPasswordField(10);
txtPersonal = new JPasswordField(10);
txtPersonal.setBounds(209, 400, 200, 400);
btnLogin = new JButton("登录");
btnRegister = new JButton("注册");
txtPersonal.setText("");


btnLogin.addActionListener(this);
btnRegister.addActionListener(this);
txtPersonal.addActionListener(this);

this.setLayout(new GridLayout(4, 3));

this.add(lblUserName);
this.add(txtUserName);
this.add(lblPassword);
this.add(txtPassword);
// this.add(lblPersonal);
Random random = new Random();
sb = new StringBuffer();
for (int i = 0; i < 6; i++)
{
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
str=new JLabel("验证码:"+sb.toString());
str.setSize(10,45);
add(str);
this.add(txtPersonal);
this.add(btnLogin);
this.add(btnRegister);

txtUserName.setFocusable(true);
setBounds(400,300,400,200);
this.setVisible(true);
}


@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();

if(btn == btnLogin) {
if(txtUserName.getText().equals("") || txtUserName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(this, "用户名不能为空!", "登录失败", JOptionPane.ERROR_MESSAGE);
return;
}
if(txtPassword.getText().equals("")) {
JOptionPane.showMessageDialog(this, "登录密码不能为空!", "登录失败", JOptionPane.ERROR_MESSAGE);
return;
}
String userName = null;
String password = null;

userName = txtUserName.getText().trim();
password = txtPassword.getText();
int i;

for(i=0; i < Data.customers.size(); i++) {
if(Data.customers.get(i).getUserName().equals(userName) && Data.customers.get(i).getPassword().equals(password)) {
break;
}
}

if(i < Data.customers.size()) {
JOptionPane.showMessageDialog(this, "欢迎您," + userName, "登录成功", JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(this, "登录失败,请检查用户名和密码是否正确......", "登录失败", JOptionPane.ERROR_MESSAGE);
}
}
else if(btn == btnRegister) {
this.dispose();
new Register();
}
}

@Override
public void run() {
// TODO Auto-generated method stub

}
}


//用户注册窗体类
class Register extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
protected JLabel lblUserName, lblPassword, lblConfirmedPassword;
protected JTextField txtUserName;
protected JPasswordField txtPassword, txtConfirmedPassword;
protected JButton btnRegister, btnReset;

public Register() {
super("用户注册");

initComponent();
}

//初始化控件
public void initComponent() {
lblUserName = new JLabel("用 户 名:");
lblPassword = new JLabel("密 码:");
lblConfirmedPassword = new JLabel("确认密码:");

txtUserName = new JTextField(10);
txtPassword = new JPasswordField(10);
txtConfirmedPassword = new JPasswordField(10);

btnReset = new JButton("重置");
btnRegister = new JButton("注册");

btnReset.addActionListener(this);
btnRegister.addActionListener(this);

this.setLayout(new GridLayout(4, 2));
this.add(lblUserName);
this.add(txtUserName);
this.add(lblPassword);
this.add(txtPassword);
this.add(lblConfirmedPassword);
this.add(txtConfirmedPassword);
this.add(btnRegister);
this.add(btnReset);

txtUserName.setFocusable(true);

setBounds(400,300,600,400);
JFrame jFrame = new JFrame("登陆界面");
jFrame.setLocationRelativeTo(null);
this.setVisible(true);
this.pack();
}

@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();

if(btn == btnRegister) {
if(txtUserName.getText().equals("") || txtUserName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(this, "用户名不能为空!", "注册失败", JOptionPane.ERROR_MESSAGE);
return;
}
if(txtPassword.getText().equals("")) {
JOptionPane.showMessageDialog(this, "登录密码不能为空!", "注册失败", JOptionPane.ERROR_MESSAGE);
return;
}
if(txtConfirmedPassword.getText().equals("")) {
JOptionPane.showMessageDialog(this, "确证密码不能为空!", "注册失败", JOptionPane.ERROR_MESSAGE);
return;
}
if(! txtConfirmedPassword.getText().equals(txtPassword.getText())) {
JOptionPane.showMessageDialog(this, "两次密码必须一致!", "注册失败", JOptionPane.ERROR_MESSAGE);
return;
}

String userName = null;
String password = null;
int i;

userName = txtUserName.getText().trim();
password = txtPassword.getText();

for(i=0; i < Data.customers.size(); i++) {
if(Data.customers.get(i).getUserName().equals(userName)) {
break;
}
}

if(i < Data.customers.size()) {
JOptionPane.showMessageDialog(this, "该用户名已经被注册,请选用其他用户名!", "注册失败", JOptionPane.ERROR_MESSAGE);
}
else {
Data.customers.add(new Customer(userName, password));
JOptionPane.showMessageDialog(this, "恭喜 " + userName + " 注册成功,请牢记您的用户名和密码。\n单击\"确定\"按钮开始登录", "注册成功", JOptionPane.INFORMATION_MESSAGE);

this.dispose();
new Login();
}
}
else if(btn == btnReset) {
txtUserName.setText("");
txtPassword.setText("");
txtConfirmedPassword.setText("");
txtUserName.setFocusable(true);
}
}
}

//用户信息类
class Customer {
protected String userName = null;
protected String password = null;

public Customer() {
}

public Customer(String userName, String password) {
this.userName = userName;
this.password = password;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}
}

//缓存用户信息的集合类
class Data {
public static List<Customer> customers = new ArrayList<Customer>();

public static void init() {
customers.add(new Customer("fxk", "123456"));
}
}

运行结果截图:

 

转载于:https://www.cnblogs.com/xiaohaigege666/p/7636371.html

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java登陆界面的代码示例: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Login extends JFrame implements ActionListener { JLabel userLabel, passwordLabel, messageLabel; JTextField userTextField; JPasswordField passwordField; JButton loginButton, resetButton; Login() { setTitle("Login Form"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); userLabel = new JLabel("Username:"); passwordLabel = new JLabel("Password:"); userTextField = new JTextField(); passwordField = new JPasswordField(); loginButton = new JButton("Login"); resetButton = new JButton("Reset"); messageLabel = new JLabel(); messageLabel.setForeground(Color.red); setLayout(new GridLayout(3, 2)); add(userLabel); add(userTextField); add(passwordLabel); add(passwordField); add(loginButton); add(resetButton); add(messageLabel); loginButton.addActionListener(this); resetButton.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent ae) { String userText; String passwordText; if (ae.getSource() == loginButton) { userText = userTextField.getText(); passwordText = passwordField.getText(); if (userText.trim().equals("admin") && passwordText.trim().equals("admin123")) { messageLabel.setText("Login Successful!"); } else { messageLabel.setText("Invalid Username or Password"); } } if (ae.getSource() == resetButton) { userTextField.setText(""); passwordField.setText(""); } } public static void main(String[] args) { Login loginForm = new Login(); } } ``` 这个代码示例包含了一个简单的登录表单,其中包括用户名和密码输入框、登录和重置按钮以及用于显示错误信息的标签。当用户点击登录按钮时,程序会检查输入的用户名和密码是否正确,并在标签上显示相应的信息。重置按钮可以清空输入框。 当然,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值