📄 regist.java
字号:
package cn.com.studentsystem.login;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import cn.com.util.DBConnection;
public class Regist {
static JTextField user_field;//用于存放用户名的文本框
static JPasswordField password_field;//用于存放密码的密码框
static JPasswordField instruction_field;//用于存放密码提示的密码框
JOptionPane jop = new JOptionPane();//消息提示框
JFrame jf;//创建注册框
public Regist(){
init();
}
/**
* 初始化注册类
*
*/
public void init(){
jf = new JFrame("注册记录");//创建一个注册框架
jf.setSize(300, 350);
jf.setLocationRelativeTo(null);
ImageIcon i = new ImageIcon("image//title.png");
jf.setIconImage(i.getImage());//设置注册框架的标题图标
FlowLayout flow = new FlowLayout(FlowLayout.CENTER,55,55);
RegistJPanel rj = new RegistJPanel();
rj.setLayout(flow);
JLabel user_name = new JLabel("用 户 名");//用户名标签
JLabel user_password = new JLabel("密 码");//密码标签
JLabel user_passinstruction = new JLabel("密码提示");//密码提示标签
/**
* 设置标签前景颜色
*/
user_name.setForeground(Color.YELLOW);
user_password.setForeground(Color.YELLOW);
user_passinstruction.setForeground(Color.YELLOW);
user_field = new JTextField(10);
password_field = new JPasswordField(10);
instruction_field = new JPasswordField(10);
JButton button_regist = new JButton("注册");//注册按钮
JButton button_cancel = new JButton("取消");//取消按钮
/**
* 设置按钮前景颜色
*/
button_regist.setForeground(Color.YELLOW);
button_cancel.setForeground(Color.YELLOW);
/**
* 将各个组件添加到带图面板
*/
rj.add(user_name);
rj.add(user_field);
rj.add(user_password);
rj.add(password_field);
rj.add(user_passinstruction);
rj.add(instruction_field);
rj.add(button_regist);
rj.add(button_cancel);
jf.add(rj);//将带图面板添加到注册框
/**
* 一个实现注册框功能的事件的内部类
* @author Administrator
*
*/
class RegistAction implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getActionCommand().equals("注册")){
Connection con = DBConnection.getConnectionOracle();
int rows = 0 ;//行数
// int presentrow = 1;//当前行
PreparedStatement ps;
ResultSet rs;
PreparedStatement ps1;
ResultSet rs1 ;
try {
ps = con.prepareStatement("select count(username) from regist");
rs = ps.executeQuery();
while(rs.next()){
rows = rs.getInt(1);
}
ps1 = con.prepareStatement("select username from regist");
rs1 = ps1.executeQuery();
List l = new List();
while(rs1.next()){
l.add((String)rs1.getObject(1 ));
}
String[] name_all = l.getItems();
for(int i=0;i<rows;i++){
if(user_field.getText().equals(name_all[i])){
jop.showMessageDialog(null, "该用户名已存在");
}else{
ps = con.prepareStatement("insert into regist values(?,?,?)");
ps.setString(1,user_field.getText());
ps.setString(2,password_field.getText());
ps.setString(3, instruction_field.getText());
ps.executeUpdate();
jop.showMessageDialog(null, "注册成功,欢迎进入中信");
jf.dispose();
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
// System.out.println("有点小问题");
}
}else if(arg0.getActionCommand().equals("取消")){
jf.dispose();
}
}
}
RegistAction regist_action = new RegistAction();//创建事件类的对象
button_regist.addActionListener(regist_action);
button_cancel.addActionListener(regist_action);
user_field.addActionListener(regist_action);
password_field.addActionListener(regist_action);
instruction_field.addActionListener(regist_action);
// LoginAction la = new LoginAction(this.user_field,this.password_field,this.instruction_field);
// user_field.addActionListener(la);
// password_field.addActionListener(la);
// instruction_field.addActionListener(la);
// button_regist.addActionListener(la);
// button_cancel.addActionListener(la);
jf.setResizable(false);
jf.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -