//注册页面
package com.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import com.DateSystem.Userdate;
import java.awt.event.*;
import java.sql.*;
public class Register extends JFrame implements ActionListener,KeyListener ,FocusListener{
//注册组件
JLabel lbl_name,lbl_password,lbl_repassword,lbl_sex,lbl_id;//用户名,密码标签
JLabel lbl_post,lbl_phon,lbl_email,lbl_address;//地址之类的标签
JLabel lbl_name_tishi,lbl_password_tishi,lbl_repassword_tishi,lbl_sex_tishi,lbl_id_tishi;//提示标签
JTextField text_name,text_id,text_post,text_phon,text_email,text_address;//输入文本框
JPasswordField password,repassword;//密码的输入文本框
CheckboxGroup group ;
Checkbox sex_m;
Checkbox sex_w;
//JRadioButton sex_m,sex_w;
//ButtonGroup bg = new ButtonGroup();//分组用来制作单选框
JButton button_submit,button_exit;//按钮
JPanel jPanel;//装载容器
public Register() {
super("用户注册");
//标签组建的初始化
//用户基本信息
lbl_name = new JLabel("用 户 名:");
lbl_password = new JLabel("密 码:");
lbl_repassword = new JLabel("确认密码:");
lbl_sex = new JLabel("性 别:");
lbl_id = new JLabel("身份证号:");
//详细信息
lbl_post = new JLabel("职 位:");
lbl_phon = new JLabel("手 机 号:");
lbl_email = new JLabel("E — mail:");
lbl_address = new JLabel("住 址:");
//提示信息
lbl_name_tishi = new JLabel("请输入您的真实姓名!");
lbl_password_tishi = new JLabel("请输入密码!");
lbl_repassword_tishi = new JLabel("两次输入不一样,请重新输入!");
lbl_sex_tishi=new JLabel("请选择性别!");
lbl_id_tishi=new JLabel("请填写您的身份证号!");
//输入框组件的初始化
//基本信息输入框
text_name = new JTextField();
password = new JPasswordField();
repassword = new JPasswordField();
//详细信息输入框
text_id = new JTextField();
text_post = new JTextField();
text_phon = new JTextField();
text_email = new JTextField();
text_address = new JTextField();
//性别组件
group = new CheckboxGroup();
sex_m = new Checkbox("男",group,true);
sex_w = new Checkbox("女",group,false);
//按钮组件的初始化
button_submit = new JButton("提交");
button_exit = new JButton("取消");
//设置字体,自号
Font font = new Font("Serif",Font.BOLD,18);
//向容器添加组件
jPanel = new JPanel();//创建容器
jPanel.setLayout(null);
//设置组件布局,我这里用的是坐标,因为这样界面会整齐些
lbl_name.setBounds(50, 50,90,20);
lbl_password.setBounds(50,80,90,20);
lbl_repassword.setBounds(50,110,90,20);
lbl_sex.setBounds(50,140,90,20);
lbl_id.setBounds(50,170,90, 20);
lbl_post.setBounds(50, 230, 90, 20);
lbl_phon.setBounds(50, 260, 90, 20);
lbl_email.setBounds(50, 290, 90, 20);
lbl_address.setBounds(50,320,90,20);
//输入框组件的添加
text_name.setBounds(150, 50,200,20);
password.setBounds(150,80,200,20);
repassword.setBounds(150,110,200,20);
sex_m.setBounds(170,140,40,20);//性别选择框
sex_w.setBounds(270, 140,40,20);
text_id.setBounds(150,170,200,20);
text_post.setBounds(150, 230, 200, 20);
text_phon.setBounds(150,260,200,20);
text_email.setBounds(150,290,200,20);
text_address.setBounds(150,320,200,20);
button_submit.setBounds(150,370,90,30);
button_exit.setBounds(300,370,90,30);
//提示组件添加
//lbl_name_tishi.setBounds(360, 50,200,20);
//lbl_password_tishi.setBounds(360,80,200,20);
//lbl_repassword_tishi.setBounds(360,110,200,20);
lbl_name.setFont(font);
lbl_password.setFont(font);
lbl_repassword.setFont(font);
lbl_sex.setFont(font);
lbl_id.setFont(font);
lbl_post.setFont(font);
lbl_phon.setFont(font);
lbl_address.setFont(font);
lbl_email.setFont(font);
jPanel.add(lbl_name);
jPanel.add(lbl_password);
jPanel.add(lbl_repassword);
jPanel.add(lbl_sex);
jPanel.add(lbl_id);
jPanel.add(lbl_post);
jPanel.add(lbl_phon);
jPanel.add(lbl_email);
jPanel.add(lbl_address);
jPanel.add(text_name);
jPanel.add(password);
jPanel.add(repassword);
/*ButtonGroup不需要加入JPanel里面
ButtonGroup用于为一组按钮创建一个多斥(multiple-exclusion)作用域。使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。
你只需要定义一个ButtonGroup,然后将button加入其中,至于加入的这些button在不在同一个JPanel里都是可以的。
ButtonGroup并不能算是一个装载的容器*/
jPanel.add(sex_m);
jPanel.add(sex_w);
jPanel.add(text_id);
jPanel.add(text_post);
jPanel.add(text_phon);
jPanel.add(text_email);
jPanel.add(text_address);
jPanel.add(button_submit);
jPanel.add(button_exit);
jPanel.add(lbl_name_tishi);
jPanel.add(lbl_password_tishi);
jPanel.add(lbl_repassword_tishi);
jPanel.add(lbl_sex_tishi);
jPanel.add(lbl_id_tishi);
add(jPanel);
//注册监听
//sex_m.addItemListener(this);
//sex_w.addItemListener(this);
button_exit.addActionListener(this);
button_submit.addActionListener(this);
//文本框焦点事件监听
text_name.addFocusListener(this);
password.addFocusListener(this);
repassword.addFocusListener(this);
text_id.addFocusListener(this);
}
public static void main(String[] args) {
Register register = new Register();
register.setSize(600, 470);
register.setLocationRelativeTo(null);
register.setVisible(true);
register.setResizable(false);
//register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
//文本框焦点事件处理
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
String nameString=text_name.getText().trim();
String pass=String.valueOf(password.getPassword());
String repass=String.valueOf(repassword.getPassword());
String idString=text_id.getText();
if (e.getSource()==text_name) {
if (nameString.equals("")) {
System.out.println("用户名为空");
lbl_name_tishi.setText("o(︶︿︶)o唉!用户名不能为空!");
lbl_name_tishi.setBounds(360, 50,200,20);
lbl_name_tishi.setForeground(Color.red);
}else {
lbl_name_tishi.setText("o(≧v≦)o~~好棒");
lbl_name_tishi.setBounds(360, 50,200,20);
lbl_name_tishi.setForeground(Color.green);
}
}
if (e.getSource()==password) {
if (pass.equals("")) {
System.out.println("密码为空");
lbl_password_tishi.setText("o(︶︿︶)o唉!密码不能为空!");
lbl_password_tishi.setBounds(360,80,200,20);
lbl_password_tishi.setForeground(Color.red);
}else {
lbl_password_tishi.setText("o(≧v≦)o~~好棒");
lbl_password_tishi.setBounds(360,80,200,20);
lbl_password_tishi.setForeground(Color.green);
}
}
if (e.getSource()==repassword) {
if (!repass.equals(pass)||repass.equals("")) {
System.out.println("o(︶︿︶)o唉!两次密码不一致,请重新输入");
lbl_repassword_tishi.setText("o(︶︿︶)o唉!两次不一样!");
lbl_repassword_tishi.setBounds(360,110,200,20);
lbl_repassword_tishi.setForeground(Color.red);
}else {
lbl_repassword_tishi.setText("o(≧v≦)o~~好棒");
lbl_repassword_tishi.setBounds(360,110,200,20);
lbl_repassword_tishi.setForeground(Color.green);
}
}
if (e.getSource()==text_id) {
if (idString.equals("")) {
System.out.println("o(︶︿︶)o唉!两次密码不一致,请重新输入");
lbl_id_tishi.setText("o(︶︿︶)o唉!身份证号不能为空!");
lbl_id_tishi.setBounds(360,170,200,20);
lbl_id_tishi.setForeground(Color.red);
}else {
lbl_id_tishi.setText("o(≧v≦)o~~好棒");
lbl_id_tishi.setBounds(360,170,200,20);
lbl_id_tishi.setForeground(Color.green);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btnstring = e.getActionCommand();
if (btnstring.equals("提交")) {
//System.out.println("提交");
String nameString=text_name.getText().trim();
String pass=String.valueOf(password.getPassword());
String repass=String.valueOf(repassword.getPassword());
String sexString=group.getSelectedCheckbox().getLabel();
String idString=text_id.getText();
String post=text_post.getText();
String phon=text_phon.getText();
String e_mail=text_email.getText();
String address=text_address.getText();
if (!nameString.equals("") && !pass.equals("") && pass.equals(repass) && !idString.equals("")) {
System.out.println("提交");
String sql=
"insert into userdata values("+
"'"+nameString+"',"+
"'"+pass+"',"+
"'"+sexString+"',"+
"'"+idString+"',"+
"'"+post+"',"+
"'"+phon+"',"+
"'"+e_mail+"',"+
"'"+address+"'"+
")";
System.out.println(sql);
Userdate userdate=new Userdate();
boolean flag = userdate.updataUser(sql);
if (flag) {
JOptionPane.showMessageDialog(this, "注册成功!");
this.dispose();
}
}
}else {
System.out.println("取消");
this.dispose();
}
}
}
java注册界面+mysql数据库
最新推荐文章于 2024-07-14 22:26:51 发布