📄 add_user.java
字号:
package user;
import javax.swing.*;
import java.sql.*;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.JFrame;
public class add_user extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JPanel cp;
ImageIcon icon=new ImageIcon("picture/29.jpg");
JLabel tp=new JLabel(icon,JLabel.CENTER);
JLabel j1=new JLabel("账户名 : ");
JLabel j3=new JLabel("密码 :");
JLabel j4=new JLabel("确认密码:");
JButton b1=new JButton("注 册");
JButton b2=new JButton("取 消");
TextField t1=new TextField(20);
TextField t3=new TextField(16);
TextField t4=new TextField(16);
public add_user()
{try{
Init();
}catch(Exception err){err.printStackTrace();}}
private void Init() throws Exception{
cp=(JPanel) this.getContentPane();
cp.setLayout(null);
this.setTitle("用户注册");
this.setSize(380,360);
j1.setBounds(60,40,100,30);
j1.setFont(new java.awt.Font("Dialog",2,20));
j1.setForeground(new Color(250,0,0));
j3.setBounds(60,90,100,30);
j3.setFont(new java.awt.Font("Dialog",2,20));
j3.setForeground(new Color(250,0,0));
j4.setBounds(60,140,100,30);
j4.setFont(new java.awt.Font("Dialog",2,20));
j4.setForeground(new Color(250,0,0));
t1.setBounds(180,40,120,30);
t1.setFont(new java.awt.Font("Dialog",0,15));
t1.addActionListener(this);
t3.setBounds(180,90,120,30);
t3.setFont(new java.awt.Font("Dialog",0,14));
t3.addActionListener(this);
t4.setBounds(180,140,120,30);
t4.setFont(new java.awt.Font("Dialog",0,14));
t4.addActionListener(this);
b1.setBounds(60,240,90,30);
b1.setFont(new java.awt.Font("Dialog",0,15));
b1.addActionListener(this);
b2.setBounds(210,240,90,30);
b2.setFont(new java.awt.Font("Dialog",0,15));
b2.addActionListener(this);
tp.setText("");
tp.setSize(380,360);
cp.add(j1,null);
cp.add(j3,null);
cp.add(j4,null);
cp.add(t1,null);
cp.add(t3,null);
cp.add(t4,null);
cp.add(b1,null);
cp.add(b2,null);
cp.add(tp,null);
t3.setEchoChar('*');
t4.setEchoChar('*');
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b2){this.dispose();}
else if(t1.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入用户名!");t1.requestFocus();t3.setText("");}
else if(e.getSource()==t1){t3.requestFocus();}
else if(t3.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入密码!");t3.requestFocus();}
else if(e.getSource()==t3){t4.requestFocus();}
else if(t4.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入确认密码!");t4.requestFocus();}
else if(e.getSource()==t4||e.getSource()==b1)
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs=ps.executeQuery("select * from 用户管理 where 用户名='"+t1.getText().trim()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"此用户已存在,请重新输入!");
t1.setText("");t3.setText("");t4.setText("");t1.requestFocus();
}
else
{
if(t3.getText().equals(t4.getText()))
{
if(t3.getText().length()>=6&&t3.getText().length()<=16)
{
int v=JOptionPane.showConfirmDialog(this, "确认注册吗?","注册确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION)
{
ps.executeUpdate("insert into 用户管理 values('"+t1.getText().trim()+"','"+t3.getText().trim()+"','普通用户','未锁定')");
JOptionPane.showMessageDialog(null, "注册成功!");this.dispose();
}
else{this.dispose();}
}
else{JOptionPane.showMessageDialog(null, "密码必须是由6~16位之间的字符或数字组成!");t3.setText("");t4.setText("");t3.requestFocus();}
}
else{JOptionPane.showMessageDialog(null,"两次密码输入不一致,请重新输入!");t3.setText("");t4.setText("");t3.requestFocus();}
}
}
catch (Exception err) { err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接出错!"); }
}
public static void main(String args[])
{new add_user(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -