目录
选课系统的总体设计
(1)选课系统环境要求
1、搭建java开发环境并配置JDK、安装Intellij IDEA、Navicat、mysql等工具。
2、使用java自带的swing实现窗口化图形界面。
(2)选课系统功能及模块图
学生选课系统具体后端实现和功能为:
1、后端java逻辑层:实体类、dao层、util工具类、view展示类。
2、数据库:存储商品信息以及商品所属分类,管理员账号与密码。
学生选课系统整体的功能结构图如图3-1所示:
图3-1 学生选课系统功能结构图
学生选课系统的详细设计
(1)选课系统总体功能展示如图3-2所示:
图3-2总体功能展示图
(2)数据库设计如图3-3所示:
图3-3数据库设计展示图
(3)选课系统的实现
我主要负责的是数据库表的设计(navicat建表)连接数据库,以及登陆界面(LogOnFrm)的实现以及一部分后端逻辑层的代码,如数据库工具类(DbUtil)、字符工具类(StringUtil)、课程类(Course)等,代码如下:
(1)LogOnFrm类
packagecom.java.view;importjava.awt.EventQueue;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.Connection;importjavax.swing.ButtonGroup;importjavax.swing.GroupLayout;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JFormattedTextField;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPasswordField;importjavax.swing.JRadioButton;importjavax.swing.UIManager;importjavax.swing.WindowConstants;importjavax.swing.plaf.FontUIResource;importcom.java.dao.LogOnDao;importcom.java.model.Admin;importcom.java.model.Student;importcom.java.util.DbUtil;importcom.java.util.StringUtil;//登录界面publicclassLogOnFrmextendsJFrame{/**
*
*/privatestaticfinallong serialVersionUID =1L;//私有成员变量privateButtonGroup buttonGroup1;privateJLabel jLabel1;privateJLabel jLabel2;privateJLabel jLabel3;privateJButton jb_logOn;privateJButton jb_reset;privateJRadioButton jrb_admin;privateJRadioButton jrb_student;privateJPasswordField passwordTxt;privateJFormattedTextField userNameTxt;DbUtil dbUtil =newDbUtil();LogOnDao logOnDao =newLogOnDao();publicstaticStudent currentStudent ;//构造方法publicLogOnFrm(){//改变系统默认字体Font font =newFont("Dialog",Font.PLAIN,12);@SuppressWarnings("rawtypes")java.util.Enumeration keys =UIManager.getDefaults().keys();while(keys.hasMoreElements()){Object key = keys.nextElement();Object value =UIManager.get(key);if(value instanceofFontUIResource){UIManager.put(key, font);}}//调用放置组件方法initComponents();//设置在windows系统中默认居于屏幕正中this.setLocationRelativeTo(null);this.jrb_student.setSelected(true);}//添加组件方法privatevoidinitComponents(){//创建组件
buttonGroup1 =newButtonGroup();
jLabel1 =newJLabel();//“学生选课系统 ”大字 面板
jLabel2 =newJLabel();//账户
userNameTxt =newJFormattedTextField();//用户账号输入框
jLabel3 =newJLabel();//密码
passwordTxt =newJPasswordField();//用户密码输入框
jrb_student =newJRadioButton();//学生复选按钮
jrb_admin =newJRadioButton();//管理员复选按钮
jb_logOn =newJButton();//登录按钮
jb_reset =newJButton();//重置按钮//设置在windows系统中默认居于屏幕正中setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setTitle("用户登录");setResizable(false);//设置字体
jLabel1.setFont(newFont("隶书",1,24));//jLabel1设置图片
jLabel1
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/sys.png")));//jLabel1设置文字
jLabel1.setText("学生选课系统");
jLabel2
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/userName.png")));
jLabel2.setText("账户");
jLabel3
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/password.png")));
jLabel3.setText("密码");//buttonGroup1添加学生按钮
buttonGroup1.add(jrb_student);
jrb_student.setText("学生");
jrb_student
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/student.png")));//buttonGroup1添加管理员按钮
buttonGroup1.add(jrb_admin);
jrb_admin.setText("管理员");
jrb_admin
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/admin.png")));
jb_logOn
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/login.png")));
jb_logOn.setText("登录 ");//用内部类的方式为登录按钮注册监听器
jb_logOn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent evt){jb_logOnActionPerformed(evt);}});
jb_reset
.setIcon(newImageIcon(this.getClass().getResource("/com/java/view/image/reset.png")));
jb_reset.setText("重置 ");//用内部类的方式为重置按钮注册监听器
jb_reset.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent evt){jb_resetActionPerformed(evt);}});//创建GroupLayout布局管理器GroupLayout layout =newGroupLayout(getContentPane());getContentPane().setLayout(layout);//设置沿水平轴确定组件的位置和大小
layout.setHorizontalGroup(layout
.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
layout
.createSequentialGroup().addGap(106,106,106).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(jLabel1).addGroup(
layout
.createSequentialGroup().addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING,
layout
.createSequentialGroup().addComponent(
jLabel3).addGap(33,33,33)).addGroup(
layout
.createSequentialGroup().addComponent(
jLabel2).addGap(33,33,33))).addGap(6,6,6).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.TRAILING,false).addComponent(
passwordTxt,0,0,Short.MAX_VALUE).addComponent(
userNameTxt,GroupLayout.DEFAULT_SIZE,135,Short.MAX_VALUE).addGroup(GroupLayout.Alignment.LEADING,
layout
.createSequentialGroup().addGroup(
layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(
jb_logOn).addComponent(
jrb_student)).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.LEADING,false).addGroup(
layout
.createSequentialGroup().addGap(29,29,29).addComponent(
jb_reset)).addGroup(
layout
.createSequentialGroup().addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
jrb_admin,GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))))))).addContainerGap(143,Short.MAX_VALUE)));//设置沿垂直轴确定组件的位置和大小
layout.setVerticalGroup(layout
.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING,
layout
.createSequentialGroup().addContainerGap(77,Short.MAX_VALUE).addComponent(jLabel1).addGap(39,39,39).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(
userNameTxt,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(
passwordTxt,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(
jrb_student).addComponent(jrb_admin)).addGap(32,32,32).addGroup(
layout
.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jb_reset).addComponent(jb_logOn)).addGap(58,58,58)));pack();//依据放置的组件设定窗口的大小,使窗口恰好容纳所有的组件}//登录行为方法privatevoidjb_logOnActionPerformed(ActionEvent evt){//获取文本框中的信息String userName = userNameTxt.getText();String password =newString(passwordTxt.getPassword());//关于文本框中输入为空的各种情况的判断if(StringUtil.isEmpty(userName)){JOptionPane.showMessageDialog(this,"账号不能为空!");return;}if(StringUtil.isEmpty(password)){JOptionPane.showMessageDialog(this,"密码不能为空!");return;}//创建一个连接Connection con=null;if(this.jrb_student.isSelected()){Student student =newStudent(Integer.parseInt(userName), password);try{
con=dbUtil.getCon();
currentStudent = logOnDao.login(con,
student);if(currentStudent !=null){this.dispose();newMainFrm_student().setVisible(true);}else{JOptionPane.showMessageDialog(this,"用户名或密码错误!");}}catch(Exception e){// TODO Auto-generated catch block
e.printStackTrace();JOptionPane.showMessageDialog(this,"登录失败!");}finally{try{
dbUtil.closeCon(con);}catch(Exception e){// TODO Auto-generated catch block
e.printStackTrace();}}}elseif(this.jrb_admin.isSelected()){Admin admin =newAdmin(Integer.parseInt(userName), password);try{
con=dbUtil.getCon();Admin currentAdmin = logOnDao.login(con, admin);if(currentAdmin !=null){this.dispose();newMainFrm_admin().setVisible(true);}else{JOptionPane.showMessageDialog(this,"用户名或密码错误!");}}catch(Exception e){// TODO Auto-generated catch block
e.printStackTrace();JOptionPane.showMessageDialog(this,"登录失败!");}finally{try{
dbUtil.closeCon(con);}catch(Exception e){// TODO Auto-generated catch block
e.printStackTrace();}}}}//重置行为方法privatevoidjb_resetActionPerformed(ActionEvent evt){this.resetValue();}//重置的清空文本框方法privatevoidresetValue(){this.userNameTxt.setText("");this.passwordTxt.setText("");this.jrb_student.setSelected(true);//默认复选框选择学生}//主函数publicstaticvoidmain(String args[]){EventQueue.invokeLater(newRunnable(){publicvoidrun(){newLogOnFrm().setVisible(true);}});}}
效果部分展示
源码
链接:https://pan.baidu.com/s/18yJygTEMutRBHvxslL8VsA
提取码:sxk9
版权归原作者 champion564 所有, 如有侵权,请联系我们删除。