首先明确我们的目标:创建一个员工管理系统的用户登录界面,要求有:实现用户的注册,登录,修改密码,注销等操作。
- 根据分析我们首先得写一个登录窗口:
分析步骤如下
- 首先实现一个窗口,调整位置大小,设置为可见
- 然后添加组件,图片等(合理运用好布局,流式,网格)
- 最后对按键组件添加监听事件
package day01;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class RegistFrame extends JFrame {
JTextField txtUserName;
JPasswordField txtPassword;
String id;
String pass;
//构造器
public RegistFrame(){
init();
}
public void init(){
//初始化窗口
this.setTitle("登录窗口");
this.setContentPane(createContentPane());
this.setLocation(800, 400);
this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.pack();
this.setVisible(true);
}
public JPanel createContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("登录界面",JLabel.CENTER);
panel.add(BorderLayout.NORTH,label);
panel.add(BorderLayout.CENTER,createCenterPane());
panel.add(BorderLayout.SOUTH,createBottomPane());
return panel;
}
public JPanel createCenterPane(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
Icon image = new ImageIcon("src/day01/sh.jpg");
JLabel label = new JLabel(image);
panel.add(BorderLayout.CENTER,label);
panel.add(BorderLayout.SOUTH,createIdPwdPane());
return panel;
}
public JPanel createIdPwdPane(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,5,5));
panel.add(new JLabel("用 户 名:",JLabel.CENTER));
txtUserName = new JTextField(15);
panel.add(txtUserName);
JButton b3 = new JButton("修改密码");
panel.add(b3);
panel.add(new JLabel("密 码:",JLabel.CENTER));
txtPassword = new JPasswordField(15);
panel.add(txtPassword);
JButton b4 = new JButton("注销");
panel.add(b4);
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new Siugaimima();
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new Zhuxiao();
}
});
return panel;
}
public JPanel createBottomPane(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton b1 = new JButton("登录");
JButton b2 = new JButton("注册");
panel.add(b1);
panel.add(b2);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
id = txtUserName.getText();
pass = txtPassword.getText();
System.out.println(id +" "+pass);
Service p = new Service();
if(!p.login(id, pass)){
JOptionPane.showMessageDialog(RegistFrame.this,"账号或密码错误");
clearText();
}else{
setVisible(false);
new Frame();
dispose();
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
id = txtUserName.getText();
pass = txtPassword.getText();
Service p = new Service();
if(p.regist(id, pass)){
JOptionPane.showMessageDialog(RegistFrame.this,"注册成功");
}else{
JOptionPane.showMessageDialog(RegistFrame.this,"注册失败");
}
}
});
return panel;
}
protected void clearText() {
txtPassword.setText("");
}
}
- 具体实现修改密码和注销用户的按钮监听
思路如下:
- 点击其按钮时弹出修改密码和注销界面(注意错误处理)
- 其中首先我们还需要连接数据库
- 最后实现修改密码(对数据库进行改操作)和注销界面(对数据库进行删操作)
//连接数据库
package day01;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LianjieSql {
public Connection lianjie(){
String url = "jdbc:mysql://127.0.0.1:3307/web";
String user = "kai";
String password = "kk";
//反射
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection conn;
try {
conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void close( Statement stat, Connection conn) throws SQLException{
if(stat!=null){
((Connection) stat).close();
}if(conn!=null){
conn.close();
}
}
}
//对修改密码进行监听事件的实现
package day01;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Siugaimima extends JFrame {
JTextField txtUserName;
JPasswordField txtPassword,txtPassword1;
String id,pass,oldpass;
//构造器
public Siugaimima() {
init();
}
public void init(){
//初始化窗口
this.setTitle("修改密码");
this.setContentPane(createContentPane());
this.setLocation(800, 400);
//this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public JPanel createContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("修改密码",JLabel.CENTER);
panel.add(BorderLayout.NORTH,label);
panel.add(BorderLayout.CENTER,createIdPwdPane());
panel.add(BorderLayout.SOUTH,createBottomPane());
return panel;
}
public JPanel createIdPwdPane(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2,5,5));
panel.add(new JLabel("用 户 名:"));
txtUserName = new JTextField(15);
panel.add(txtUserName);
panel.add(new JLabel("旧 密 码:"));
txtPassword = new JPasswordField(15);
panel.add(txtPassword);
panel.add(new JLabel("新 密 码:"));
txtPassword1 = new JPasswordField(15);
panel.add(txtPassword1);
return panel;
}
public JPanel createBottomPane(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton b1 = new JButton("修改");
JButton b2 = new JButton("取消");
panel.add(b1);
panel.add(b2);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
id = txtUserName.getText();
oldpass = txtPassword.getText();
pass = txtPassword1.getText();
System.out.println(id +" "+pass);
Service p = new Service();
if(!p.updatepwd(id,oldpass,pass)){
JOptionPane.showMessageDialog(Siugaimima.this,"用户名或密码错误");
clearText();
}else{
JOptionPane.showMessageDialog(Siugaimima.this,"修改成功");
setVisible(false);
dispose();
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
setVisible(false);
dispose();
}
});
return panel;
}
protected void clearText() {
// TODO Auto-generated method stub
txtPassword.setText("");
txtPassword1.setText("");
}
}
//对注销按键进行事件处理
package day01;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Zhuxiao extends JFrame {
JTextField txtUserName;
JPasswordField txtPassword;
String id;
String pass;
//构造器
public Zhuxiao(){
init();
}
public void init(){
//初始化窗口
this.setTitle("注销用户");
this.setContentPane(createContentPane());
this.setLocation(800, 400);
//this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public JPanel createContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("注销界面",JLabel.CENTER);
panel.add(BorderLayout.NORTH,label);
panel.add(BorderLayout.CENTER,createIdPwdPane());
panel.add(BorderLayout.SOUTH,createBottomPane());
return panel;
}
public JPanel createIdPwdPane(){
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,2,5,5));
panel.add(new JLabel("用 户 名:"));
txtUserName = new JTextField(15);
panel.add(txtUserName);
panel.add(new JLabel("密 码:"));
txtPassword = new JPasswordField(15);
panel.add(txtPassword);
return panel;
}
public JPanel createBottomPane(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton b3 = new JButton("注销");
JButton b4 = new JButton("取消");
panel.add(b3);
panel.add(b4);
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
id = txtUserName.getText();
pass = txtPassword.getText();
System.out.println(id +" "+pass);
Service p = new Service();
if(!p.delete(id, pass)){
JOptionPane.showMessageDialog(Zhuxiao.this,"账号或密码错误");
clearText();
}else{
JOptionPane.showMessageDialog(Zhuxiao.this,"注销成功");
setVisible(false);
dispose();
}
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
setVisible(false);
dispose();
}
});
return panel;
}
protected void clearText() {
txtPassword.setText("");
}
}
- 我们大体的框架搭建完成,接下来我们得实现具体的数据库操作
具体步骤:
- 从点击按钮事件中获取到文本框信息
- 将信息传入实现函数中
- 完成具体的数据库操作
//业务层,对按键监听的统一层
package day01;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Service {
//注册
public boolean regist(String id,String pwd){
Dao db = new Dao();
int n = db.insert(id, pwd);
if(n > 0){
return true;
}
return false;
}
//登录
public boolean login(String id,String pwd){
Dao db = new Dao();
ResultSet p = db.select(id, pwd);
try {
if(p.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return false;
//e.printStackTrace();
}
return false;
}
//修改密码
public boolean updatepwd(String id,String oldpass,String pwd){
Dao db = new Dao();
int n = db.updete(id,oldpass,pwd);
if(n > 0){
return true;
}
return false;
}
//注销用户
public boolean delete(String id,String pwd){
Dao db = new Dao();
int n = db.delete(id,pwd);
if(n > 0){
return true;
}
return false;
}
}
//数据层——对业务层的具体实现
package day01;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
//数据层——对数据库的操作
public class Dao {
LianjieSql db = new LianjieSql();
Connection conn = db.lianjie();
java.sql.Statement stat = null;
public int insert(String id,String pwd){
try {
String sql="insert into user values("+id+","+pwd+")";
stat = conn.createStatement();
int result = stat.executeUpdate(sql);
return result;
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println("注册失败!");
//e1.printStackTrace();
}
return 0;
}
public int updete(String id,String oldpass,String pwd){
try {
String sql="update user set pass="+pwd+" where id="+id+" and pass="+oldpass+"";
stat = conn.createStatement();
int result = stat.executeUpdate(sql);
return result;
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println("修改失败!");
//e1.printStackTrace();
}
return 0;
}
public int delete(String id,String pwd){
try {
String sql="delete from user where id="+id+" and pass="+pwd+"";
stat = conn.createStatement();
int result = stat.executeUpdate(sql);
return result;
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println("注销失败!");
//e1.printStackTrace();
}
return 0;
}
public ResultSet select(String id,String pwd){
try {
String sql="select * from user where id="+id+" and pass="+pwd+"";
stat = conn.createStatement();
ResultSet result = stat.executeQuery(sql);
return result;
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println("登录失败");
//e1.printStackTrace();
}
return null;
}
}
最后那就是我们的主函数调用了:
具体的效果图如下: