一、工具准备
1.Java开发集成工具 : mycilpse (可以采用更先进的idea intellij)
2.包:jdbc包
3.数据库:sql server
二、知识需求
1.java swing (也可采用SWT designer进行辅助界面设计 )
所用组件:
JButton:登陆
JLabel:使用JLabel(new ImgeIcon(url)),进行背景设置,最好使用相对地址 "/" 将图片放在程序文件之中,也可使用本地地址:“\"
JTextField:文本框,进行账号和密码输入
JComboBox:下拉列表框,进行登陆角色选择
更多可以参考这个博主文章或者官方文档,写得很全。[Swing 学习](https://blog.csdn.net/xietansheng/article/details/72814492)
2.sql (连数据库请见 https://blog.csdn.net/qq_44654974/article/details/106676570)
登陆注册:只需要进行用户检测和添加
select * from usertable
Insert usertable values('Username','Password')
三、代码
登陆界面:
package View;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame {
public static String usern;
public static void main(String args[]) {
new Login();
}
Login() {
Content();
this.setTitle("企业员工健康系统");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(600, 300);
this.setSize(800, 560);
this.setVisible(true);
}
void Content() {
// 背景设置
JLabel bg = new JLabel(new ImageIcon("Images/LoginIcon/LoginBg.png"));
this.setContentPane(bg);
// 登陆板块内容
JLabel logo = new JLabel("你的健康,我来负责");
logo.setBounds(20, 20, 180, 25);
logo.setFont(new Font(null, Font.BOLD, 16));// 字体设置
logo.setForeground(Color.orange);
this.add(logo);
JLabel label = new JLabel("欢迎登陆");
label.setBounds(380, 180, 120, 25);
label.setFont(new Font(null, Font.BOLD, 18));// 字体设置
this.add(label);
JLabel User = new JLabel("账号");
User.setBounds(320, 222, 30, 25);
User.setFont(new Font(null, Font.PLAIN, 14));// 字体设置
this.add(User);
JLabel User1 = new JLabel(new ImageIcon("Images/LoginIcon/User.png"));
User1.setBounds(350, 220, 30, 30);
this.add(User1);
JLabel Password = new JLabel("密码");
Password.setBounds(320, 254, 30, 25);
Password.setFont(new Font(null, Font.PLAIN, 14));// 字体设置
this.add(Password);
JLabel Password1 = new JLabel(new ImageIcon("Images/LoginIcon/Lock.png"));
Password1.setBounds(352, 250, 30, 30);
this.add(Password1);
JLabel Type = new JLabel("用户");
Type.setBounds(320, 286, 30, 25);
Type.setFont(new Font(null, Font.PLAIN, 14));// 字体设置
this.add(Type);
JLabel Type1 = new JLabel(new ImageIcon("Images/LoginIcon/Type.png"));
Type1.setBounds(350, 280, 30, 30);
this.add(Type1);
// 文本框和按钮
JButton btn = new JButton("登陆");
btn.setBounds(320, 320, 70, 25);
this.add(btn);
JButton btn1 = new JButton("注册");
btn1.setBounds(430, 320, 70, 25);
this.add(btn1);
JTextField User2 = new JTextField();
User2.setBounds(380, 222, 120, 25);
this.add(User2);
JPasswordField Password2 = new JPasswordField();
Password2.setBounds(380, 254, 120, 25);
this.add(Password2);
String[] listData = new String[] {
"员工", "企业管理者", "系统管理员" };
JComboBox<String> type1 = new JComboBox<String>(listData);
type1.setBounds(380, 284, 120, 25);
this.add(type1);
JPanel panel = new JPanel(null);
panel.setBounds(290, 170, 245, 200);
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.gray));
this.add(panel);
JLabel Mention = new JLabel("");
Mention.setBounds(310, 100, 280, 25);
Mention.setForeground(Color.RED);
Mention.setFont(new Font(null, Font.BOLD, 14));
this.add(Mention);
// 登陆事件
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// System.out.print(type1.getSelectedIndex());
String LoginID = User2.getText();
String password = Password2.getText();
String type = listData[type1.getSelectedIndex()];
if (new Data.Login().Check(LoginID, password, type)) {
dispose();
if (type.equals("员工")) {
usern = LoginID;
new EmployMain().open();
} else if (type.equals("企业管理者")) {
new Admin().open();
} else if (type.equals("系统管理员")) {
//
dispose();
new ADMIN1();
}
} else {
Mention.setText("输入信息有误,请重新输入");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Mention.setText("2");
}
}
});
// 注册事件
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
Register R = new Register();
}
});
}
}
package View;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Register extends JFrame{
String CODE;
String NAME;
/**
* Launch the application.
*/
public static void main(String[] args) {
new Register();
}
/**
* Create the application.
*/
public Register() {
initialize();
this.setBounds(500, 300, 800, 560);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("注册界面");
this.setLocation(500, 300);
this.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JLabel bg = new JLabel(new ImageIcon("Images/Register/BG2.jpg"));
this.setContentPane(bg);
JLabel label = new JLabel("欢迎注册");
label.setBounds(380, 180, 120, 25);
label.setFont(new Font(null, Font.BOLD, 18));// 字体设置
this.add(label);
JLabel User = new JLabel("账号");
User.setBounds(322, 222, 30, 25);
User.setFont(new Font(null, Font.PLAIN, 14));// 字体设置
this.add(User);
JLabel User1 = new JLabel(new ImageIcon("Images/LoginIcon/User.png"));
User1.setBounds(350, 220, 30, 30);
this.add(User1);
JLabel Password = new JLabel("密码");
Password.setBounds(322, 254, 30, 25);
Password.setFont(new Font(null, Font.PLAIN, 14));// 字体设置
this.add(Password);
JLabel Password1 = new JLabel(new ImageIcon("Images/LoginIcon/Lock.png"));
Password1.setBounds(352, 250, 30, 30);
this.add(Password1);
VerificationCodeImgUtil V=new VerificationCodeImgUtil();
//new VerificationCodeImgUtil().Excute();
V.Excute();
CODE=V.code;
NAME=V.Name;
JLabel Check = new JLabel(new ImageIcon("Images/RadomCheck/"+NAME+".png"));
System.out.println("2:"+CODE);
Check.setBounds(322, 285, 120, 30);
this.add(Check);
JTextField Check2 = new JTextField();
Check2.setBounds(444, 285, 55, 30);
this.add(Check2);
// 文本框和按钮
JButton btn = new JButton("注册");
btn.setBounds(320, 325, 180, 25);
this.add(btn);
JTextField User2 = new JTextField();
User2.setBounds(380, 222, 120, 25);
this.add(User2);
JPasswordField Password2 = new JPasswordField();
Password2.setBounds(380, 254, 120, 25);
this.add(Password2);
JPanel panel = new JPanel(null);
panel.setBounds(290, 170, 245, 200);
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.gray));
this.add(panel);
JButton return1=new JButton("返回");
return1.setBounds(10, 10, 60, 25);
this.add(return1);
return1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
new Login();
}
});
//事件处理
JLabel Mention=new JLabel("");
Mention.setBounds(350, 100, 120, 25);
Mention.setForeground(Color.RED);
Mention.setFont(new Font(null,Font.BOLD,17));
this.add(Mention);
JLabel Readme=new JLabel("注册须知 ");
Readme.setBounds(620,200 , 120, 25);
Readme.setForeground(Color.RED);
this.add(Readme);
JLabel Readme1=new JLabel(
"1.账号长度为5位,若纯数字"
);
Readme1.setBounds(580,230 , 160, 25);
Readme1.setForeground(Color.RED);
this.add(Readme1);
JLabel Readme2=new JLabel(
"需要大于00100"
);
Readme2.setBounds(580,260 , 160, 25);
Readme2.setForeground(Color.RED);
this.add(Readme2);
JLabel Readme3=new JLabel(
"2.密码需要在10位以内"
);
Readme3.setBounds(580,290 , 160, 25);
Readme3.setForeground(Color.RED);
this.add(Readme3);
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//new VerificationCodeImgUtil().Excute();
// NAME=new VerificationCodeImgUtil().Name;
//Check.setIcon(new ImageIcon("Images/RadomCheck/"+NAME+".png") );
System.out.print("Y");
//Check 密码、账号规范和验证码匹配
String RadomCheck=Check2.getText();
String Password=Password2.getText();
String UserName=User2.getText();
//String CODE=new VerificationCodeImgUtil().getCode();
System.out.println(CODE+" "+RadomCheck);
if(RadomCheck.equals(CODE)){
//
if(Password.length()>=10||UserName.length()!=5){
Mention.setText("注意注册规范!");
VerificationCodeImgUtil V=new VerificationCodeImgUtil();
//new VerificationCodeImgUtil().Excute();
V.Excute();
CODE=V.code;
NAME=V.Name;
Check.setIcon(new ImageIcon("Images/RadomCheck/"+NAME+".png") );
}else{
if(new Data.Login().Checkexist(UserName)){
Mention.setText("改用户名已注册!");
VerificationCodeImgUtil V=new VerificationCodeImgUtil();
//new VerificationCodeImgUtil().Excute();
V.Excute();
CODE=V.code;
NAME=V.Name;
Check.setIcon(new ImageIcon("Images/RadomCheck/"+NAME+".png") );
}else{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= df.format(new Date());
new Data.Login().InsertNewUser(UserName, Password,time);
Mention.setText("注册成功!");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Mention.setText("");
}
}
// dispose();
// new Login();
}else{
//延时??
Mention.setText("验证码错误!");
VerificationCodeImgUtil V=new VerificationCodeImgUtil();
//new VerificationCodeImgUtil().Excute();
V.Excute();
CODE=V.code;
NAME=V.Name;
Check.setIcon(new ImageIcon("Images/RadomCheck/"+NAME+".png") );
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
}