package test;
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 java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class Enrolled extends JFrame {//注册账号
public static String name1;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Enrolled frame1 = new Enrolled();
frame1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Enrolled() {
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\JAVA\\My\\\u56FE\u6807\\ConstructionBank.jpg"));
setTitle("\u6B22\u8FCE\u4F7F\u7528\u94F6\u884CATM\u673A");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 625, 390);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("\u8D26\u53F7\uFF1A");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel_1.setBounds(109, 83, 78, 44);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel_2.setBounds(109, 178, 78, 44);
contentPane.add(lblNewLabel_2);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 20));
textField.setColumns(10);
textField.setBounds(219, 83, 253, 44);
contentPane.add(textField);
passwordField = new JPasswordField();
passwordField.setFont(new Font("宋体", Font.PLAIN, 20));
passwordField.setEchoChar('*');
passwordField.setBounds(219, 181, 253, 44);
contentPane.add(passwordField);
JButton btnNewButton = new JButton("\u53D6\u6D88");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
passwordField.setText(null);
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton.setBounds(109, 274, 106, 53);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u786E\u8BA4");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
DBHelper dbHelper=new DBHelper();
String sql2 = "select * from atm where binary name ="+"'"+textField.getText()+"'";
ResultSet rsResultSet = dbHelper.DB(sql2);
if(rsResultSet.next()==true){//判断该用户名是否存在
JOptionPane.showMessageDialog(contentPane, "该用户名已存在!", "提示:",JOptionPane.WARNING_MESSAGE);
dbHelper.downConn();
}
else {
if(textField.getText().matches("[a-zA-Z\\w\\p{Punct}]{5,17}$")
&& passwordField.getText().matches("[a-zA-Z\\w\\p{Punct}]{5,17}$")) {//限制账户和密码的长度
String sql = "insert into atm (balance,name,password) values(?,?,?)";
PreparedStatement ps = dbHelper.connection.prepareStatement(sql);
ps.setString(1, "0");
ps.setString(2, textField.getText());//向数据库中添加数据
ps.setString(3, passwordField.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(contentPane, "注册成功!\n请登录!", "提示:",JOptionPane.WARNING_MESSAGE);
// System.exit(0);
dispose ();//关闭当前窗口
}
else {
JOptionPane.showMessageDialog(contentPane, "账户名和密码有误!", "错误提示:",JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(contentPane, "账户名和密码由5-17位\n(字母、数字或符号)组成!", "错误提示:",JOptionPane.WARNING_MESSAGE);
}
}
}
catch (Exception e1) {
}
}
});
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton_1.setBounds(413, 274, 106, 53);
contentPane.add(btnNewButton_1);
}
}
Java Swing 注册界面
最新推荐文章于 2024-04-26 18:06:40 发布