📄 enroljdialog.java
字号:
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
/*
* EnrolJDialog.java
*
* Created on 2008年1月2日, 上午1:27
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Wangyiming
*/
public class EnrolJDialog extends JDialog{
/** Creates a new instance of EnrolJDialog */
public EnrolJDialog(JFrame owner) {
super(owner,"注册用户",true);
this.setLayout(null);
//初始化各个控件
jLabelUserName=new JLabel("用户名:");
jLabelPassword=new JLabel("密码:");
jtextUserName=new JTextField("");
jpfPassword=new JPasswordField("");
jButtonOk=new JButton("确定");
jButtonCancel=new JButton("取消");
//设置控件位置
jLabelUserName.setBounds(10,20,80,20);
jLabelPassword.setBounds(10,50,80,20);
jtextUserName.setBounds(100,20,150,20);
jpfPassword.setBounds(100,50,150,20);
jButtonOk.setBounds(80,90,60,20);
jButtonCancel.setBounds(160,90,60,20);
//为按钮添加监听器
jButtonOk.addActionListener(new OkAction());
jButtonOk.registerKeyboardAction(new OkAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), JComponent.WHEN_IN_FOCUSED_WINDOW);
jButtonCancel.addActionListener(new CancelAction());
jButtonCancel.registerKeyboardAction(new CancelAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), JComponent.WHEN_IN_FOCUSED_WINDOW);
//将控件添加到容器中
this.add(jLabelUserName);
this.add(jLabelPassword);
this.add(jtextUserName);
this.add(jpfPassword);
this.add(jButtonOk);
this.add(jButtonCancel);
//设置位置,大小,标题
this.setBounds(280,320,300,150);
this.setTitle("注册用户");
this.setResizable(false);
}
private JLabel jLabelUserName;
private JLabel jLabelPassword;
private JTextField jtextUserName;
private JPasswordField jpfPassword;
private JButton jButtonOk,jButtonCancel;
private class OkAction implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String name=jtextUserName.getText().trim();
String pwd=new String(jpfPassword.getPassword()).trim();
if(name.length()<1)
{
JOptionPane.showConfirmDialog(null,"请输入用户名!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
return;
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
return;
}
try {
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\Data.mdb;pwd=shujuyuan";
Connection con=DriverManager.getConnection(url);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select name from users");
while(rs.next())
if(name.trim().equalsIgnoreCase(new String(rs.getString("name"))))
{
JOptionPane.showConfirmDialog(null,"此用户已注册!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
return;
}
PreparedStatement ps=con.prepareStatement("insert into users values(?,?)");
ps.setString(1,name);
ps.setString(2,pwd);
ps.executeUpdate();
JOptionPane.showConfirmDialog(null,"注册新用户成功!","Congratulations",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
jtextUserName.setText("");
jpfPassword.setText("");
setVisible(false);
con.close();
st.close();
// rs.close();
} catch(Exception ex) {
JOptionPane.showConfirmDialog(null,"用户数据库文件不存在!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
return;
}
}//actionperformed
}
private class CancelAction implements ActionListener
{
public void actionPerformed(ActionEvent e) {
EnrolJDialog.this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -