用java写一个登陆注册界面代码
答案:5 悬赏:40 手机版
解决时间 2021-12-04 10:37
- 提问者网友:戎马万世
- 2021-12-03 11:26
用java写一个登陆注册界面代码
最佳答案
- 五星知识达人网友:独行浪子会拥风
- 2021-12-03 13:05
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.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
public static void init() {
customers.add(new Customer("fxk", "123456"));
}
}
运行过程截图:
全部回答
- 1楼网友:一把行者刀
- 2021-12-03 15:53
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);
}
}
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);
}
}
- 2楼网友:北城痞子
- 2021-12-03 14:59
什么需求,发我
- 3楼网友:撞了怀
- 2021-12-03 14:46
是在控制台输出的那种吗
- 4楼网友:旧脸谱
- 2021-12-03 14:07
使用框架可以吗 ssi,或者ssh~追问据说用什么文件流追答我帮你看看,我有没有代码,写估计会来不及追问嗯嗯 QAQ一定要有啊追答代码太多啊,我提交不了,能用其它工具发给你吗?追问好 什么工具 邮箱吗?.?追答邮箱吧 你们考多久?估计没时间了...追问不是考试,是作业 851159052QQ邮箱追答嗯 兄弟 先给分吧 ,我办你搞定追问噢大神,不要消失啊QAQ
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯