这篇文章将为大家详细讲解有关怎么在Java中使用Swing与Eclipse实现一个学生成绩管理系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
package com.sjsq.model; import java.util.ArrayList; import java.util.List; /** * 学生实体类 * @author shuijianshiqing * */ public class Student { // 学号 private String sId; // 姓名 private String sName; // 性别 private String sSex; // 学院 private String sCollege; private String sC; private String sMath; private String sEnglish; private String sChinese; private String sJava; public Student() { super(); } public Student(String sId, String sName, String sSex, String sCollege, String sC, String sMath, String sEnglish, String sChinese, String sJava) { super(); this.sId = sId; this.sName = sName; this.sSex = sSex; this.sCollege = sCollege; this.sC = sC; this.sMath = sMath; this.sEnglish = sEnglish; this.sChinese = sChinese; this.sJava = sJava; } public static List<Student> students=new ArrayList<Student>(); public String getsId() { return sId; } public void setsId(String sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsSex() { return sSex; } public void setsSex(String sSex) { this.sSex = sSex; } public String getsCollege() { return sCollege; } public void setsCollege(String sCollege) { this.sCollege = sCollege; } public String getsC() { return sC; } public void setsC(String sC) { this.sC = sC; } public String getsMath() { return sMath; } public void setsMath(String sMath) { this.sMath = sMath; } public String getsEnglish() { return sEnglish; } public void setsEnglish(String sEnglish) { this.sEnglish = sEnglish; } public String getsChinese() { return sChinese; } public void setsChinese(String sChinese) { this.sChinese = sChinese; } public String getsJava() { return sJava; } public void setsJava(String sJava) { this.sJava = sJava; } @Override public String toString() { return "Student [sId=" + sId + ", sName=" + sName + ", sSex=" + sSex + ", sCollege=" + sCollege + ", sC=" + sC + ", sMath=" + sMath + ", sEnglish=" + sEnglish + ", sChinese=" + sChinese + ", sJava=" + sJava + "]"; } }
StudentDao.java
package com.sjsq.dao; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.sjsq.model.Student; import com.sjsq.util.DBUtil; public class StudentDao { public static int progress; // private final String FILE_PATH="d:\\student.xls"; public static StudentDao getInstance() { StudentDao studentDao; return studentDao = new StudentDao(); } // 查询所有学生的信息 public ResultSet queryAll() { ResultSet rs = null; String sql = "select * from Student"; System.out.println("------查询所有学生信息------"); System.out.println("sql语句:" + sql); DBUtil db = new DBUtil(); try { rs = db.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 查询所有学生信息,并且返回List public List<Student> queryAllStudent() { List<Student> listStudent = new ArrayList<Student>(); String sql = "select * from student"; System.out.println("------查询所有学生信息------"); System.out.println("sql语句:" + sql); DBUtil db = new DBUtil(); ResultSet rs = null; try { rs = db.executeQuery(sql); while (rs.next()) { Student studentTmp = new Student(); studentTmp.setsId(rs.getString("sId")); studentTmp.setsName(rs.getString("sName")); studentTmp.setsSex(rs.getString("sSex")); studentTmp.setsCollege(rs.getString("sCollege")); studentTmp.setsC(rs.getString("sC")); studentTmp.setsMath(rs.getString("sMath")); studentTmp.setsEnglish(rs.getString("sEnglish")); studentTmp.setsChinese(rs.getString("sChinese")); studentTmp.setsJava(rs.getString("sJava")); listStudent.add(studentTmp); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } db.close(); } return listStudent; } public static int getprogress() { return progress; } // 按照条件查询 public ResultSet queryByCondition(String[] data) { ResultSet rs = null; String sql = "select * from student where 1=1"; int i = 0; // 学号 if (!(data[0].equals(""))) { sql += " and sid='" + data[0] + "' "; } // 姓名 if (!(data[1].equals(""))) { sql += " and sname='" + data[1] + "' "; } // 省份 if (!(data[2].equals(""))) { sql += " and senglish='" + data[2] + "' "; } // 性别 if (!(data[3].equals(""))) { sql += " and ssex='" + data[3] + "' "; } System.out.println("------查询所有学生信息------"); System.out.println("sql语句:" + sql); DBUtil db = new DBUtil(); try { rs = db.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 插入学生信息,并且返回插入结果 public boolean insertStudentInformation(Student student) { DBUtil DBUtil = new DBUtil(); // 通过student对象设置sid的值 String sid = student.getsId(); String sname = student.getsName(); String ssex = student.getsSex(); String scollege = student.getsCollege(); String sc = student.getsC(); String smath = student.getsMath(); String senglish = student.getsEnglish(); String schinese = student.getsChinese(); String sjava = student.getsJava(); String sql = "insert into student(sid,sname,ssex,scollege,sc,smath,senglish,schinese,sjava)" + "values('" + sid + "','" + sname + "','" + ssex + "','" + scollege+ "','" + sc+ "','" + smath + "','" + senglish + "','" + schinese + "','" + sjava + "')"; System.out.println("------插入学生信息------"); System.out.println("sql语句:" + sql); // 向数据库插入数据语句 // 定义一个boolean型变量,用于判断插入数据是否成功 boolean flag = false; try { // 更新数据库信息 flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } DBUtil.close(); // 返回执行结果 return flag; } // 统计同一学号有多少人 public int queryForsidinformation(String sid) { DBUtil db = new DBUtil(); // 通过sid查询数据库中是否有相同sid数据 String sql = "select * from student where sid=" + sid; System.out.println("------验证学生学号信息------"); System.out.println("sql语句:" + sql); ResultSet rs = null; int count = 0; try { // 更新数据库保存到结果集里 rs = (ResultSet) db.executeQuery(sql); // 找到相同的count++ if (rs.next()) { count++; } } catch (SQLException e) { e.printStackTrace(); } // 关闭数据库连接 db.close(); // 返回count return count; } // 更新学生信息,这里学号不能更新 public boolean updateStudentInformation(Student student) { DBUtil DBUtil = new DBUtil(); // scollege,sc,smath,senglish,schinese,sjava String sql = "update student set sname=" + "'" + student.getsName() + "'" + " ," + " ssex=" + "'" + student.getsSex() + "'" + " ," + " scollege=" + "'" + student.getsCollege() + "' " + " ," + " sc=" + "'" + student.getsC() + "' " + " ," + " smath=" + "'" + student.getsMath() + "' " + " ," + " senglish=" + "'" + student.getsEnglish() + "'" + "," + " schinese=" + "'" + student.getsChinese() + "'" + " ," + " sjava=" + "'" + student.getsJava()+ "'" + " where" + " sid=" + "'"+ student.getsId() + "'"; System.out.println("------更新学生信息------"); System.out.println("sql语句:" + sql); boolean flag = false; try { flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return flag; } // 删除学生信息 public boolean deleteStudentInfoBySid(String sid) { boolean flag = false; String sql = "delete from student where sid=" + "'" + sid + "'"; System.out.println("------删除学生信息------"); System.out.println("sql语句:" + sql); DBUtil DBUtil = new DBUtil(); try { flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return flag; } }
LoginFrame.java
package com.sjsq.view; import org.jb2011.lnf.beautyeye.ch4_button.BEButtonUI; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.Timer; import javax.swing.UIManager; import com.sjsq.model.User; import com.sjsq.util.MD5Util; import com.sjsq.dao.UserDao; public class LoginFrame extends JFrame implements ActionListener { public static String uname = null; public static String pwd = null; String username, password, login, quit; // 登录标签 private JLabel loginJLabel; private JPanel jContentPane = null; private JButton jButtonLogin = null; private JButton jButtonExit = null; private JTextField jTextFieldUserName = null; private JTextField jTextFieldPassWord = null; // 登录用户名 static int storeUserId; // 图片 public JLabel jLabel_Image = null; // 登录用户名 public static String storeUserame = null; // 登录密码 public static String storeUserPassword = null; // 重新登陆标记 static boolean RELOAD = true; private JLabel jLabelUserName = null; private JLabel jLabelPassWord = null; int index; public LoginFrame() { // 设置背景 setForeground(new Color(255, 255, 255)); this.setResizable(false); this.setSize(800, 500); this.setTitle("学生成绩管理系统"); this.setLocationRelativeTo(null); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// 使用windows外观 } catch (Exception e) { e.printStackTrace(); } loginJLabel = new JLabel("学生成绩管理系统"); Font font = new Font("微软雅黑",Font.BOLD,40); loginJLabel.setFont(font); loginJLabel.setBounds(220,100,650, 50); loginJLabel.setForeground(Color.DARK_GRAY); getContentPane().add(loginJLabel); Font font2 = new Font("微软雅黑",Font.BOLD,15); jLabelPassWord = new JLabel(); jLabelPassWord.setFont(font2); jLabelPassWord.setBounds(new Rectangle(300, 250, 71, 29)); jLabelPassWord.setText("用户名:"); jLabelUserName = new JLabel(); jLabelUserName.setBounds(new Rectangle(300, 300, 71, 29)); jLabelUserName.setFont(font2); jLabelUserName.setText("密码:"); // 账号输入框 jTextFieldUserName = new JTextField(20); jTextFieldUserName.setBounds(new Rectangle(360, 250, 154, 33)); // 密码输入框 jTextFieldPassWord = new JPasswordField(); jTextFieldPassWord.setBounds(new Rectangle(360, 300, 154, 33)); // 登录 jButtonLogin = new JButton(); jButtonLogin.setBounds(new Rectangle(320, 380, 78, 26)); jButtonLogin.setText("登录"); jButtonLogin.setUI(new BEButtonUI() .setNormalColor(BEButtonUI.NormalColor.green)); // 回车登录 getRootPane().setDefaultButton(jButtonLogin); // 退出 jButtonExit = new JButton(); jButtonExit.setUI(new BEButtonUI() .setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonExit.setBounds(new Rectangle(420, 380, 78, 26)); jButtonExit.setText("退出"); // 包含所有的元素 jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(jLabelUserName, null); jContentPane.add(jLabelPassWord, null); jContentPane.add(jButtonLogin, null); jContentPane.add(jButtonExit, null); jContentPane.add(jTextFieldUserName, null); jContentPane.add(jTextFieldPassWord, null); getContentPane().add(jContentPane); jTextFieldUserName.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { if(e.getKeyChar() == KeyEvent.VK_ENTER){ jTextFieldPassWord.requestFocus(); } } }); jTextFieldPassWord.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyChar() == KeyEvent.VK_ENTER){ username = jTextFieldUserName.getText(); password = MD5Util.string2MD5(jTextFieldPassWord.getText()); User user = new User(); user.setUsername(username); user.setPassword(password); String s=user.getUsername(); String p=jTextFieldPassWord.getText(); UserDao userDao = new UserDao(); int choice = 0; if(!userDao.checkUser(user)) { JOptionPane.showMessageDialog(null, "用户名和密码错误","消息提示",JOptionPane.WARNING_MESSAGE); }else { setVisible(false); MainMenuFrame MM = new MainMenuFrame(); uname = s; pwd=p; } } } }); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); // 添加监控 jTextFieldUserName.addActionListener(this); jTextFieldPassWord.addActionListener(this); jButtonLogin.addActionListener(this); jButtonExit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // 获取账号和密码 username = jTextFieldUserName.getText(); password = MD5Util.string2MD5(jTextFieldPassWord.getText()); User user = new User(); user.setUsername(username); user.setPassword(password); String s = user.getUsername(); String p = jTextFieldPassWord.getText(); UserDao userDao = new UserDao(); int choice = 0; if (e.getSource() == jButtonLogin) { if (!userDao.checkUser(user)) { JOptionPane.showMessageDialog(null, "用户名和密码错误", "消息提示", JOptionPane.WARNING_MESSAGE); } else { setVisible(false); MainMenuFrame MM = new MainMenuFrame(); uname = s; pwd = p; } } else if (e.getSource() == jButtonExit) { System.exit(0); } } // 测试 public static void main(String[] args) { new LoginFrame(); } }
MainMenuFrame.java
package com.sjsq.view; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import org.jb2011.lnf.beautyeye.ch4_button.BEButtonUI; /* * 登陆成功后主界面 */ public class MainMenuFrame extends JFrame implements ActionListener { private JFrame mainmenu; private JTextArea text; // 标题栏(关于) private JMenuBar menuBar1; private JMenuItem aboutSystem; public MainMenuFrame() { mainmenu(); } public void mainmenu() { mainmenu = new JFrame("极客大学学生管理系统"); setLocationRelativeTo(null);// 将容器显示在屏幕中央 mainmenu.setSize(850, 650); mainmenu.getContentPane().add(new JScrollPane(text)); JPanel jContentPane = new JPanel(); jContentPane.setLayout(null); // 查询学生按钮 JButton jButtonSelect = new JButton(); jButtonSelect.setBounds(new Rectangle(320, 100, 100, 52)); jButtonSelect.setText("查询学生"); jButtonSelect.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); getRootPane().setDefaultButton(jButtonSelect);// 回车登录 // 添加学生按钮 JButton jButtonAdd = new JButton(); jButtonAdd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonAdd.setBounds(new Rectangle(320, 200, 100, 52)); jButtonAdd.setText("添加学生"); // 修改密码按钮 JButton jButtonAlterPwd = new JButton(); jButtonAlterPwd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonAlterPwd.setBounds(new Rectangle(320, 300, 100, 52)); jButtonAlterPwd.setText("密码修改"); // 退出系统按钮 JButton jButtonExit = new JButton(); jButtonExit.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonExit.setBounds(new Rectangle(320, 400, 100, 52)); jButtonExit.setText("退出系统"); jContentPane.add(jButtonSelect, null); jContentPane.add(jButtonAdd, null); jContentPane.add(jButtonAlterPwd, null); jContentPane.add(jButtonExit, null); // 添加Label到Frame mainmenu.getContentPane().add(jContentPane); // 设置字体 Font font = new Font("微软雅黑", Font.BOLD, 16); // 标题栏 menuBar1 = new JMenuBar(); // aboutSystem = new JMenuItem("关于本系统",new // ImageIcon("src/images/icons/about.png")); aboutSystem = new JMenuItem("关于本系统"); aboutSystem.setMnemonic('H'); menuBar1.add(aboutSystem); // 查询学生 jButtonSelect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------查询学生-----"); QueryStudentInfo queryStudentInfo = new QueryStudentInfo(); } }); // 添加学生 jButtonAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------添加学生-----"); AddStudentInfo addStudentInfo = new AddStudentInfo(); } }); // 修改密码 jButtonAlterPwd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------修改密码-----"); ModifyPasswordInfo modifyPasswordInfo = new ModifyPasswordInfo(); } }); // 退出系统 jButtonExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------退出系统-----"); System.exit(1); } }); // 关于系统 aboutSystem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------关于系统------"); AboutSystem aboutSystem = new AboutSystem(); } }); // 关闭窗口监控 mainmenu.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(1); } }); mainmenu.setJMenuBar(menuBar1); mainmenu.setVisible(true); // mainmenu.setLocation(250, 50); mainmenu.setLocationRelativeTo(null); aboutSystem.addActionListener(this); } // 主函数测试 public static void main(String[] args) { new MainMenuFrame(); } @Override public void actionPerformed(ActionEvent arg0) { } }
关于怎么在Java中使用Swing与Eclipse实现一个学生成绩管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。