Java学生信息管理系统实训第四天总结

实训第四天,上次了将服务类的全部测试完了以后,就开始写应用程序类。最先写了Application.
在这里插入图片描述
package net.lsl.student.app;

import net.lsl.student.gui.LoginFrame;
import net.lsl.student.gui.MainFrame;
import net.lsl.student.gui.RegisterFrame;

/**

  • 功能:应用程序类
  •   存放全局数据
    
  • 作者:李世兰
  • 日期:2019年6月20日08:26:04
    /
    public class Application {
    /
    *
    • 登录用户标识符
      /
      public static int id;
      /
      *
    • 登录用户名
      /
      public static String username;
      /
      *
    • 登录用户密码
      /
      public static String password;
      /
      *
    • 登录窗口
      /
      public static LoginFrame loginFrame;
      /
      *
    • 主窗口
      /
      public static MainFrame mainFrame;
      /
      *
    • 注册窗口
      */
      public static RegisterFrame registerFrame;
      }

然后再建立了三个界面类。
在这里插入图片描述
1、主界面窗口MainFrame
package net.lsl.student.gui;

import net.lsl.student.app.Application;
import net.lsl.student.bean.Status;
import net.lsl.student.service.StatusService;
import net.lsl.student.service.impl.StatusServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.io.IOException;

public class MainFrame extends JFrame {

/**
 * 菜单部分
 */
private JMenuBar mnbMain;
/**
 * 设置菜单
 */
private JMenu mnuSet;
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
/**
 * 操作菜单
 */
private JMenu mnuOperate;
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
/**
 * 删除菜单
 */
private JMenu mnuDelStu;
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
/**
 * 查询菜单
 */
private JMenu mnuFind;
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
/**
 * 统计菜单
 */
private JMenu mnuCount;
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
/**
 * 帮助菜单
 */
private JMenu mnuHelp;
private JMenuItem mniHelp;
private JMenuItem mniAbout;
/**
 * 面板
 */
private JPanel panel;
private JPanel  pnlCenter;
private JPanel  pnlSouth;
/**
 * 状态栏标签
 */
private JLabel lblStatusBar;
/**
 * 背景标签
 */
private JLabel lblBackground;

/**
 * 图标对象
 */
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
/**
 * 工具栏
 */
private JToolBar toolbar;
/**
 * 按钮
 */
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
/**
 * 状态对象
 */

private static StatusService statusService;
private static Status status;

/**
 *
 * @param title
 */
public MainFrame(String title){
    super(title);
    initGUI();
}
private void initGUI(){

    // 创建主菜单
    mnbMain = new JMenuBar();

    // 创建【设置】菜单及其菜单项
    mnuSet = new JMenu("系统设置[S]");
    mnuSet.setMnemonic(KeyEvent.VK_S);
    mniSetCollegeInfo = new JMenuItem("学校信息");
    mniSetStatusBar = new JMenuItem("状态栏信息");
    mniChangePassword = new JMenuItem("修改密码");
    mniExit = new JMenuItem("退出系统");

    // 创建【操作】菜单及其菜单项
    mnuOperate = new JMenu("数据操作[O]");
    mnuOperate.setMnemonic(KeyEvent.VK_O);
    mniAddStudent = new JMenuItem("增加学生表记录");
    mnuDelStu = new JMenu("删除学生表记录");
    mniEditStudent = new JMenuItem("编辑学生表记录");
    mniBrowseStudent = new JMenuItem("浏览学生表记录");

    // 创建【删除学生表记录】的子菜单
    mniDelStudentById = new JMenuItem("按学号删除");
    mniDelStudentsByClass = new JMenuItem("按班级删除");
    mniDelStudentsByDepartment = new JMenuItem("按系部删除");

    // 创建【查询】菜单及其菜单项
    mnuFind = new JMenu("查询学生[Q]");
    mnuFind.setMnemonic(KeyEvent.VK_Q);
    mniFindStudentById = new JMenuItem("按学号查询");
    mniFindStudentsByName = new JMenuItem("按姓名查询");
    mniFindStudentsByClass = new JMenuItem("按班级查询");
    mniFindStudentsByDepartment = new JMenuItem("按系部查询");

    // 创建【统计】菜单及其菜单项
    mnuCount = new JMenu("人数统计[C]");
    mnuCount.setMnemonic(KeyEvent.VK_C);
    mniCountStudentsBySex = new JMenuItem("按性别统计");
    mniCountStudentsByClass = new JMenuItem("按班级统计");
    mniCountStudentsByDepartment = new JMenuItem("按系部统计");

    // 创建【帮助】菜单及其菜单项
    mnuHelp = new JMenu("帮助[H]");
    mnuHelp.setMnemonic(KeyEvent.VK_H);
    mniHelp = new JMenuItem("帮助");
    mniAbout = new JMenuItem("关于");

    // 创建图标对象
    imgCollege = new ImageIcon("images/college.png");
    imgPassword = new ImageIcon("images/password.png");
    imgQuery = new ImageIcon("images/query.png");
    imgBrowse = new ImageIcon("images/browse.png");
    imgCount = new ImageIcon("images/count.png");
    imgExit = new ImageIcon("images/exit.png");

    // 创建工具栏
    toolbar = new JToolBar();
    btnSetCollege = new JButton("设置学校", imgCollege);
    btnSetCollege.setToolTipText("设置学校信息");
    btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
    btnChangePassword = new JButton("修改密码", imgPassword);
    btnChangePassword.setToolTipText("修改用户密码");
    btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
    btnBrowseStudent = new JButton("浏览学生", imgBrowse);
    btnBrowseStudent.setToolTipText("浏览学生记录");
    btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
    btnFindStudentById = new JButton("查询学生", imgQuery);
    btnFindStudentById.setToolTipText("按学号查询学生记录");
    btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
    btnCountByDepartment = new JButton("统计人数", imgCount);
    btnCountByDepartment.setToolTipText("按系部统计学生人数");
    btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
    btnExit = new JButton("退出系统", imgExit);
    btnExit.setToolTipText("退出系统");
    btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
    btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
    toolbar.add(btnSetCollege);
    toolbar.add(btnChangePassword);
    toolbar.add(btnBrowseStudent);
    toolbar.add(btnFindStudentById);
    toolbar.add(btnCountByDepartment);
    toolbar.add(btnExit);

    // 创建面板
    panel = (JPanel) getContentPane();
    pnlCenter = new JPanel();
    pnlSouth = new JPanel();
    pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));

    // 创建背景图片
    imgBackground = new ImageIcon("images/background.jpg");
    // 创建背景标签
    lblBackground = new JLabel(imgBackground);
    // 创建状态栏标签
    lblStatusBar = new JLabel();

    // 设置菜单栏
    setJMenuBar(mnbMain);
    // 添加【设置】菜单
    mnbMain.add(mnuSet);
    mnuSet.add(mniSetCollegeInfo);
    mnuSet.add(mniSetStatusBar);
    mnuSet.add(mniChangePassword);
    mnuSet.addSeparator();
    mnuSet.add(mniExit);

    // 添加【删除学生表记录】菜单
    mnuDelStu.add(mniDelStudentById);
    mnuDelStu.add(mniDelStudentsByClass);
    mnuDelStu.add(mniDelStudentsByDepartment);

    // 添加【操作】菜单
    mnbMain.add(mnuOperate);
    mnuOperate.add(mniAddStudent);
    mnuOperate.add(mniEditStudent);
    mnuOperate.add(mnuDelStu);
    mnuOperate.add(mniBrowseStudent);

    // 添加【查询】菜单
    mnbMain.add(mnuFind);
    mnuFind.add(mniFindStudentById);
    mnuFind.add(mniFindStudentsByName);
    mnuFind.add(mniFindStudentsByClass);
    mnuFind.add(mniFindStudentsByDepartment);

    // 添加【统计】菜单
    mnbMain.add(mnuCount);
    mnuCount.add(mniCountStudentsBySex);
    mnuCount.add(mniCountStudentsByClass);
    mnuCount.add(mniCountStudentsByDepartment);

    // 添加【帮助】菜单
    mnbMain.add(mnuHelp);
    mnuHelp.add(mniHelp);
    mnuHelp.add(mniAbout);

    // 添加面板
    panel.setLayout(new BorderLayout());
    panel.add(toolbar, "North");
    panel.add(pnlCenter, "Center");
    panel.add(pnlSouth, "South");
    pnlCenter.add(lblBackground);
    pnlSouth.add(lblStatusBar);

    // 非管理员不能设置状态栏
    if (!Application.username.equals("admin")) {
        mniSetStatusBar.setEnabled(false);
        mniAddStudent.setEnabled(false);
        mnuDelStu.setEnabled(false);
        mniEditStudent.setEnabled(false);
    }


    // 设置状态栏信息
    setStatusBar();
    //创建状态服务对象
    statusService = new StatusServiceImpl();
    //获取状态对象
    status = statusService.findStatusById(1);

    //设置窗口尺寸
    setSize(800,640);
    //设置窗口可见
    setVisible(true);
    //设置窗口屏幕居中
    setLocationRelativeTo(null);
    //设置窗口标题
    setTitle("学生信息管理系统" + status.getVersion());

    //注册窗口监听器
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            exitSystem();
        }
    });

    // 设置菜单
    // 【设置学校信息】菜单项单击事件

mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});

    // 【设置状态栏信息】菜单项单击事件
    mniSetStatusBar.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new SetStatusBarFrame("");
        }
    });

    // 【修改密码】菜单项单击事件
    mniChangePassword.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new ChangePasswordFrame("");
        }
    });

    // 【退出系统】菜单项单击事件
    mniExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            exitSystem();
        }
    });

    // 查询菜单
    // 【按学号查询】菜单项单击事件
    mniFindStudentById.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new FindStudentByIdFrame("");
        }
    });

    // 【按姓名查询】菜单项单击事件
    mniFindStudentsByName.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new FindStudentsByNameFrame("");
        }
    });

    // 【按班级查询】菜单项单击事件
    mniFindStudentsByClass.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new FindStudentsByClassFrame("");
        }
    });

    // 【按系部查询】菜单项单击事件
    mniFindStudentsByDepartment.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new FindStudentsByDepartmentFrame("");
        }
    });

    // 统计菜单
    // 【按性别统计人数】菜单项单击事件
    mniCountStudentsBySex.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new CountStudentBySexFrame("");
        }
    });

    // 【按班级统计人数】菜单项单击事件
    mniCountStudentsByClass.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new CountStudentByClassFrame("");
        }
    });

    // 【按系部统计人数】菜单项单击事件
    mniCountStudentsByDepartment.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new CountStudentsByDepartmentFrame("");
        }
    });

    // 【增加学生记录】菜单项单击事件
    mniAddStudent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new AddStudentFrame("");
        }
    });

    // 【按学号删除学生记录】菜单项单击事件
    mniDelStudentById.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new DeleteStudentByIdFrame("");
        }
    });

    // 【按班级删除学生记录】菜单项单击事件
    mniDelStudentsByClass.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new DeleteStudentsByClassFrame("");
        }
    });

    // 【按系部删除学生记录】菜单项单击事件
    mniDelStudentsByDepartment.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new DeleteStudentsByDepartmentFrame("");
        }
    });

    // 【编辑学生记录】菜单项单击事件
    mniEditStudent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new EditStudentFrame("");
        }
    });

    // 【浏览学生记录】菜单项单击事件
    mniBrowseStudent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new BrowseStudentFrame("");
        }
    });

    // 【帮助】菜单单击事件
    mniHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");
            } catch (IOException e1) {
                JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
            }
        }
    });

    // 【关于】菜单单击事件
    mniAbout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null,
                    "开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()
                            + "\n电子邮箱:" + status.getEmail(),
                    "学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
        }
    });

    // 工具栏按钮单击事件
    // 【设置学校信息】按钮
    btnSetCollege.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new SetCollegeInfoFrame("");
        }
    });

    // 【修改密码】按钮
    btnChangePassword.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new ChangePasswordFrame("");
        }
    });

    // 【浏览】按钮
    btnBrowseStudent.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new BrowseStudentFrame("");
        }
    });

    // 【查询】按钮
    btnFindStudentById.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new FindStudentByIdFrame("");
        }
    });

    // 【统计】按钮
    btnCountByDepartment.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new CountStudentsByDepartmentFrame("");
        }
    });

    // 【退出】按钮
    btnExit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            exitSystem();
        }
    });
}


/**
 * 退出系统
 */
private void exitSystem(){
    int choice = JOptionPane.showConfirmDialog(this,
            "您是否要退出系统?","学生信息管理系统",JOptionPane.YES_NO_OPTION);
    if (choice == JOptionPane.YES_OPTION){
        System.exit(0);
    }else{
        //卸载当前窗口
        dispose();
        //重新显示主窗口
        Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());
    }
}


/**
 * 设置状态栏信息
 */
public void setStatusBar() {
    // 创建状态服务对象
    statusService = new StatusServiceImpl();
    // 获取状态栏对象
    status = statusService.findStatusById(1);
    // 设置状态栏标签
    lblStatusBar.setText(
            status.getCollege() + "学生信息管理系统" + status.getVersion() + "      作者:" + status.getAuthor() + "      地址:"
                    + status.getAddress() + "      电话:" + status.getTelephone() + "      邮箱:" + status.getEmail());
}


public static void main(String[] args) {
    StatusService statusService = new StatusServiceImpl();
    Status status = statusService.findStatusById(1);
    Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());

}

}

这里有两种方法进行编写,但是有一种比较困难,所以我们采取了另一种方法。

2、用户注册窗口RegisterFrame
package net.lsl.student.gui;

import net.lsl.student.app.Application;
import net.lsl.student.bean.User;
import net.lsl.student.service.UserService;
import net.lsl.student.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;
import java.sql.Timestamp;
import java.util.Date;

/**

  • 功能:注册窗口

  • 作者:李世兰

  • 日期:2019年6月20日09:32:47
    */
    public class RegisterFrame extends JFrame {
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JLabel lblTelephone;

    private JTextField txtUsername;
    private JTextField txtTelephone;
    private JPasswordField txtPassword;

    private JButton btnSubmit;
    private JButton btnCancel;
    private JButton btnLogin;

    private JPanel panel;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;

    private String username;
    private String password;
    private String telephone;
    private Date registerTime;

    /**

    • 构造方法
    • @param title
      */

    public RegisterFrame(String title) {
    super(title);
    initGUI();
    }

    /**

    • 初始化图形用户界面
      */
      private void initGUI() {
      //创建组件(面板与控件)
      panel = (JPanel) getContentPane();
      panel1 = new JPanel();
      panel2 = new JPanel();
      panel3 = new JPanel();
      panel4 = new JPanel();

      lblUsername = new JLabel(“用户名:”);
      lblPassword = new JLabel(“密 码:”);
      lblTelephone = new JLabel(“电 话:”);
      txtUsername = new JTextField("", 15);
      txtPassword = new JPasswordField("", 15);
      txtTelephone = new JTextField("", 15);
      btnSubmit = new JButton(“提交[S]”);
      btnCancel = new JButton(“取消[C]”);
      btnLogin = new JButton(“登录[L]”);

      //将控件添加到四个小面板
      panel1.add(lblUsername);
      panel1.add(txtUsername);
      panel2.add(lblPassword);
      panel2.add(txtPassword);
      panel3.add(lblTelephone);
      panel3.add(txtTelephone);
      panel4.add(btnSubmit);
      panel4.add(btnCancel);
      panel4.add(btnLogin);

      //设置打面板布局,将四个小面板添加到大面板
      panel.setLayout(new GridLayout(4, 1));
      panel.add(panel1);
      panel.add(panel2);
      panel.add(panel3);
      panel.add(panel4);

      //设置窗口大小
      setSize(350, 300);
      //设置窗口屏幕居中
      setLocationRelativeTo(null);
      //设置窗口不可调整大小
      setResizable(false);
      //设置窗口刚好容纳组件
      pack();
      //设置窗口标题
      setTitle(“用户注册”);
      //设置窗口可见
      setVisible(true);
      //设置窗口默认关闭操作
      //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //注册窗口监听器
      addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
      dispose();
      //让登陆窗口可见
      Application.loginFrame.setVisible(true);

       }
      

      });
      //【提交】按钮单击事件处理
      btnSubmit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      register();
      }
      });
      //【提交按钮事件处理
      btnSubmit.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER){
      register();
      }
      }
      });

      //【取消】按钮单击事件处理
      btnCancel.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      //显示登录窗口
      Application.loginFrame.setVisible(true);
      //卸载当前窗口
      dispose();
      }
      });
      //【登录】按钮单击事件处理
      btnLogin.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
      //显示登录窗口
      Application.loginFrame.setVisible(true);
      //卸载当前窗口
      dispose();
      }
      });

      //【用户名】文本框按钮事件处理
      txtUsername.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER){
      txtPassword.requestFocus();
      }
      }
      });
      //【密码】文本框按钮事件处理
      txtPassword.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == 10){
      txtTelephone.requestFocus();
      }
      }
      });
      //【电话】文本框按钮事件处理
      txtTelephone.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER){
      btnSubmit.requestFocus();
      }
      }
      });
      }

    private void register(){
    //获取用户名
    username = txtUsername.getText().trim();
    //获取密码
    password = new String(txtPassword.getPassword());
    //获取电话
    telephone =txtTelephone.getText().trim();
    //定义当前时间为注册时间
    registerTime = new Timestamp(System.currentTimeMillis());

     //定义用户服务对象
     UserService userService = new UserServiceImpl();
     //创建用户
     User user = new User();
     user.setUsername(username);
     user.setPassword(password);
     user.setTelephone(telephone);
     user.setRegisterTime(registerTime);
    
     //添加用户
     int count = userService.addUser(user);
    
    
     //判断是否添加成功
     if (count > 0){
         setVisible(false);
         JOptionPane.showMessageDialog(null,"恭喜!注册成功!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
         setVisible(true);
     }else{
         JOptionPane.showMessageDialog(null,"遗憾!注册失败!","学生信息管理系统",JOptionPane.INFORMATION_MESSAGE);
     }
    

    }
    /**

    • 主方法
    • @param args
      */
      public static void main(String[] args) {
      Application.registerFrame = new RegisterFrame("");
      }
      }

3、用户登录窗口LoginFrame

package net.lsl.student.gui;

import net.lsl.student.app.Application;
import net.lsl.student.bean.Status;
import net.lsl.student.bean.User;
import net.lsl.student.service.StatusService;
import net.lsl.student.service.UserService;
import net.lsl.student.service.impl.StatusServiceImpl;
import net.lsl.student.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**

  • 功能:登陆窗口

  • 作者:李世兰

  • 日期:2019年6月20日10:12:15
    */
    public class LoginFrame extends JFrame {
    private String username;
    private String password;

    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField txtPassword;

    private JButton btnOK;
    private JButton btnCancel;
    private JButton btnRegister;
    private JPanel panel,panel1,panel2,panel3;

    public LoginFrame(String title) {
    super(title);
    initGUI();
    }

    private void initGUI() {
    //实例化组件(面板与控件)
    panel = (JPanel) getContentPane();
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();

     lblUsername = new JLabel("用户名:");
     lblPassword = new JLabel("密   码:");
     txtUsername = new JTextField(15);
     txtPassword = new JPasswordField(15);
     btnOK = new JButton("确定[O]");
     btnCancel = new JButton("取消[C]");
     btnRegister = new JButton("注册[R]");
    
     //将控件添加到三个小面板
     panel1.add(lblUsername);
     panel1.add(txtUsername);
     panel2.add(lblPassword);
     panel2.add(txtPassword);
     panel3.add(btnOK);
     panel3.add(btnCancel);
     panel3.add(btnRegister);
    
     //设置主面板为三行一列的网格布局
     panel.setLayout(new GridLayout(3,1));
     //将三个小面板依次添加到主面板
     panel.add(panel1);
     panel.add(panel2);
     panel.add(panel3);
     //设置按钮的热键字母
     btnOK.setMnemonic(KeyEvent.VK_0);
     btnCancel.setMnemonic(KeyEvent.VK_C);
     btnRegister.setMnemonic(KeyEvent.VK_R);
     //设置密码框回显字符
     txtPassword.setEchoChar('*');
    
     //设置窗口大小
     setSize(500,600);
     //设置窗口居中
     setLocationRelativeTo(null);
     //设置出窗口不可调整大小
     setResizable(false);
     //设置窗口刚好容纳主键
     pack();
     //设置窗口可见
     setVisible(true);
     //设置窗口默认关闭操作
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     //【确定】按钮单击事件处理
     btnOK.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
             login();
         }
     });
     //【确定】按钮事件处理
     btnOK.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e) {
             if (e.getKeyCode() == KeyEvent.VK_ENTER){
                 login();
             }
         }
     });
    
     //【用户名】文本框案件事件处理
     txtUsername.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e) {
             //判断是否按下回车键
             if (e.getKeyCode() == KeyEvent.VK_ENTER){
                 //让密码文本框获取焦点
                 txtPassword.requestFocus();
             }
         }
     });
    
     //【注册】按钮单击事件处理
     btnRegister.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
             //隐藏登录窗口
             Application.registerFrame = new RegisterFrame("用户注册");
         }
     });
    
     //【密码】文本框按钮事件处理
     txtPassword.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e) {
             if (e.getKeyCode() == KeyEvent.VK_ENTER){
                 login();
             }
         }
     });
    
     //【取消】按钮单击事件处理
     btnCancel.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
             System.exit(0);
         }
     });
    

    }
    //

    /**

    • 登录方法
      */

    private void login(){
    username = txtUsername.getText().trim();
    //获取密码
    password = new String(txtPassword.getPassword());

     //创建用户服务对象
     UserService userService = new UserServiceImpl();
     //调用登录方法,返回登录用户对象
     User user = userService.login(username,password);
    
     //判断登录用户身份为空,决定登录是否成功
     if (user != null){
         //隐藏登录窗口
         Application.loginFrame.setVisible(false);
         //定义状态服务对象
         StatusService statusService = new StatusServiceImpl();
         //获取状态对象
         Status  status = statusService.findStatusById(1);
         //保存登录用户的信息
         Application.id = user.getId();
         Application.username = user.getUsername();
         Application.password = user.getPassword();
         //提示用户登录成功
         JOptionPane.showMessageDialog(null,
                 "欢迎使用学生信息管理系统" +status.getVersion(),
                 "用户登录",JOptionPane.INFORMATION_MESSAGE  );
         //显示系统主窗口
         Application.mainFrame = new MainFrame("");
         //释放登录窗口
         Application.loginFrame.dispose();
     }else{
         //隐藏登录窗口
         Application.loginFrame.setVisible(false);
         //提示用户登录失败,要重新输入用户名或密码
         JOptionPane.showMessageDialog(null,"用户名或密码错误,请重新输入!",
                 "用户登录",JOptionPane.ERROR_MESSAGE);
         //显示登录窗口
         Application.loginFrame.setVisible(true);
         //用户名文本框内容全选
         txtUsername.selectAll();
         //密码文本框内容全选
         txtPassword.selectAll();
         //用户名文本框获取焦点
         txtUsername.requestFocus();
    
     }
    

    }

    public static void main(String[] args) {
    Application.loginFrame = new LoginFrame(“用户登录”);
    }

}

这里的代码是一次又一次进行完善的,经过很多次修改添加。在写的过程中会因为一些小细节而导致错误,有时候自己找一找可以自己解决,但是有时候自己却找不到错误。只能通过请教老师来解决这些问题。

4、接下来就写了设置学校信息窗口SetCollegeInfoFrame
package net.lsl.student.gui;

import net.lsl.student.bean.College;
import net.lsl.student.service.CollegeService;
import net.lsl.student.service.impl.CollegeServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**

  • 功能:设置学校信息

  • 作者:李世兰

  • 日期:2019年6月21日18:49:37
    */
    public class SetCollegeInfoFrame extends JFrame {
    //面板
    JPanel panel;
    JPanel pnlNorth;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    JPanel panel4;
    JPanel panel5;
    JPanel pnlCenter;
    JPanel pnlSouth;

    //标签
    JLabel lblName;
    JLabel lblPresident;
    JLabel lblStartTime;
    JLabel lblTelephone;
    JLabel lblEmail;
    JLabel lblAddress;
    JLabel lblProfile;

    //按钮
    JButton btnSave;
    JButton btnExit;

    //文本框与文本区
    JTextField txtName;
    JTextField txtPresident;
    JTextField txtStartTime;
    JTextField txtTelephone;
    JTextField txtEmail;
    JTextField txtAddress;
    JTextArea txtProfile;

    //滚动面板
    JScrollPane scrollPane;

    //学校服务
    private CollegeService collegeService;
    private College college;
    private int id = 1;

    public SetCollegeInfoFrame(String title){
    super(title);
    initGUI();
    }

    //初始化用户界面
    private void initGUI() {
    //创建组件(面板与控件)
    panel = (JPanel) getContentPane();
    pnlNorth = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    panel4 = new JPanel();
    panel5 = new JPanel();
    pnlCenter = new JPanel();
    pnlSouth = new JPanel();

     lblName = new JLabel("学校名称:");
     lblPresident = new JLabel("校  长:");
     lblStartTime = new JLabel("建校时间:");
     lblTelephone = new JLabel("联系电话:");
     lblEmail = new JLabel("电子邮箱:");
     lblAddress = new JLabel("通讯地址:");
     lblProfile = new JLabel("学校简介:");
    
     txtName = new JTextField(30);
     txtPresident = new JTextField(11);
     txtStartTime = new JTextField(12);
     txtTelephone = new JTextField(11);
     txtEmail = new JTextField(12);
     txtAddress = new JTextField(30);
     txtProfile = new JTextArea(5,37);
     txtProfile.setLineWrap(true);
     scrollPane = new JScrollPane(txtProfile,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     btnSave = new JButton("保存[S]");
     btnSave.setMnemonic(KeyEvent.VK_X);
     btnExit = new JButton("退出[X]");
     btnExit.setMnemonic(KeyEvent.VK_X);
    
     //设置大面板布局,将北面板、中面板与南面板添加到大面板
     panel.setLayout(new BorderLayout());
     panel.add(pnlNorth,"North");
     panel.add(pnlCenter,"Center");
     panel.add(pnlSouth,"South");
    
     //设置北面板布局,添加五个小面板
     pnlNorth.setLayout(new GridLayout(5,1));
     pnlNorth.add(panel1);
     pnlNorth.add(panel2);
     pnlNorth.add(panel3);
     pnlNorth.add(panel4);
     pnlNorth.add(panel5);
    
    
     //将控件依次添加到五个小面板
     panel1.add(lblName);
     panel1.add(txtName);
     panel2.add(lblPresident);
     panel2.add(txtPresident);
     panel2.add(lblStartTime);
     panel2.add(txtStartTime);
     panel3.add(lblTelephone);
     panel3.add(txtTelephone);
     panel3.add(lblEmail);
     panel3.add(txtEmail);
     panel4.add(lblAddress);
     panel4.add(txtAddress);
     panel5.add(lblProfile);
    
     //将滚动面板添加到中面板
     pnlCenter.add(scrollPane);
    
     //将两个按钮添加到南面板
     pnlSouth.add(btnSave);
     pnlSouth.add(btnExit);
    
     //创建学习服务对象
     collegeService = new CollegeServiceImpl();
     //获取学校对象
     college = collegeService.findCollegeById(id);
     //利用学校对象属性值设置相应控件的内容
     txtName.setText(college.getName());
     txtPresident.setText(college.getPresident());
     txtStartTime.setText(college.getStartTime().toString());
     txtTelephone.setText(college.getTelephone());
     txtEmail.setText(college.getEmail());
     txtAddress.setText(college.getAddress());
     txtProfile.setText(college.getProfile());
    
     //设置窗口大小
     setSize(450,400);
     //设置窗口屏幕居中
     setLocationRelativeTo(null);
     //设置窗口不可调整大小
     setResizable(false);
     //设置窗口刚好容纳组件
     pack();
     //设置窗口标题
     setTitle("设置学校信息");
     //设置窗口可见
     setVisible(true);
     //设置窗口默认关闭操作
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     //【关闭】按钮单击事件处理
     btnExit.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
             //关闭窗口
             dispose();
         }
     });
    
     //【保存】按钮单击事件处理
     btnSave.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent event) {
             try {
                 college.setName(txtName.getText());
                 college.setPresident(txtPresident.getText());
                 college.setStartTime(new Timestamp(new SimpleDateFormat("yyyy-mm-dd").parse(txtStartTime.getText().toString()).getTime()));
                 college.setTelephone(txtTelephone.getText());
                 college.setEmail(txtEmail.getText());
                 college.setAddress(txtAddress.getText());
                 college.setProfile(txtProfile.getText());
                 //更新学校信息
                 collegeService.updateCollege(college);
                 //提示用户更新成功
                 JOptionPane.showMessageDialog(null,"更新数据成功!");
             } catch (ParseException e) {
                 JOptionPane.showMessageDialog(null,"更新数据失败!");
             }
         }
     });
    

    }

    public static void main(String[] args) {
    new SetCollegeInfoFrame("");
    }
    }

5、修改用户密码窗口ChangePasswordFrame
package net.lsl.student.gui;

import net.lsl.student.app.Application;
import net.lsl.student.bean.User;
import net.lsl.student.service.UserService;
import net.lsl.student.service.impl.UserServiceImpl;

import javax.swing.;
import java.awt.
;
import java.awt.event.*;

/**

  • 功能:修改用户密码

  • 作者:李世兰

  • 日期:2019年6月21日20:03:40
    */
    public class ChangePasswordFrame extends JFrame {

    /**

    • 面板
      /
      private JPanel panel;
      private JPanel pnlRow1;
      private JPanel pnlRow2;
      private JPanel pnlRow3;
      private JPanel pnlRow4;
      private JPanel pnlRow5;
      /
      *
    • 标签
      /
      private JLabel lblUsername;
      private JLabel lblPassword;
      private JLabel lblNewPassword1;
      private JLabel lblNewPassword2;
      /
      *
    • 文本框
      /
      private JTextField txtUsername;
      private JPasswordField txtPassword;
      private JPasswordField txtNewPassword1;
      private JPasswordField txtNewPassword2;
      /
      *
    • 按钮
      /
      private JButton btnOK;
      private JButton btnCancel;
      /
      *
    • 用户服务对象
      */
      private UserService userService;

    /**

    • 构造方法
    • @param title
      */
      public ChangePasswordFrame(String title) {
      super(title);
      initGUI();
      }

    /**

    • 初始化用户界面
      */
      private void initGUI() {
      // 创建组件(面板与控件)
      panel = (JPanel) getContentPane();
      pnlRow1 = new JPanel();
      pnlRow2 = new JPanel();
      pnlRow3 = new JPanel();
      pnlRow4 = new JPanel();
      pnlRow5 = new JPanel();

      lblUsername = new JLabel(“用户名:”);
      lblPassword = new JLabel(“旧密码:”);
      lblNewPassword1 = new JLabel(“新密码:”);
      lblNewPassword2 = new JLabel(“确 认:”);

      txtUsername = new JTextField(20);
      txtUsername.setEditable(false);
      txtPassword = new JPasswordField(20);
      txtNewPassword1 = new JPasswordField(20);
      txtNewPassword2 = new JPasswordField(20);

      btnOK = new JButton(“确定[O]”);
      btnOK.setMnemonic(KeyEvent.VK_O);
      btnCancel = new JButton(“取消[C]”);
      btnCancel.setMnemonic(KeyEvent.VK_C);

      // 设置大面板布局,添加五个小面板
      panel.setLayout(new GridLayout(5, 1));
      panel.add(pnlRow1);
      panel.add(pnlRow2);
      panel.add(pnlRow3);
      panel.add(pnlRow4);
      panel.add(pnlRow5);

      // 将控件添加到五个小面板
      pnlRow1.add(lblUsername);
      pnlRow1.add(txtUsername);
      pnlRow2.add(lblPassword);
      pnlRow2.add(txtPassword);
      pnlRow3.add(lblNewPassword1);
      pnlRow3.add(txtNewPassword1);
      pnlRow4.add(lblNewPassword2);
      pnlRow4.add(txtNewPassword2);
      pnlRow5.add(btnOK);
      pnlRow5.add(btnCancel);

      // 设置窗口不可调整大小
      setResizable(false);
      // 设置窗口刚好容纳组件
      pack();
      // 设置窗口屏幕居中
      setLocationRelativeTo(null);
      // 设置窗口标题
      setTitle(“修改用户密码”);
      // 设置窗口可见
      setVisible(true);
      // 设置窗口默认关闭操作
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // 设置控件属性
      txtUsername.setEditable(false);
      txtUsername.setText(Application.username);

      // 【取消】按钮单击事件处理
      btnCancel.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      dispose();
      }
      });

      // 【确定】按钮单击事件处理
      btnOK.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      changePassword();
      }
      });

      // 【确定】按钮按键事件处理
      btnOK.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == 10) {
      changePassword();
      }
      }
      });

      // 【用户名】文本框按键事件处理
      txtUsername.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
      txtPassword.requestFocus();
      }
      }
      });

      // 【密码】文本框按键事件处理
      txtPassword.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
      txtNewPassword1.requestFocus();
      }
      }
      });

      // 【新密码1】文本框按键事件处理
      txtNewPassword1.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
      txtNewPassword2.requestFocus();
      }
      }
      });

      // 【新密码2】文本框按键事件处理
      txtNewPassword2.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
      btnOK.requestFocus();
      }
      }
      });
      }

    /**

    • 修改密码方法
      */
      private void changePassword() {
      // 获取用户信息
      int id = Application.id;
      String username = Application.username;

      // 保存用户输入的旧密码与两个新密码
      String password = new String(txtPassword.getPassword());
      String newPassword1 = new String(txtNewPassword1.getPassword());
      String newPassword2 = new String(txtNewPassword2.getPassword());

      // 创建学生服务对象
      userService = new UserServiceImpl();

      if (userService.login(username, password) == null) {
      JOptionPane.showMessageDialog(null, “旧密码错误,请重新输入!”, “错误提示”, JOptionPane.ERROR_MESSAGE);
      txtPassword.requestFocus();
      txtPassword.selectAll();
      } else if (newPassword1.equals("")) {
      JOptionPane.showMessageDialog(null, “新密码不能为空!”, “错误提示”, JOptionPane.ERROR_MESSAGE);
      txtNewPassword1.requestFocus();
      } else if (newPassword2.equals("")) {
      JOptionPane.showMessageDialog(null, “确认密码不能为空!”, “错误提示”, JOptionPane.ERROR_MESSAGE);
      txtNewPassword2.requestFocus();
      } else if (!newPassword1.equals(newPassword2)) {
      JOptionPane.showMessageDialog(null, “两次密码不一致,请重新输入!”, “错误提示”, JOptionPane.ERROR_MESSAGE);
      txtNewPassword1.setText("");
      txtNewPassword2.setText("");
      txtNewPassword1.requestFocus();
      } else {
      // 按标识符获取用户
      User user = userService.findUserById(id);
      // 修改用户的密码属性
      user.setPassword(newPassword1);
      // 调用用户服务对象的更新方法,更新用户信息
      int count = userService.updateUser(user);
      if (count > 0) {
      JOptionPane.showMessageDialog(null, “密码修改成功!”, “设置密码”, JOptionPane.INFORMATION_MESSAGE);
      dispose();
      } else {
      JOptionPane.showMessageDialog(null, “密码修改失败!”, “设置密码”, JOptionPane.WARNING_MESSAGE);
      }
      }
      }

    /**

    • 主方法
    • @param args
      */
      public static void main(String[] args) {
      Application.id = 1;
      Application.username = “李刚”;
      new ChangePasswordFrame("");
      }
      }
      今天感觉好累啊,在编写的过程中遇到了很多的问题。有时候自己找不到解决的办法就感觉非常烦躁。但是也没办法,依然要继续的努力敲代码。明天继续加油吧!

你可能感兴趣的:(Java实训)