小组项目
功能模块:
登录功能
注册功能
管理员增删改查
前端展示功能
1.登录功能
public class DengLu extends JFrame implements MouseListener{
int i = 1;
JButton denglu = new JButton();
JButton zhuce = new JButton();
JButton guanli = new JButton();
JTextField username = new JTextField();
JTextField password = new JTextField();
JTextField yanzheng = new JTextField();
JCheckBox jCheckBox =new JCheckBox();
JDialog jDialog = new JDialog();
ArrayList<Gmaeuser> userList;
ArrayList<String> usernameList;
String path = "D:\\image\\素材\\EDG.png";
Utils utils = new Utils();
Gmaeuser gmaeuser =new Gmaeuser();
private YanZhengMa vcode = new YanZhengMa();
public DengLu(ArrayList<Gmaeuser> userList, ArrayList<String> usernameList ){
this.userList = userList;
this.usernameList = usernameList;
initFrame();
//在主界面中添加其他组件
initView();
setVisible(true);
}
public void initView() {
JLabel usernameText = new JLabel("账号:");
usernameText.setForeground(new Color(255, 255, 216));
usernameText.setBounds(1050, 220, 51, 19);//位置
username.setBounds(1050, 240, 200, 30);//用户名输入框位置
//创建JLabel添加密码文字
JLabel passwordText = new JLabel("密码:");
passwordText.setForeground(new Color(255, 255, 216));
passwordText.setBounds(1050, 280, 35, 18);
password.setBounds(1050, 300, 200, 30);//密码位置
JLabel yz = new JLabel("验证码:");
yz.setBounds(1050, 340, 45, 18);
yz.setForeground(new Color(255, 255, 216));
yanzheng.setBounds(1050, 360, 100, 40);
vcode.setBounds(1150, 360, 100, 10);
jCheckBox.setText("我已阅读同意用户换肤协议");
jCheckBox.setBackground(new Color(12, 13, 13));
jCheckBox.setForeground(new Color(255, 255, 216));
jCheckBox.setBounds(1050,400,200,15);
denglu.setText("用户登录");
denglu.setBounds(1075, 440, 160, 30);
//给登录按钮绑定鼠标监听
//当事件被触发之后,执行当前类中的对应方法
denglu.addMouseListener(this);
zhuce.setText("注册");
zhuce.setBounds(1075, 480, 160, 30);
zhuce.addMouseListener(this);
//管理员按钮位置
guanli.setText("管理员登录");
guanli.setBounds(1075, 520, 160, 30);
guanli.addMouseListener(this);
JLabel backGround = new JLabel(new ImageIcon("D:\\image\\WPS图片-修改尺寸.jpg"));
backGround.setBounds(0, 0, 1280, 662);
this.getContentPane().add(username);
this.getContentPane().add(password);
this.getContentPane().add(usernameText);
this.getContentPane().add(passwordText);
this.getContentPane().add(denglu);
this.getContentPane().add(zhuce);
this.getContentPane().add(guanli);
this.getContentPane().add(jCheckBox);
this.getContentPane().add(yanzheng);
this.getContentPane().add(vcode);
this.getContentPane().add(yz);
this.getContentPane().add(backGround);
this.setTitle("登录界面");
}
public void initFrame() {
//大小
setSize(1280, 670);
//居中
setLocationRelativeTo(null);
//置顶
setAlwaysOnTop(true);
//关闭模式
setDefaultCloseOperation(3);
//取消内部默认布局
setLayout(null);
}
//该方法用于展示一个弹框
//形参:表示当前弹框展示的内容
public void showDialog(String content) {
//JDialog就是一个弹框,跟JFrame是一样的
//isVisible表示判断弹框是否展示出来,
//如果没有展示出来,我们才进行设置,并展示
if(!jDialog.isVisible()){
//设置弹框的宽和高
jDialog.setSize(300, 150);
//居中
jDialog.setLocationRelativeTo(null);
jDialog.setAlwaysOnTop(true);
//置顶
jDialog.setAlwaysOnTop(true);
//删除弹框中所有的内容
jDialog.getContentPane().removeAll();
//创建一个JLabel去编写文本内容
JLabel jLabel = new JLabel(content);
//把文本添加到弹框当中
jDialog.add(jLabel);
//把弹框展示出来
jDialog.setVisible(true);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == denglu) {
if("".equals(username.getText())){
showDialog("用户名为空");
}
if("".equals(password.getText())){
showDialog("密码为空");
}
else if(yanzheng.getText().isEmpty()) {
showDialog("请输入验证码!");
return;
}else
if(!isValidCodeRight()) {
showDialog("验证码错误!");
return;
}
if(jCheckBox.isSelected()){
username.setEnabled(true);
password.setEnabled(true);
yanzheng.setEnabled(true);
}else {
showDialog("请勾选隐私协议");
return;
}
try {
FileReader fr = new FileReader("D:\\aaa\\a.txt");
//用于接收读取到的字符
int len = 0;
String aa ="";
//循环读取到len=-1,即文件末尾
while((len = fr.read()) != -1){
//不加强转输出结果是数字,要将他们转换为字符
char len1 = (char) len;
aa+=len1;
}
// list.add(aa);
//关流
fr.close();
String[] split = aa.split(",");
int count = 0;
for (int i = 0; i < split.length; i++) {
String[] splt1 = split[i].split("#");
if(splt1[0].equals(username.getText())&&splt1[1].equals(password.getText())){
this.setVisible(false);
new Exhibit("展示",path);
return;
}
}
if (i == 3){
System.exit(0);
}else {
showDialog("您还有"+(3-i)+"次机会");
i++;
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}else if(e.getSource() == zhuce){
this.setVisible(false);
new Register(userList,usernameList);
}else if(e.getSource()==guanli){
if("".equals(username.getText())&&"".equals(password.getText())){
showDialog("用户名和密码为空");
}
else if("admin".equals(username.getText())&&"123".equals(password.getText())){
int count = 0;
if(yanzheng.getText().isEmpty()) {
showDialog("请输入验证码!");
return;
}else
if(!isValidCodeRight()) {
showDialog("验证码错误!");
return;
}
if(jCheckBox.isSelected()){
username.setEnabled(false);
password.setEnabled(false);
yanzheng.setEnabled(false);
}else {
showDialog("请勾选隐私协议");
username.setEnabled(true);
password.setEnabled(true);
yanzheng.setEnabled(true);
return;
}
this.setVisible(false);
new Admin(userList,usernameList);
} else {
showDialog("用户名和密码输错了");
}
}
}
@Override
public void mousePressed(MouseEvent e) {
//修改背景图片
//判断一下,当前按下的是否为登录按钮
/* if (e.getSource() == denglu) {
denglu.setIcon(new ImageIcon("D:\\BaiduNetdiskDownload\\2048登录注册\\登录按下.png"));
}else if(e.getSource() == zhuce){
zhuce.setIcon(new ImageIcon("D:\\BaiduNetdiskDownload\\2048登录注册\\注册按下.png"));
}*/
}
@Override
public void mouseReleased(MouseEvent e) {
//当鼠标松开的时候,需要把背景色改回来
/* if (e.getSource() == denglu) {
denglu.setIcon(new ImageIcon("D:\\BaiduNetdiskDownload\\2048登录注册\\登录.png"));
}else if(e.getSource() == zhuce){
zhuce.setIcon(new ImageIcon("D:\\BaiduNetdiskDownload\\2048登录注册\\注册.png"));
}*/
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public boolean isValidCodeRight() {
if(yanzheng == null) {
return false;
}else if(vcode == null) {
return true;
}else if(vcode.getCode() .equals(yanzheng.getText())) {
return true;
}else
return false;
}
}
2.注册功能
public class Register extends JFrame implements MouseListener {
private YanZhengMa yzma = new YanZhengMa();
Utils utils = new Utils();
Gmaeuser gmaeuser = new Gmaeuser();
JCheckBox cbx = new JCheckBox("我已同意《换肤用户协议》");
ArrayList<Gmaeuser> userList;
ArrayList<String> usernamelList;
//提交按钮
JButton submit = new JButton();
//重置按钮
JButton reset = new JButton();
//用户名文本输入框
JTextField username = new JTextField();
//密码文本输入框
JTextField password = new JTextField();
//确认密码文本输入框
JTextField repassword = new JTextField();
//验证码文本输入框
JTextField yzm = new JTextField();
JDialog jDialog = new JDialog();
public Register(ArrayList<Gmaeuser> userList, ArrayList<String> usernamelList){
this.userList = userList;
this.usernamelList = usernamelList;
//调用主界面方法
initFrame();
initview();
//将界面显示出来
setVisible(true);
}
public void initview() {
//创建注册界面组件
//创建用户名标题
JLabel usernameJlabel = new JLabel("注册用户名");
usernameJlabel.setFont(new Font("微软雅黑",0,20));
usernameJlabel.setBounds(230,150, 100, 100);
//用户名文本输入框的位
username.setBounds(340, 185, 200, 30);
//创建密码标题的
JLabel passwordJlabel = new JLabel("注册密码");
passwordJlabel.setFont(new Font("微软雅黑",0,20));
passwordJlabel.setBounds(250,200,100,100);
//密码文本输入框的位置
password.setBounds(340, 235, 200, 30);
//创建再次输入密码标题
JLabel repasswordJlabel = new JLabel("再次输入密码");
repasswordJlabel.setFont(new Font("微软雅黑",0,20));
repasswordJlabel.setBounds(210,240,120,120);
//再次输入密码文本输入框的位置
repassword.setBounds(340, 285, 200, 30);
//创建验证码标题
JLabel yzmJlabel = new JLabel("验证码");
yzmJlabel.setFont(new Font("微软雅黑",0,20));
yzmJlabel.setBounds(270, 320, 60, 60);
//验证码文本输入框的位置
yzm.setBounds(340, 335, 100, 40);
//生成验证码的位置
yzma.setBounds(440, 335, 100, 10);
//设置复选框
cbx.setFont(new Font("微软雅黑",0,12));
cbx.setBounds(400,390,200,15);
cbx.addMouseListener(this);
//提交按钮和重置按钮的样式
//设置提交按钮的样式
submit.setText("注册");
submit.setFont(new Font("微软雅黑",0,20));
submit.setIcon(new ImageIcon("D:\\image\\注册.png"));
submit.setBounds(270, 420, 85, 40);
submit.addMouseListener(this);
//设置重置按钮的样式
reset.setText("重置");
reset.setFont(new Font("微软雅黑",0,20));
reset.setIcon(new ImageIcon("D:\\image\\重置.png"));
reset.setBounds(420, 420, 85, 40);
reset.addMouseListener(this);
//创建背景标题
JLabel background = new JLabel(new ImageIcon("D:\\image\\1.jpg"));
//组件位置
//设置用户名标题位置
//背景标题位置
background.setBounds(0, 0, 1920, 1080);
//将组件添加进窗口
this.getContentPane().add(submit);
this.getContentPane().add(reset);
this.getContentPane().add(username);
this.getContentPane().add(password);
this.getContentPane().add(repassword);
this.getContentPane().add(yzm);
this.getContentPane().add(usernameJlabel);
this.getContentPane().add(passwordJlabel);
this.getContentPane().add(repasswordJlabel);
this.getContentPane().add(yzmJlabel);
this.getContentPane().add(yzma);
this.getContentPane().add(cbx);
this.getContentPane().add(background);
}
//将主界面方法封装
public void initFrame() {
//大小
setSize(1920, 1080);
//居中
setLocationRelativeTo(null);
//置顶
setAlwaysOnTop(true);
//关闭模式
setDefaultCloseOperation(3);
//取消内部默认布局
setLayout(null);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == submit) {
try {
FileReader fr = new FileReader("D:\\aaa\\a.txt");
//用于接收读取到的字符
int len = 0;
String aa ="";
//循环读取到len=-1,即文件末尾
while((len = fr.read()) != -1){
//不加强转输出结果是数字,要将他们转换为字符
char len1 = (char) len;
aa+=len1;
}
// list.add(aa);
//关流
fr.close();
String[] split = aa.split(",");
if ("".equals((username.getText())) || "".equals(password.getText())) {
showDialog("用户名或者密码为空");
return;
}
if (!password.getText().equals(repassword.getText