使用eclipse设计学生管理系统

1.下载安装eclipse,建立一个java工程(File>>New>>Java Project>>Project Name: StudentManager)

2.在src中建立一个view包,来保存视图工程(src右击>>New>>Package>>Name:view)

3.使用可视化设计器和布局工具来创建简单的Windows窗体进行登录页面设计,右击view>>New>>Other>>WindowBuilder>>Swing Design>>JFrame,Next>>Name:loginframe

4.可视化设计登录页面:

1)点击design进入可视化编辑页面:

(关于在eclipse中中文汉字乱码的解决方式   明礼馨德 博客)

2)在Class javax.swing.JFrame 的title中输入该界面名称:学生管理系统

3) 在contentPane>>Layout>>选择GroupLaout,如此我们可以自由进行布局

4)我们需要一个标题,在components中选择JLable来添加一个文本框,在text一栏中输入显示的文本:学生管理系统重复

5)重复上述步骤,我们还需建立三个文本框,分别重命名为:lable lable_1 lable_2内容分别为:  用户名: 密码: 用户类型: ,此时我们会发现一个问题,由于文本的长度不同,所以他们是没有对齐的,非常不美观,所以我们要对他们进行处理,首先我们想到的方法是直接在文本内容中加空格,但是这中方法添加空格非常不稳定,细微上不容易对齐,其次我们会想到回到Source界面,使用十六进制输入汉字,然后在其中添加空格,相对容易但比较麻烦,我们这里采用第三种方法,右对齐,即使签中的文字在标签里面右对齐,JLabel a=new JLabel("用户名",JLabel.RIGHT)然后使用适当的空格进行填充。GBK中一个汉字占2个字节 空格占1个字节 ;UTF-8中一个汉字占3个字节一个空格占1个字节

6)添加两个文本输入框

在components中选择JTextField,调整合适的大小,建立两个文本输入框,分别重命名为:usernametextField passwordtextField

7)添加组合框

在components中选择JCombox(组合框 :显示一个可编辑的文本框和下拉列表,用户可以从下拉列表中选择一个或多个值。用户可以直接输入文本到列表的顶部,或者从列表中选择一个或多个现成的值。),调整适当大小,重命名为:usertypecomboBox(在这里解释一下重命名的作用,这是为了方便我们后期再Source界面进行调试的时候,方便快捷的知道该模块的代码是那一部分的,防止把用户名的输入框当成密码的输入框这种类似的错误发生),在model中输入系统管理员 换行 教师 换行 学生 换行 三个选择 ,注意在后期我们会建立枚举类来替代这三个选择,此时输入最主要是为了在source界面中产生相应的代码

8)添加两个按钮

在components中选择JButton,重命名为:loginbutton  resetbutton , text中分别输入 登录  重置

如此一个简易的登录页面算完成了:

5.设计主页面

过程与设计登录页面过程基本相似

1)新建mainframe,进入design页面

2)调整适合的框图大小

3)添加菜单栏

在Menu中选择JMenuBar

4)在菜单栏中添加两个菜单

在JMenuBar中添加两个JMenu,分别重命名为:menu  menu_1,text中分别输入系统设置,班级管理

5)添加菜单项目

在JMenu中添加JMenuItem,此次我们需要在系统设置里添加两选项:修改密码 退出系统 在班级管理中添加一个选项:添加班级,然后依次进行重命名:menuItem menuItem_1 menuItem_2,然后再text中添加文字描述

6)添加虚拟界面

在Container中添加JDesktopPane(用于创建多文档界面或虚拟桌面的容器)。为了方便区分,我们给它加上背景颜色

6.设计修改密码页面

1)新建editpasswordframe,进入design页面

new>>others>>JInternalFrame

2)调整适合的框图大小

3)添加五个文本框,分别为:   当前用户:     原密码: 新密码: 确认密码:  空白文本框(在当前用户:后面)分别重命名为:label_3   label    label_1    label_2    currentUserLabel

4)添加三个文本输入框,分别重命名为:oldpwdtextField   newpwdtextField  confirmpwdField按钮

5)添加两个按钮,分别重命名为:submitbutton resetbutton  ,在text中分别输入 确定  重置

到目前为止,三个可视化窗口的设计算是基本完成了

7.将登陆页面窗口置于屏幕中央

显然,我们每次首先要运行的是登录页面,那么在运行时我们就会发现,点击运行后,该弹窗显示在屏幕左上角,我们为了视觉上的舒适感通常会把它拖到屏幕中央,怎样默认让它显示在屏幕中央呢?

我们只需在loginframe.java的public loginframe() {     (setContentPane(contentPane);下放)   中添加一行代码

setLocationRelativeTo(null);//窗口将置于屏幕的中央

就能实现这个功能了。

(public void setLocationRelativeTo(Component c)  设置窗口相对于指定组件的位置。 如果组件当前未显示或者 c 为 null,则此窗口将置于屏幕的中央。  中点可以使用 GraphicsEnvironment.getCenterPoint 确定    披Zhe羊皮De狼 博客)

8.登录页面功能完善

登录按钮和重置按钮的实现

        JButton loginbutton = new JButton("登录");loginbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {login();//登录按钮实现的方法,具体实现在下面}});JButton resetbutton = new JButton("重置");resetbutton.addActionListener(new ActionListener() {//重置按钮的实现public void actionPerformed(ActionEvent arg0) {usernametextField.setText("");passwordtextField.setText("");usertypecomboBox.setSelectedIndex(0);}});......protected void login() {     //我们把该函数放在整体代码最后来实现整体代码的美观String name=usernametextField.getText().toString();String password=passwordtextField.getText().toString();usertype selectItem=(usertype) usertypecomboBox.getSelectedItem();if(stringutil.isEmpty(name)){JOptionPane.showMessageDialog(this, "用户名不能为空");   //弹出提示性对话框return;}if(stringutil.isEmpty(password)){JOptionPane.showMessageDialog(this, "密码不能为空"); return;}Admin admin=null;if("系统管理员".equals(selectItem.getName())){Admindao admindao=new Admindao();Admin admin2=new Admin();admin2.setName(name);admin2.setPassword(password);admin=admindao.login(admin2);if(admin==null){JOptionPane.showMessageDialog(this, "用户名或者密码错误");return;}this.dispose();//当前登录界面关闭new mainframe(selectItem,admin).setVisible(true);//主页面显示}

此时会出现许多小问题,不要慌,我们去逐个破解,首先是

usertype selectItem=(usertype) usertypecomboBox.getSelectedItem();

这是因为我们把 usertypecomboBox定义在了public loginframe()中,而在protected void login()不包含它的定义所导致的,因此我们只需要把它定义在最外层public class loginframe extends JFrame  中就可以了,同时记得修改public loginframe()中的定义,避免重复定义。

public class loginframe extends JFrame {private JPanel contentPane;private JTextField usernametextField;private JTextField passwordtextField;private JComboBox usertypecomboBox ;    //...public loginframe() {...usertypecomboBox = new JComboBox();usertypecomboBox.setModel(new DefaultComboBoxModel(new String[] {"系统管理员", "教师", "学生"}));

接下来的问题也主要是元素没有定义,这是因为本人发布为最终代码,其中部分优化加入了其他的包和元素定义,在这里我带领大家一一加入。

1)在原有view包的基础上建立四个包分别为  dao(存储于数据库有关的代码) images(存放图片) model(存放枚举类等)util(存放连接和判断条件)

2)在model中新建枚举类usertype    (如何自动生成代码模板问题)

model>>new>>Enum

package model;public enum usertype {ADMIN("系统管理员",0),TEACHER("教师",1),STUDENT("学生",2);private String name;private int index;private usertype(String name,int index){this.name = name;this.index = index;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}@Overridepublic String toString(){return this.name;}}

在loginframe中引入枚举类

 usertypecomboBox.setModel(new DefaultComboBoxModel(new usertype[] {usertype.ADMIN, usertype.TEACHER, usertype.STUDENT}));//用户类型的值为枚举类中设定的

3)在util中生成stringutil

util>>new>>class

package util;
/** 判断字符串是否为空*/
public class stringutil {public static boolean isEmpty(String str){if("".equals(str)|| str == null){return true;}return false;}
}

4)在model中新建Admin 类

model>>new>>class

package model;public class Admin {private int id;private String name;private String password;private String createdate;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getCreatedate() {return createdate;}public void setCreatedate(String createdate) {this.createdate = createdate;}}

5)在dao中新建Admindao

dao>>new>>class

package dao;import java.sql.SQLException;import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;import model.Admin;public class Admindao extends Basedao {/*管理员登录,查询用户名和密码,是否和数据库中的匹配* */public Admin login(Admin admin) {String sql="select * from admin where name=? and password=?";Admin admin2=null;try {PreparedStatement preparedStatement=(PreparedStatement) con.prepareStatement(sql);preparedStatement.setString(1, admin.getName());preparedStatement.setString(2, admin.getPassword());ResultSet resultSet=(ResultSet) preparedStatement.executeQuery();if(resultSet.next()){admin2=new Admin();admin2.setId(resultSet.getInt("id"));admin2.setName(resultSet.getString("name"));admin2.setPassword(resultSet.getString("password"));admin2.setCreatedate(resultSet.getString("createdate")); }} catch (SQLException e) {// TODO: handle exception}return admin2;}/** 修改密码,先查询旧密码,再更新新密码*/public String editPassword(Admin admin,String newPassword){String sql = "select * from admin where id=? and password=?";PreparedStatement prst = null;int id = 0;try {prst = (PreparedStatement) con.prepareStatement(sql);prst.setInt(1, admin.getId());prst.setString(2, admin.getPassword());ResultSet executeQuery = (ResultSet) prst.executeQuery();if(!executeQuery.next()){String retString = "旧密码错误!";return retString;}id = executeQuery.getInt("id");} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String retString = "修改失败";String sqlString = "update admin set password = ? where id = ?";try {prst = (PreparedStatement) con.prepareStatement(sqlString);prst.setString(1, newPassword);prst.setInt(2, id);int rst = prst.executeUpdate();if(rst > 0){retString = "密码修改成功!";}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return retString;}
}

6)导入jar包

mysql-connector-java-5.0.4-bin (附件)

新建lib文件夹,复制粘贴入该jar包,build path  >>ALL

7)在dao包中新建Basedao

dao>>new>>Basedao

package dao;import java.sql.Connection;
import java.sql.SQLException;import util.Dbutil;public class Basedao {
//创建数据库连接对象public Connection con=new Dbutil().getCon();public void closeDao() {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

8)在util中新建DButil

util>>new>>DButil

package util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class Dbutil {private String dbUrl="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址private String dbUserName="root"; // 用户名  此处用户名和密码均为举例,需要实际输入你电脑 // 上的数据库名和密码等private String dbPassword="root"; // 密码private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称/*** 获取数据库连接* @return* @throws Exception*/public Connection getCon(){try {Class.forName(jdbcName);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Connection con = null;try {con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}/*** 关闭数据库连接* @param con* @throws Exception*/public void closeCon(Connection con)throws Exception{if(con!=null){con.close();}}public static void main(String[] args) {Dbutil dbUtil=new Dbutil();try {dbUtil.getCon();System.out.println("数据库连接成功!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("数据库连接失败");}}
}

9.完善mainframe.java

1)定义元素

public class mainframe extends JFrame {private JPanel contentPane;public static usertype usertype;public static Object userobject;private JDesktopPane desktopPane;
...

2)注销原有的样式

//   public static void main(String[] args) {
//      EventQueue.invokeLater(new Runnable() {
//          public void run() {
//              try {
//                  mainframe frame = new mainframe();
//                  frame.setVisible(true);
//              } catch (Exception e) {
//                  e.printStackTrace();
//              }
//          }
//      });
//  }

3)

public mainframe(usertype usertype,Object userObject) {this.usertype=usertype;this.userobject=userObject;...setLocationRelativeTo(null);
....

4)给修改密码添加一个监听事件

menuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {editpassword(ae);}});

5)给退出系统添加一个监听事件

menuItem_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {if(JOptionPane.showConfirmDialog(mainframe.this, "确定要退出吗?")==JOptionPane.OK_OPTION){System.exit(0);}}});

6)给添加班级添加一个监听事件

menuItem_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {
//              //addclassframe addclassframe=new addclassframe();
//              addclassframe.setVisible(true);
//              desktopPane.add(addclassframe);}});

7)定义 editpassword

protected void editpassword(ActionEvent ae) {// TODO Auto-generated method stubeditpasswordframe editpasswordframe = new editpasswordframe();editpasswordframe.setVisible(true);desktopPane.add(editpasswordframe);}

10.完善editpassword.java

1)定义lable

public class editpasswordframe extends JFrame {private JPanel contentPane;private JTextField oldpwdtextField;private JTextField newpwdtextField;private JTextField confirmpwdField;private JLabel label_1;private JLabel label_2;private JLabel currentUserLabel;

2)同样注销原有界面

//   public static void main(String[] args) {
//      EventQueue.invokeLater(new Runnable() {
//          public void run() {
//              try {
//                  editpasswordframe frame = new editpasswordframe();
//                  frame.setVisible(true);
//              } catch (Exception e) {
//                  e.printStackTrace();
//              }
//          }
//      });
//  }

3)设置窗口关闭,设置最小化窗口

        setClosable(true);//设置窗口关闭setIconifiable(true);//设置最小化的窗口

4)为确认按钮添加监听事件

submitbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {submit(e);//确认按钮的监听实现方法,具体实现在下面}});

5)为重置按钮添加监听事件

resetbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {resetvalue(ae);//重置按钮的监听实现方法,具体实现在下面}});

6)获取当前用户名

        /** 获取当前登录的用户名字*/if("系统管理员".equals(mainframe.usertype.getName())){Admin admin=(Admin) mainframe.userobject;currentUserLabel.setText("【系统管理员】"+admin.getName());}}

7)定义确定事件和重置事件

protected void submit(ActionEvent e) {// TODO Auto-generated method stubString oldpwd=oldpwdtextField.getText().toString();String newpwd=newpwdtextField.getText().toString();String confirmpwd=confirmpwdtextField.getText().toString();if(stringutil.isEmpty(oldpwd)){JOptionPane.showMessageDialog(this, "旧密码不能为空");return;}if(stringutil.isEmpty(newpwd)){JOptionPane.showMessageDialog(this, "新密码不能为空");return;}if(stringutil.isEmpty(confirmpwd)){JOptionPane.showMessageDialog(this, "确认密码不能为空");return;}if(!newpwd.equals(confirmpwd)){JOptionPane.showMessageDialog(this, "两次密码输入不一致");return;}if("系统管理员".equals(mainframe.usertype.getName())){Admindao adminDao = new Admindao();Admin adminTmp = new Admin();Admin admin = (Admin)mainframe.userobject;adminTmp.setName(admin.getName());adminTmp.setId(admin.getId());adminTmp.setPassword(oldpwd);JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp,newpwd));adminDao.closeDao();return;}}protected void resetvalue(ActionEvent ae) {// TODO Auto-generated method stuboldpwdtextField.setText("");newpwdtextField.setText("");confirmpwdField.setText("");}

全部代码:

package dao;import java.sql.SQLException;import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;import model.Admin;public class Admindao extends Basedao {/*管理员登录,查询用户名和密码,是否和数据库中的匹配* */public Admin login(Admin admin) {String sql="select * from admin where name=? and password=?";Admin admin2=null;try {PreparedStatement preparedStatement=(PreparedStatement) con.prepareStatement(sql);preparedStatement.setString(1, admin.getName());preparedStatement.setString(2, admin.getPassword());ResultSet resultSet=(ResultSet) preparedStatement.executeQuery();if(resultSet.next()){admin2=new Admin();admin2.setId(resultSet.getInt("id"));admin2.setName(resultSet.getString("name"));admin2.setPassword(resultSet.getString("password"));admin2.setCreatedate(resultSet.getString("createdate")); }} catch (SQLException e) {// TODO: handle exception}return admin2;}/** 修改密码,先查询旧密码,再更新新密码*/public String editPassword(Admin admin,String newPassword){String sql = "select * from admin where id=? and password=?";PreparedStatement prst = null;int id = 0;try {prst = (PreparedStatement) con.prepareStatement(sql);prst.setInt(1, admin.getId());prst.setString(2, admin.getPassword());ResultSet executeQuery = (ResultSet) prst.executeQuery();if(!executeQuery.next()){String retString = "旧密码错误!";return retString;}id = executeQuery.getInt("id");} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String retString = "修改失败";String sqlString = "update admin set password = ? where id = ?";try {prst = (PreparedStatement) con.prepareStatement(sqlString);prst.setString(1, newPassword);prst.setInt(2, id);int rst = prst.executeUpdate();if(rst > 0){retString = "密码修改成功!";}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return retString;}
}
package dao;import java.sql.Connection;
import java.sql.SQLException;import util.Dbutil;public class Basedao {
//创建数据库连接对象public Connection con=new Dbutil().getCon();public void closeDao() {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
package model;public class Admin {private int id;private String name;private String password;private String createdate;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getCreatedate() {return createdate;}public void setCreatedate(String createdate) {this.createdate = createdate;}}
package model;public enum usertype {ADMIN("系统管理员",0),TEACHER("教师",1),STUDENT("学生",2);private String name;private int index;private usertype(String name,int index){this.name = name;this.index = index;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}@Overridepublic String toString(){return this.name;}}
package util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class Dbutil {private String dbUrl="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址private String dbUserName="root"; // 用户名private String dbPassword="root"; // 密码private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称/*** 获取数据库连接* @return* @throws Exception*/public Connection getCon(){try {Class.forName(jdbcName);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Connection con = null;try {con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}/*** 关闭数据库连接* @param con* @throws Exception*/public void closeCon(Connection con)throws Exception{if(con!=null){con.close();}}public static void main(String[] args) {Dbutil dbUtil=new Dbutil();try {dbUtil.getCon();System.out.println("数据库连接成功!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("数据库连接失败");}}
}
package util;
/** 判断字符串是否为空*/
public class stringutil {public static boolean isEmpty(String str){if("".equals(str)|| str == null){return true;}return false;}
}
package view;import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;import dao.Admindao;
import model.Admin;
import util.stringutil;public class editpasswordframe extends JInternalFrame {private JPanel contentPane;private JLabel label_1;private JLabel currentUserLabel;private JTextField newpwdtextField;private JLabel label_2;private JTextField confirmpwdtextField;private JTextField oldpwdtextField;/*** Launch the application.*/
//  public static void main(String[] args) {
//      EventQueue.invokeLater(new Runnable() {
//          public void run() {
//              try {
//                  editpasswordframe frame = new editpasswordframe();
//                  frame.setVisible(true);
//              } catch (Exception e) {
//                  e.printStackTrace();
//              }
//          }
//      });
//  }/*** Create the frame.*/public editpasswordframe() {setTitle("\u7BA1\u7406\u5458\u4FEE\u6539\u5BC6\u7801");//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 626, 500);setClosable(true);//设置窗口关闭setIconifiable(true);//设置最小化的窗口contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JLabel label = new JLabel("\u539F\u5BC6\u7801\uFF1A");label.setFont(new Font("宋体", Font.PLAIN, 25));label_1 = new JLabel("\u65B0\u5BC6\u7801\uFF1A");label_1.setFont(new Font("宋体", Font.PLAIN, 25));newpwdtextField = new JTextField();newpwdtextField.setColumns(10);label_2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");label_2.setFont(new Font("宋体", Font.PLAIN, 25));confirmpwdtextField = new JTextField();confirmpwdtextField.setColumns(10);oldpwdtextField = new JTextField();oldpwdtextField.setColumns(10);JButton submitbutton = new JButton("\u786E\u8BA4");submitbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {submit(e);//确认按钮的监听实现方法,具体实现在下面}});submitbutton.setFont(new Font("宋体", Font.PLAIN, 24));JButton resetbutton = new JButton("\u91CD\u7F6E");resetbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {resetvalue(ae);//重置按钮的监听实现方法,具体实现在下面}});resetbutton.setFont(new Font("宋体", Font.PLAIN, 24));JLabel label_3 = new JLabel("\u5F53\u524D\u7528\u6237\uFF1A");label_3.setFont(new Font("宋体", Font.PLAIN, 25));currentUserLabel = new JLabel("");//currentUserLabel.setEnabled(false);GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane.createSequentialGroup().addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane.createSequentialGroup().addContainerGap().addComponent(submitbutton).addGap(83).addComponent(resetbutton).addGap(36)).addGroup(gl_contentPane.createSequentialGroup().addGap(109).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(label_1).addComponent(label).addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false).addComponent(label_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(label_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))).addGap(13).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(1).addComponent(confirmpwdtextField, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(newpwdtextField, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(currentUserLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(oldpwdtextField, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)))))).addContainerGap(152, GroupLayout.PREFERRED_SIZE)));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(37).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(label_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).addComponent(currentUserLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)).addGap(34).addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(label).addComponent(oldpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)).addGap(47).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(label_1).addComponent(newpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)).addGap(52).addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(label_2).addComponent(confirmpwdtextField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)).addPreferredGap(ComponentPlacement.RELATED, 65, Short.MAX_VALUE).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(submitbutton, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE).addComponent(resetbutton)).addGap(34)));contentPane.setLayout(gl_contentPane);/** 获取当前登录的用户名字*/if("系统管理员".equals(mainframe.usertype.getName())){Admin admin=(Admin) mainframe.userobject;currentUserLabel.setText("【系统管理员】"+admin.getName());}}protected void submit(ActionEvent e) {// TODO Auto-generated method stubString oldpwd=oldpwdtextField.getText().toString();String newpwd=newpwdtextField.getText().toString();String confirmpwd=confirmpwdtextField.getText().toString();if(stringutil.isEmpty(oldpwd)){JOptionPane.showMessageDialog(this, "旧密码不能为空");return;}if(stringutil.isEmpty(newpwd)){JOptionPane.showMessageDialog(this, "新密码不能为空");return;}if(stringutil.isEmpty(confirmpwd)){JOptionPane.showMessageDialog(this, "确认密码不能为空");return;}if(!newpwd.equals(confirmpwd)){JOptionPane.showMessageDialog(this, "两次密码输入不一致");return;}if("系统管理员".equals(mainframe.usertype.getName())){Admindao adminDao = new Admindao();Admin adminTmp = new Admin();Admin admin = (Admin)mainframe.userobject;adminTmp.setName(admin.getName());adminTmp.setId(admin.getId());adminTmp.setPassword(oldpwd);JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp,newpwd));adminDao.closeDao();return;}}protected void resetvalue(ActionEvent ae) {// TODO Auto-generated method stuboldpwdtextField.setText("");newpwdtextField.setText("");confirmpwdtextField.setText("");}
}
package view;import java.awt.BorderLayout;
import java.awt.EventQueue;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;import dao.Admindao;
import model.Admin;
import model.usertype;
import util.stringutil;import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;public class loginframe extends JFrame {private JPanel contentPane;private JTextField usernametextField;private JTextField passwordtextField;private JComboBox usertypecomboBox ;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {loginframe frame = new loginframe();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public loginframe() {setTitle("学生管理系统");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 587, 453);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);setLocationRelativeTo(null);//窗口将置于屏幕的中央JLabel lblNewLabel = new JLabel("学生管理系统");lblNewLabel.setFont(new Font("华文楷体", Font.PLAIN, 25));JLabel label = new JLabel("用 户 名:",JLabel.RIGHT);JLabel lable_1 = new JLabel("密    码:",JLabel.RIGHT);JLabel lable_2 = new JLabel("用户类型:",JLabel.RIGHT);usernametextField = new JTextField();usernametextField.setColumns(10);passwordtextField = new JTextField();passwordtextField.setColumns(10);usertypecomboBox = new JComboBox();usertypecomboBox.setModel(new DefaultComboBoxModel(new usertype[] {usertype.ADMIN, usertype.TEACHER, usertype.STUDENT}));//用户类型的值为枚举类中设定的JButton loginbutton = new JButton("登录");loginbutton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {login();//登录按钮实现的方法,具体实现在下面}});JButton resetbutton = new JButton("重置");resetbutton.addActionListener(new ActionListener() {//重置按钮的实现public void actionPerformed(ActionEvent arg0) {usernametextField.setText("");passwordtextField.setText("");usertypecomboBox.setSelectedIndex(0);}});GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(210).addComponent(lblNewLabel)).addGroup(gl_contentPane.createSequentialGroup().addGap(85).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false).addComponent(lable_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(lable_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addGap(18).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false).addComponent(usertypecomboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(usernametextField).addComponent(passwordtextField, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))).addContainerGap(153, Short.MAX_VALUE)).addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addGap(195).addComponent(loginbutton, 0, 0, Short.MAX_VALUE).addGap(49).addComponent(resetbutton, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE).addGap(183)));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addGap(30).addComponent(lblNewLabel).addGap(43).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(usernametextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(35).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lable_1).addComponent(passwordtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(58).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lable_2).addComponent(usertypecomboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGap(37).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(loginbutton).addComponent(resetbutton)).addGap(15)));contentPane.setLayout(gl_contentPane);}      protected void login() {String name=usernametextField.getText().toString();String password=passwordtextField.getText().toString();usertype selectItem=(usertype) usertypecomboBox.getSelectedItem();if(stringutil.isEmpty(name)){JOptionPane.showMessageDialog(this, "用户名不能为空");   //弹出提示性对话框return;}if(stringutil.isEmpty(password)){JOptionPane.showMessageDialog(this, "密码不能为空"); return;}Admin admin=null;if("系统管理员".equals(selectItem.getName())){Admindao admindao=new Admindao();Admin admin2=new Admin();admin2.setName(name);admin2.setPassword(password);admin=admindao.login(admin2);if(admin==null){JOptionPane.showMessageDialog(this, "用户名或者密码错误");return;}this.dispose();//当前登录界面关闭new mainframe(selectItem,admin).setVisible(true);//主页面显示}}
}
package view;import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;import model.usertype;import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JDesktopPane;
import java.awt.Color;public class mainframe extends JFrame {private JPanel contentPane;public static usertype usertype;public static Object userobject;private JDesktopPane desktopPane;/*** Launch the application.*/
//  public static void main(String[] args) {
//      EventQueue.invokeLater(new Runnable() {
//          public void run() {
//              try {
//                  mainframe frame = new mainframe();
//                  frame.setVisible(true);
//              } catch (Exception e) {
//                  e.printStackTrace();
//              }
//          }
//      });
//  }/*** Create the frame.*/public mainframe(usertype usertype,Object userObject) {this.usertype=usertype;this.userobject=userObject;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 925, 707);setLocationRelativeTo(null);JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);JMenu menu = new JMenu("系统设置");menuBar.add(menu);JMenuItem menuItem = new JMenuItem("修改密码");menuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {editpassword(ae);}});menu.add(menuItem);JMenuItem menuItem_1 = new JMenuItem("退出系统");menuItem_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {if(JOptionPane.showConfirmDialog(mainframe.this, "确定要退出吗?")==JOptionPane.OK_OPTION){System.exit(0);}}});menu.add(menuItem_1);JMenu menu_1 = new JMenu("班级管理");menuBar.add(menu_1);JMenuItem menuItem_2 = new JMenuItem("添加班级");menuItem_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {
//              //addclassframe addclassframe=new addclassframe();
//              addclassframe.setVisible(true);
//              desktopPane.add(addclassframe);}});menu_1.add(menuItem_2);contentPane = new JPanel();contentPane.setBackground(new Color(173, 216, 230));contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));contentPane.setLayout(new BorderLayout(0, 0));setContentPane(contentPane);JDesktopPane desktopPane = new JDesktopPane();desktopPane.setBackground(Color.CYAN);contentPane.add(desktopPane, BorderLayout.NORTH);}protected void editpassword(ActionEvent ae) {// TODO Auto-generated method stubeditpasswordframe editpasswordframe = new editpasswordframe();editpasswordframe.setVisible(true);desktopPane.add(editpasswordframe);}
}

使用eclipse设计学生管理系统相关推荐

  1. 用python设计学生管理系统_python+tkinter实现学生管理系统

    本文实例为大家分享了python+tkinter实现学生管理系统的具体代码,供大家参考,具体内容如下 from tkinter import * from tkinter.messagebox imp ...

  2. 用python设计学生管理系统_Python实现GUI学生信息管理系统

    本文实例为大家分享了Python实现GUI学生信息管理系统的具体代码,供大家参考,具体内容如下 项目环境: 软件环境: OS:RedHat6.3 Lib:Pygtk Language:Python S ...

  3. 用python设计学生管理系统_基于python和tkinter实现的一个简单的学生信息管理系统...

    一个简单的学生信息管理系统基于python和tkinter 1.需求分析1.大学生信息管理系统使用tkinter接口创建一个窗口.使界面更漂亮.2. 实现与数据库的连接,教师和学生的信息可以保存或读取 ...

  4. 数据库设计-学生管理系统数据库系统

    数据库系统设计综合实验 注: 这一篇的代码是我看着视频(很详细)一步一步打出来.其中在C#的设计,看到代码也无用,主要那个设计过程才是比较重要的,所以我顺便也把视频链接发上来了,跟着视频一步一步来更方 ...

  5. 参考别人博客,自己实现用idea运行eclipse项目--学生管理系统-

    别人的博客:https://blog.csdn.net/zeal9s/article/details/90690524 我自己录制的视频: 百度网盘:链接:https://pan.baidu.com/ ...

  6. 采用三层架构(JAVA)设计学生管理系统

    0.前提工作安装好MySQL数据库 1.根据三层架构规范项目 2.分析数据库 !!!强烈建议复制,要不容易出现错误(语句中的大小写没有关系,use或者USE,但是数据库名要一直) 数据库名:stude ...

  7. 用python设计学生管理系统_Django实现学生管理系统实例分享

    首页 添加 {% block content %} {% endblock %}

  8. Java-GUI编程实战之管理系统 Day3【学生管理系统GUI设计、学生管理系统增删改查、项目MVC结构介绍、Java技能图谱】

    视频.课件.源码[链接:https://pan.baidu.com/s/13ffqGDzH-DZib6-MFViW3Q 提取码:zjxs] Java-GUI编程实战之管理系统 Day1[项目开发流程. ...

  9. 学生管理系统c#语言代码,基于C#语言的学生管理系统的设计(ASP.NET2.0)

    摘  要 随着科学技术的不断提高,计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用.学生管理系统是学校管理中不可少的一部分.而基于B/S架构的学生管 ...

最新文章

  1. 北京大学AI写作机器人来了,会替代记者?
  2. python列表的索引算法_Python-确定列表是否对称的算法
  3. Gradle自定义插件
  4. 关于CSS的fixed定位
  5. 别折腾安全软件了 你的手机也许还不配被黑客破解
  6. 用python计算工程量_基于Python脚本程序的电缆工程量快速统计方法与流程
  7. Linux LAMP架构介绍及配置
  8. Http权威指南笔记(三)——HTTP报文
  9. oracle查询表锁定以及解锁方法
  10. 电商项目实战之支付宝支付订单
  11. java咖啡馆_Java咖啡馆(11):Java插件技术
  12. vmware虚拟机序列号
  13. 基于pytorch的双模态数据载入
  14. SAP FICO 财务月结--自动清账
  15. 自适应滤波(LMS,RLS)
  16. 安装Linux详细教程
  17. Java祝福生日快乐小程序
  18. 本地项目连接虚拟机的数据库oracle
  19. websocket菜鸟教程(1.1)
  20. 周杰伦入局元宇宙?6200万个联名「幻象熊」40分钟全卖光

热门文章

  1. App A 唤起 App B 并传参,AppB在登录之后的页面,无论哪个页面都有一个弹窗。点击确定按钮,唤起App A (传参)
  2. Ubuntu重装回Windows的21个步骤
  3. 虚拟手机服务器,华为发布"鲲鹏云手机":基于云服务器,具有虚拟手机功能...
  4. 【Java文件操作】renameTo()方法,实现文件重命名
  5. [HIS] HIT行业常用名词及缩写定义
  6. Splay入门解析【保证让你看不懂(滑稽)】
  7. 这个外包公司太恶心了。。进去请三思!
  8. Unity XR Interaction Toolkit(一)配置环境
  9. Echarts通过ajax获取数据定时更新轮询的解决方案及问题分析
  10. STM32 外部中断