JAVA:一个用户登陆的程序语言翻译 我初学的哪位大哥帮个忙
//显示出图形界面,Login.javapublicLogin(Stringmsg){super(msg);setLayout(newBorderLayout());lo...
//显示出图形界面,Login.java
public Login(String msg) { super(msg);
setLayout(new BorderLayout());
login_message = new JTextField("用户登陆");
login_message.setEditable(false);
login = new JButton("登陆");
regeist = new JButton("注册");
setVisible(true);
add(login_message,"North");
JPanel btn = new JPanel();
btn.setLayout(new GridLayout(1,2));
btn.add(login);
btn.add(regeist);
add(btn,"South");
JPanel input = new JPanel();
JLabel unameLabel = new JLabel("用户名:");
uname = new JTextField("",20);
JLabel pwdLabel = new JLabel("密 码 :");
pwd = new JPasswordField("",20);
input.setLayout(new GridLayout(2,2));
input.add(unameLabel);
input.add(uname);
input.add(pwdLabel);
input.add(pwd);
add(input,"Center");
login.addActionListener(new loginListener());
regeist.addActionListener(new regeistListener());
}
class loginListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
UserDao ud = new UserDao();
if(ud.login(uname.getText().trim(), pwd.getText().trim())) {
new MainGui().start();
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "您的输入不正确,请重输","警告", 1);
}
}
loginListener() {
}
}
//数据库中检查是使用UserDao.java
public boolean login(String username,String password) {
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "select * from user where username='" + username +"'";
ResultSet rs = DB.getResultSet(stmt, sql);
try {
while(rs.next()) {
String dbpassword = rs.getString("password");
if(dbpassword != null && dbpassword.equals(password))
return true;
}
} catch (SQLException e) {
System.out.println("用户登陆连接数据库异常,系统退出");
System.exit(-1);
}
return false;
} 展开
public Login(String msg) { super(msg);
setLayout(new BorderLayout());
login_message = new JTextField("用户登陆");
login_message.setEditable(false);
login = new JButton("登陆");
regeist = new JButton("注册");
setVisible(true);
add(login_message,"North");
JPanel btn = new JPanel();
btn.setLayout(new GridLayout(1,2));
btn.add(login);
btn.add(regeist);
add(btn,"South");
JPanel input = new JPanel();
JLabel unameLabel = new JLabel("用户名:");
uname = new JTextField("",20);
JLabel pwdLabel = new JLabel("密 码 :");
pwd = new JPasswordField("",20);
input.setLayout(new GridLayout(2,2));
input.add(unameLabel);
input.add(uname);
input.add(pwdLabel);
input.add(pwd);
add(input,"Center");
login.addActionListener(new loginListener());
regeist.addActionListener(new regeistListener());
}
class loginListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
UserDao ud = new UserDao();
if(ud.login(uname.getText().trim(), pwd.getText().trim())) {
new MainGui().start();
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "您的输入不正确,请重输","警告", 1);
}
}
loginListener() {
}
}
//数据库中检查是使用UserDao.java
public boolean login(String username,String password) {
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "select * from user where username='" + username +"'";
ResultSet rs = DB.getResultSet(stmt, sql);
try {
while(rs.next()) {
String dbpassword = rs.getString("password");
if(dbpassword != null && dbpassword.equals(password))
return true;
}
} catch (SQLException e) {
System.out.println("用户登陆连接数据库异常,系统退出");
System.exit(-1);
}
return false;
} 展开
展开全部
public Login(String msg) { super(msg);/*说明构造方法,当为super()时,表示构造方法为public Login() */
setLayout(new BorderLayout());//设置布局方式为BorderLayou,既东南西北4个的方式,还有其他的方式,如GridLayout,详细解释可正蠢禅以看API
login_message = new JTextField("用户登陆"); //初始化文本域login_messag,默认值为"用户登陆"
login_message.setEditable(false);//设置为不可编辑,既不可以输入
login = new JButton("登陆");//初始化按钮login
regeist = new JButton("注册");//初始化按钮regeist
setVisible(true);//设置这个面板为可见
add(login_message,"North");//将login_message防止在面板的北面
JPanel btn = new JPanel();//新建一个面板
btn.setLayout(new GridLayout(1,2));//布局设置为1行2列
btn.add(login);//加入按钮LOGIN
btn.add(regeist);//加入按钮regeist
add(btn,"South");//姜面板BTN加入到大面板的南面
JPanel input = new JPanel();//初始化面板INPUT
JLabel unameLabel = new JLabel("用户名:");////初始化标签unameLAble
uname = new JTextField("",20);//初始化文本域UNAME,可以输入20个字符
JLabel pwdLabel = new JLabel("密 码 :");//初始化PWDLABEL标签,显示密码二字
pwd = new JPasswordField("",20);//同上
input.setLayout(new GridLayout(2,2));//同上
input.add(unameLabel);//在面板inut加入控件
input.add(uname);
input.add(pwdLabel);
input.add(pwd);
add(input,"Center");
login.addActionListener(new loginListener());//为登录按钮添档差加事件监听
regeist.addActionListener(new regeistListener());//为注册按钮添加事件监听
}
class loginListener implements ActionListener {//实现时间监听
public void actionPerformed(ActionEvent event) {
UserDao ud = new UserDao();//新建一个USERDAO对象
if(ud.login(uname.getText().trim(), pwd.getText().trim())) {//获取名字和密码处举尘的值,去除空格(调用的是login方法)
new MainGui().start();
setVisible(false);//设置可见
} else {
JOptionPane.showMessageDialog(null, "您的输入不正确,请重输","警告", 1);//不满足条件,既没有这个用户,弹出对话框"您的输入不正确,请重输","警告",
}
}
loginListener() {
}
}
//数据库中检查是使用UserDao.java
public boolean login(String username,String password) {
Connection conn = DB.getConn();//新建连接
Statement stmt = DB.getStatement(conn);
String sql = "select * from user where username='" + username +"'";//根据USERNAME查询出此用户的信息
ResultSet rs = DB.getResultSet(stmt, sql);//新建ResultSet 对象
try {
while(rs.next()) {//循环查询
String dbpassword = rs.getString("password");/将PASSWORD字段的值放在dbpassword里
if(dbpassword != null && dbpassword.equals(password)) //判断传入的password值和dbpassword是否相等
return true;
}
} catch (SQLException e) {//不满足就抛出异常
System.out.println("用户登陆连接数据库异常,系统退出");
System.exit(-1);
}
return false;
}
看在我打了这么多字的份上,就给我分了吧。。
祝你成功!!
setLayout(new BorderLayout());//设置布局方式为BorderLayou,既东南西北4个的方式,还有其他的方式,如GridLayout,详细解释可正蠢禅以看API
login_message = new JTextField("用户登陆"); //初始化文本域login_messag,默认值为"用户登陆"
login_message.setEditable(false);//设置为不可编辑,既不可以输入
login = new JButton("登陆");//初始化按钮login
regeist = new JButton("注册");//初始化按钮regeist
setVisible(true);//设置这个面板为可见
add(login_message,"North");//将login_message防止在面板的北面
JPanel btn = new JPanel();//新建一个面板
btn.setLayout(new GridLayout(1,2));//布局设置为1行2列
btn.add(login);//加入按钮LOGIN
btn.add(regeist);//加入按钮regeist
add(btn,"South");//姜面板BTN加入到大面板的南面
JPanel input = new JPanel();//初始化面板INPUT
JLabel unameLabel = new JLabel("用户名:");////初始化标签unameLAble
uname = new JTextField("",20);//初始化文本域UNAME,可以输入20个字符
JLabel pwdLabel = new JLabel("密 码 :");//初始化PWDLABEL标签,显示密码二字
pwd = new JPasswordField("",20);//同上
input.setLayout(new GridLayout(2,2));//同上
input.add(unameLabel);//在面板inut加入控件
input.add(uname);
input.add(pwdLabel);
input.add(pwd);
add(input,"Center");
login.addActionListener(new loginListener());//为登录按钮添档差加事件监听
regeist.addActionListener(new regeistListener());//为注册按钮添加事件监听
}
class loginListener implements ActionListener {//实现时间监听
public void actionPerformed(ActionEvent event) {
UserDao ud = new UserDao();//新建一个USERDAO对象
if(ud.login(uname.getText().trim(), pwd.getText().trim())) {//获取名字和密码处举尘的值,去除空格(调用的是login方法)
new MainGui().start();
setVisible(false);//设置可见
} else {
JOptionPane.showMessageDialog(null, "您的输入不正确,请重输","警告", 1);//不满足条件,既没有这个用户,弹出对话框"您的输入不正确,请重输","警告",
}
}
loginListener() {
}
}
//数据库中检查是使用UserDao.java
public boolean login(String username,String password) {
Connection conn = DB.getConn();//新建连接
Statement stmt = DB.getStatement(conn);
String sql = "select * from user where username='" + username +"'";//根据USERNAME查询出此用户的信息
ResultSet rs = DB.getResultSet(stmt, sql);//新建ResultSet 对象
try {
while(rs.next()) {//循环查询
String dbpassword = rs.getString("password");/将PASSWORD字段的值放在dbpassword里
if(dbpassword != null && dbpassword.equals(password)) //判断传入的password值和dbpassword是否相等
return true;
}
} catch (SQLException e) {//不满足就抛出异常
System.out.println("用户登陆连接数据库异常,系统退出");
System.exit(-1);
}
return false;
}
看在我打了这么多字的份上,就给我分了吧。。
祝你成功!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询