package com.frm;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class RegFrm extends JFrame {
// RegFrm 注册的窗体 : register注册 Frm用来区别普通的类
//1.先准备好数据库, 导入jar (架构师)
//2.界面的搭建 v
//3.功能jdbc
private JPanel contentPane;
private JTextField textField;
private final JButton btnNewButton_2 = new JButton("\u5DF2\u6709\u8D26\u53F7\uFF0C\u70B9\u51FB\u767B\u5F55");
private JPasswordField passwordField;
private JPasswordField passwordField_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RegFrm frame = new RegFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RegFrm() {
setTitle("\u56FE\u4E66\u9986\u6CE8\u518C");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
//自动居中
setSize(500,400);
setLocationRelativeTo(null);//(居中)
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//layout
JLabel lblNewLabel = new JLabel("\u8D26\u53F7\uFF1A");
lblNewLabel.setBounds(72, 64, 54, 15);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(136, 61, 168, 18);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_1.setBounds(72, 99, 54, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
lblNewLabel_2.setBounds(47, 134, 68, 18);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("");
lblNewLabel_3.setForeground(Color.RED);
lblNewLabel_3.setBounds(136, 174, 121, 15);
contentPane.add(lblNewLabel_3);
JRadioButton rdbtnNewRadioButton = new JRadioButton("\u8D85\u7EA7\u7BA1\u7406\u5458");
rdbtnNewRadioButton.setSelected(true);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(72, 201, 121, 23);
contentPane.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("\u7BA1\u7406\u5458");
buttonGroup.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_1.setBounds(212, 201, 121, 23);
contentPane.add(rdbtnNewRadioButton_1);
//注册
JButton btnNewButton = new JButton("\u6CE8\u518C");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//点击事件
System.out.println("点击一次执行一次");
//获取 账号的值
String username= textField.getText();
System.out.println("用户输入的账号:"+username);
String pwd=passwordField.getText();
System.out.println("用户输入的密码:"+pwd);
String repwd=passwordField_1.getText();
System.out.println("用户输入的密码:"+repwd);
int root=1;//默认是超级管理员
if(rdbtnNewRadioButton.isSelected()) {
//选中了超级管理员的按钮
// System.out.println("true");
root=1;//超级管理员
}
if(rdbtnNewRadioButton_1.isSelected()) {
root=2;//管理员
}
//非空判断
if(username==null||username.equals("")) {
//弹窗 showmess 中间 intel
JOptionPane.showMessageDialog(null, "请填写账号");
return;//停止写在方法里面,代码不会继续往下执行
}
//首先查询出所有的游戏名。对比一下你输入有没有一模一样。有一模一样的就是重复
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/java0708book",
"root",
"root");
String sql="select username from glytb where username=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setObject(1, username);
pstmt.execute();
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "账号已被注册,请换一个");
return;
}
rs.close();pstmt.close();conn.close();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(pwd==null||pwd.equals("")) {
JOptionPane.showMessageDialog(null, "请填写密码");
return;
}
if(repwd==null||repwd.equals("")) {
//本来没字,点击为空 就设置 setText 提示信息
lblNewLabel_3.setText("请填写确认密码");
//1.让没填的框获得焦点 ------ 让鼠标的光标自动定位到要填写的位置
passwordField_1.requestFocus();//获得焦点
//2.提醒一下就消失,或者输入了就消失 键盘输入事件
}
if(!pwd.equals(repwd)) {
// System.out.println("密码和确认密码不一致,请重新输入");
JOptionPane.showMessageDialog(null, "密码和确认密码不一致");
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/java0708book",
"root",
"root");
String sql="insert glytb (username,pwd,root) value(?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setObject(1, username);
pstmt.setObject(2, pwd);
pstmt.setObject(3, root);
int row=pstmt.executeUpdate();
if(row>0) {
JOptionPane.showMessageDialog(null, "注册成功");
//跳转的页面frm
LoginFrm lf=new LoginFrm();
lf.setVisible(true);
setVisible(false);
}
pstmt.close();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(72, 242, 93, 23);
contentPane.add(btnNewButton);
//重复按钮
JButton btnNewButton_1 = new JButton("\u91CD\u590D");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
passwordField.setText("");
passwordField_1.setText("");
//设置超级管理员选中
// pass alt+/
rdbtnNewRadioButton.setSelected(true);
}
});
btnNewButton_1.setBounds(200, 242, 93, 23);
contentPane.add(btnNewButton_1);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrm lf=new LoginFrm();
lf.setVisible(true);//登录显示
setVisible(false);//自己隐藏
}
});
btnNewButton_2.setBounds(93, 292, 185, 18);
contentPane.add(btnNewButton_2);
//
passwordField = new JPasswordField();
passwordField.setBounds(136, 96, 168, 21);
contentPane.add(passwordField);
passwordField_1 = new JPasswordField();
passwordField_1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// System.out.println("pressed就是点击键盘,点击一次执行一次");
// String repwd=passwordField_1.getText();
// System.out.println("你输入的确认密码:"+repwd);
lblNewLabel_1.setText("");
}
});
passwordField_1.setBounds(136, 133, 168, 21);
contentPane.add(passwordField_1);
}
}