📄 registerframe.java
字号:
//用户注册窗口
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.*;
public class RegisterFrame extends JFrame implements ActionListener{
static final long serialVersionUID=80;
JLabel userLabel,passwordLabel,confirmLabel,messageLabel;
JTextField userText;
JPasswordField passwordText,confirmText;
JButton confirmButton,cancelButton;
Container c=getContentPane();
Statement sql;
Dimension screenSize;
Toolkit tool;
Image myimage;
RegisterFrame(String s,Statement sql){
super(s);
setSize(220,210);
tool=getToolkit();
URL url = getClass().getResource("/images/USER.GIF");
if(url!=null){
myimage=tool.getImage(url);
setIconImage(myimage);
}
this.sql=sql;
userLabel=new JLabel("用户名");
passwordLabel=new JLabel("密码");
confirmLabel=new JLabel("确认密码");
messageLabel=new JLabel("用户名长度>4<20,密码长度>8<20");
userText=new JTextField(18);
passwordText=new JPasswordField(18);
confirmText=new JPasswordField(18);
confirmButton=new JButton("确定");
cancelButton=new JButton("取消");
confirmButton.addActionListener(this);
cancelButton.addActionListener(this);
c.setLayout(null);
c.setBackground(Color.ORANGE);
c.add(userLabel);
userLabel.setBounds(10,10,40,30);
c.add(userText);
userText.setBounds(50,14,160,22);
c.add(passwordLabel);
passwordLabel.setBounds(20,40,30,30);
c.add(passwordText);
passwordText.setBounds(50,44,160,22);
c.add(confirmLabel);
confirmLabel.setBounds(0,70,50,30);
c.add(confirmText);
confirmText.setBounds(50,74,160,22);
c.add(confirmButton);
confirmButton.setBounds(30,110,60,30);
c.add(cancelButton);
cancelButton.setBounds(130,110,60,30);
c.add(messageLabel);
messageLabel.setBounds(10,150,220,30);
Dimension frameSize=getSize();
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==cancelButton){
dispose();
}
else{
String user=userText.getText().trim();
String password=new String(passwordText.getPassword());
String confirm=new String(confirmText.getPassword());
if(!password.equals(confirm)){
JOptionPane.showMessageDialog(this,"请确定一定以及肯定两次输入的密码一样!","警告对话框",JOptionPane.WARNING_MESSAGE);
return;
}
if(user.length()>=4&user.length()<=20&password.length()>=8&password.length()<=20&confirm.length()>=8&confirm.length()<=20){
try{
user="'"+user+"'";
password="'"+password+"'";
String temp="INSERT INTO User_Info VALUES ("+user+","+password+","+"'Common')";
int n=sql.executeUpdate(temp);
if(n==1){
JOptionPane.showMessageDialog(this,"注册成功","消息",JOptionPane.INFORMATION_MESSAGE);
dispose();
}
}
catch(SQLException e2){
JOptionPane.showMessageDialog(this,"用户名已存在");
}
}
else{
JOptionPane.showMessageDialog(this,"请输入正确长度的用户名与密码","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -