永发信息网

用java写一个登陆注册界面代码

答案:5  悬赏:40  手机版
解决时间 2021-12-04 10:37
用java写一个登陆注册界面代码
最佳答案
Java程序如下:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

//登录窗体类
public class Main extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
protected JLabel lblPrompt, lblTime, lblUserName, lblPassword;
protected JTextField txtUserName;
protected JPasswordField txtPassword;
protected JButton btnLogin, btnRegister;
protected static Thread thread = null;

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

public Main() {
super("用户登录");

initComponent();
}

//初始化控件
public void initComponent() {
lblPrompt = new JLabel("登录剩余时间(秒):");
lblTime = new JLabel("60");
lblUserName = new JLabel("用户名:");
lblPassword = new JLabel("密    码:");

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

btnLogin = new JButton("登录");
btnRegister = new JButton("注册");

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

this.setLayout(new GridLayout(4, 2));
this.add(lblPrompt);
this.add(lblTime);
this.add(lblUserName);
this.add(txtUserName);
this.add(lblPassword);
this.add(txtPassword);
this.add(btnLogin);
this.add(btnRegister);

txtUserName.setFocusable(true);

this.setSize(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() {
while(true) {
try {
Thread.sleep(1000);
int num = Integer.valueOf(lblTime.getText());
if(num <= 0) {
JOptionPane.showMessageDialog(this, "时间到,窗口即将关闭!", "登录失败", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else {
num--;
}
lblTime.setText(String.valueOf(num));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}

//用户注册窗体类
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);

this.setSize(400, 200);
this.setVisible(true);
}

@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 + " 注册成功,请牢记您的用户名和密码。
单击"确定"按钮开始登录", "注册成功", JOptionPane.INFORMATION_MESSAGE);

this.dispose();
new Main();
}
}
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 customers = new ArrayList();

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

运行过程截图:


全部回答
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Test26 {

public static void main(String[] args) {
final String userName = "abc";
final String passwrod = "123";
JFrame jFrame = new JFrame("登陆界面");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
jFrame.add(label1);

JLabel label2 = new JLabel("密码");
label2.setBounds(10, 40, 100, 30);
jFrame.add(label2);

final JTextField text1 = new JTextField();
text1.setBounds(50, 15, 130, 20);
jFrame.add(text1);

final JPasswordField text2 = new JPasswordField();
text2.setBounds(50, 45, 130, 20);
jFrame.add(text2);

JButton button = new JButton("Login");
button.setBounds(10, 75, 170, 40);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if(userName.equals(text1.getText()) && passwrod.equals(text2.getText())) {
JOptionPane.showMessageDialog(null, "登陆成功误", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);

jFrame.setVisible(true);
}

}
什么需求,发我
是在控制台输出的那种吗
使用框架可以吗 ssi,或者ssh~追问据说用什么文件流追答我帮你看看,我有没有代码,写估计会来不及追问嗯嗯 QAQ一定要有啊追答代码太多啊,我提交不了,能用其它工具发给你吗?追问好 什么工具 邮箱吗?.?追答邮箱吧 你们考多久?估计没时间了...追问不是考试,是作业 851159052QQ邮箱追答嗯 兄弟 先给分吧 ,我办你搞定追问噢大神,不要消失啊QAQ
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
邓州市南阳名人名剪(人民路)这个地址怎么能查
五年级下册语文课文14课小练笔以朝鲜人民的口
与另一车发生轻微擦碰,前轮左上方有些刮痕,
羽加中,左羽右中,怎么念?
亚里士多德提出的所有错误观点
是所有人在准备跑步的时候都特别紧张吗?
请问形容泥土的AABB词语有什么?(一个就行)
我媳妇叫杨阳,求一个与杨阳大概相似的网名
下面的算式是按一定的规律排列的,4+2,5+8,
麻将新手,问一个有关杠的问题
如何将运动的实物拍成静止状态,比如运转的风
Jeep指南者怎么样?值得买吗?
物流进出口操作主要做什么?
描写快马在道路上奔驰的句子
帮忙想个三个字的植物!
推荐资讯
十几米的大树下可以种什么
蜂蜡能对水喝吗
ikon成员都毕业于哪儿
"高位者的寂寞 不过是因为下属只是惧怕 从未
泸州gBt22045一瓶价格
西门子PLC如何编程多台冷却塔风机时门均恒控
开个喜糖铺,求店名
男主是赛车手,女主有一个朋友,和朋友一起开
求农村家庭大门瓷砖对联
郑州哪里做汽车隔音做的好,我想做全车隔音,
凉席上有螨虫怎么除掉
全城热恋东哥太温柔小明明5号他们怎么不参加
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?