📄 login3test.java
字号:
package com.hs ;
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.* ;
import java.sql.* ;
public class LogIn3Test extends JFrame implements ActionListener {
JTextField t_username = new JTextField() ;
JPasswordField t_password = new JPasswordField() ;
LogIn3Test() {
this.setTitle("新用户注册") ;
this.setSize(250 , 300) ;
this.setLocation(720 , 194) ;
JLabel l_username = new JLabel(" 用户名 ") ;
l_username.setForeground(Color.red) ;
JLabel l_password = new JLabel(" 登陆密码 ") ;
l_password.setForeground(Color.red) ;
JLabel l_age = new JLabel(" 年龄 ") ;
JLabel l_gender = new JLabel(" 性别 ") ;
JLabel l_birthday = new JLabel(" 出生年月 ") ;
JLabel l_address = new JLabel(" 家庭地址 ") ;
JTextField t_age = new JTextField() ;
JTextField t_gender = new JTextField() ;
JTextField t_birthday = new JTextField() ;
JTextField t_address = new JTextField() ;
JButton b_ok = new JButton(" 申请 ") ;
JButton b_unok = new JButton(" 取消 ") ;
//注册按钮监听
b_ok.addActionListener(this) ;
b_unok.addActionListener(this) ;
JPanel p_button = new JPanel(new FlowLayout()) ;
p_button.add(b_ok) ;
p_button.add(b_unok) ;
JPanel p_label = new JPanel(new GridLayout(6 , 1 , 5 , 5)) ;
p_label.add(l_username) ;
p_label.add(l_password) ;
p_label.add(l_age) ;
p_label.add(l_gender) ;
p_label.add(l_birthday) ;
p_label.add(l_address) ;
JPanel p_field = new JPanel(new GridLayout(6 , 1 , 5 , 5)) ;
p_field.add(t_username) ;
p_field.add(t_password) ;
p_field.add(t_age) ;
p_field.add(t_gender) ;
p_field.add(t_birthday) ;
p_field.add(t_address) ;
JPanel p_landf = new JPanel(new BorderLayout()) ;
p_landf.add(p_label , BorderLayout.WEST) ;
p_landf.add(p_field , BorderLayout.CENTER) ;
this.add(p_landf , BorderLayout.CENTER) ;
this.add(p_button , BorderLayout.SOUTH) ;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(" 申请 ")) {
if(t_username.getText().length()>=3 &&t_username.getText().length()<= 10
&&t_password.getText().length()>=3&&t_password.getText().length() <= 10) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") ;
Connection cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=QQ" , "sa" , "1234") ;
PreparedStatement ps = cn.prepareStatement("insert into Users values(?,?)") ;
ps.setString(1 , t_username.getText()) ;
ps.setString(2 , t_password.getText()) ;
ps.executeUpdate() ;
System.out.println ("注册信息写入数据库成功") ;
JOptionPane.showMessageDialog(null,
"您的用户名为: "+t_username.getText()+"\n"+"密码为: "+t_password.getText()+" 请注意保管",
"恭喜您,申请成功", JOptionPane.CLOSED_OPTION);
t_password.setText("") ;
}
catch (Exception ex) {
}
}else {
JOptionPane.showMessageDialog(null , "滚蛋,没一点诚意") ;
}
}
}
/*public static void main(String[] args) {
LogIn3Test l = new LogIn3Test() ;
l.setVisible(true) ;
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -