日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java登录界面

發(fā)布時間:2025/3/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java登录界面 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?設(shè)計(jì)思路:

讀取用戶輸入的信息,包括賬號密碼,隨機(jī)產(chǎn)生一個6位的字符串。
驗(yàn)證用戶輸入的驗(yàn)證碼是不是和產(chǎn)生的隨機(jī)字符串一樣。
如果不是一樣的話就提示并且回到重新輸入的界面。
如果一樣的話就提示登陸成功,并且刷新驗(yàn)證碼,以方便下一個用戶的使用。

流程圖:

原程序代碼:

//孫丙海 信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("驗(yàn)證碼:");

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("驗(yàn)證碼:"+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("確認(rèn)密碼:");

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, "該用戶名已經(jīng)被注冊,請選用其他用戶名!", "注冊失敗", 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"));
}
}

運(yùn)行結(jié)果截圖:

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaohaigege666/p/7636371.html

總結(jié)

以上是生活随笔為你收集整理的java登录界面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。