qq源代码 java_Java源码的仿QQ聊天程序

这是一个Java实现的仿QQ注册对话框,包含了账号、密码输入及验证功能。用户输入昵称和密码,程序会检查密码长度、一致性以及用户名是否已存在。点击提交后,如果所有条件满足,会显示注册成功,并关闭注册窗口。
摘要由CSDN通过智能技术生成

package com.qq.regist;

//Download by 链接已屏蔽

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusAdapter;

import java.awt.event.FocusEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class Regist extends JDialog {

private JLabel userId;

private JLabel userPassword;

private JLabel userPasswordConfirm;

private JTextField inputId;

private JPasswordField inputPassword;

private JPasswordField inputPasswordConfirm;

private JButton btSubmit;

private JButton btCancel;

public Regist() {

// 初始化JLabel和JButton

userId = new JLabel("帐号(昵称)");

userPassword = new JLabel("密码");

userPasswordConfirm = new JLabel("密码重复");

inputId = new JTextField("可以是中文");

inputPassword = new JPasswordField();

inputPasswordConfirm = new JPasswordField();

btSubmit = new JButton("提交");

btCancel = new JButton("取消");

// 设置此JFrame的属性

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screenSize = tk.getScreenSize();

int x = (int) screenSize.getWidth();

int y = (int) screenSize.getHeight();

this.setBounds((x - 240) / 2, (y - 600) / 2, 240, 600);

this.setLayout(null);

this.setModal(true);

// 设置JLabel属性

userId.setBounds(30, 100, 60, 20);

userPassword.setBounds(30, 140, 40, 20);

userPasswordConfirm.setBounds(30, 180, 60, 20);

// 设置文本域属性

inputId.setBounds(90, 100, 100, 20);

inputPassword.setBounds(90, 140, 100, 20);

inputPassword.setEchoChar('*');

inputPasswordConfirm.setBounds(90, 180, 100, 20);

inputPasswordConfirm.setEchoChar('*');

// 设置JButton属性

btSubmit.setBounds(50, 240, 60, 20);

btCancel.setBounds(120, 240, 60, 20);

// 添加inputId的监听器

inputId.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent arg0) {

inputId.setText("");

}

});

// 添加inputPassword的监听器

inputPassword.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent arg0) {

inputPassword.setText("");

}

});

inputPassword.addFocusListener(new FocusAdapter() {

public void focusLost(FocusEvent e) {

if ((new String(inputPassword.getPassword())).length() < 6) {

JOptionPane.showMessageDialog(null, "密码长度必须大于6");

inputPassword.setText("");

}

}

});

// 注册inputPasswordConfirm的监听器

inputPasswordConfirm.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

inputPasswordConfirm.setText("");

}

});

// 注册《提交》按钮的监听器

btSubmit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String userName = inputId.getText();

String userPassword = new String(inputPassword.getPassword());

String userPasswordConfirm = new String(inputPasswordConfirm

.getPassword());

System.out.println("您点击了提交按钮");

if (userName.equals("")) {

JOptionPane.showMessageDialog(null, "用户名不能为空");

} else if ("".equals(userPassword)

|| "".equals(userPasswordConfirm)) {

JOptionPane.showMessageDialog(null, "密码和密码重复都不能为空");

} else if (!userPassword.equals(userPasswordConfirm)) {

JOptionPane.showMessageDialog(null, "密码和密码重复不一致");

} else {

UserInformation user = new UserInformation();

if (user.isExist(userName)) {

JOptionPane.showMessageDialog(null, "此用户名已存在");

} else {

JOptionPane.showMessageDialog(null, "注册成功");

user.insert(userName, userPassword);

Regist.this.dispose();

}

}

}

});

// 注册《取消》按钮的监听器

btCancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println("您点击了取消按钮");

Regist.this.dispose();

}

});

this.add(userId);

this.add(userPassword);

this.add(userPasswordConfirm);

this.add(inputId);

this.add(inputPassword);

this.add(inputPasswordConfirm);

this.add(btSubmit);

this.add(btCancel);

this.setVisible(true);

}

public static void main(String[] args) {

new Regist();

}

}



更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明

Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值