java学生管理系统界面设计
生活随笔
收集整理的這篇文章主要介紹了
java学生管理系统界面设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于學生管理系統的界面設計:代碼如下:
數據庫設計
添加學生信息界面
/*** @author 逸軒* https://www.jianshu.com/u/1b1f7b0b32d6*/ import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class StuAddDialog extends JDialog implements ActionListener{ //=========面板控件 //......左側標題欄 private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......右側信息選擇填寫欄 private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //......添加和取消按鈕 private JButton addBtn,cancelBtn; //......布局控件 private JPanel left,center,bottom; //構造函數 public StuAddDialog(Frame owner, String title, boolean modal) { //========重寫父類方法 super(owner, title, modal); //========左側標簽欄 idLab = new JLabel("學號: "); nameLab = new JLabel("姓名: "); sexLab = new JLabel("性別: "); ageLab = new JLabel("年齡: "); jgLab = new JLabel("籍貫: "); deptLab = new JLabel("系別: "); //========右側信息填寫欄 idTxt = new JTextField(); nameTxt = new JTextField(); sexTxt = new JTextField(); ageTxt = new JTextField(); jgTxt = new JTextField(); deptTxt = new JTextField(); //========添加和取消按鈕 addBtn = new JButton("添加"); cancelBtn = new JButton("取消"); //......添加監聽 addBtn.addActionListener(this); addBtn.setActionCommand("add"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //========創建布局 //......創建左邊欄 left = new JPanel(); left.setLayout(new GridLayout(6, 1)); left.add(idLab); left.add(nameLab); left.add(sexLab); left.add(ageLab); left.add(jgLab); left.add(deptLab); //......創建右邊欄 center = new JPanel(); center.setLayout(new GridLayout(6, 1)); center.add(idTxt); center.add(nameTxt); center.add(sexTxt); center.add(ageTxt); center.add(jgTxt); center.add(deptTxt); //========底層添加和取消按鈕 bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //========整體布局 this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========設置窗口屬性 this.setSize(300, 250); this.setResizable(false); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("add")) { /***********************添加學生信息**************************/ StuModel tmp = new StuModel(); String sql = "insert into stu values(?,?,?,?,?,?)"; String []paras = {idTxt.getText(),nameTxt.getText(),sexTxt.getText(), ageTxt.getText(),jgTxt.getText(),deptTxt.getText()}; if(!tmp.cudStu(sql, paras)) JOptionPane.showMessageDialog(this, "添加學生信息失敗"); //========關閉窗口 this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //========關閉窗口 this.dispose(); } } }數據庫連接
/*** @author 逸軒* https://www.jianshu.com/u/1b1f7b0b32d6*/ import java.sql.*; public class SqlHelper { //========數據庫 private Connection ct = null; private PreparedStatement ps = null; private ResultSet rs = null; private String driver = "com.mysql.jdbc.Driver"; //后面加上utf-8 不然中文亂碼private String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"; private String user = "root"; private String passwd = ""; //========查詢 public ResultSet queryExecute(String sql, String []paras) { try { //========1、加載驅動 Class.forName(driver); //========2、連接 ct = DriverManager.getConnection(url, user, passwd); //========3、創建PreparedStatement ps = ct.prepareStatement(sql); //========4、給問號賦值 if(paras != null) { for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } } //========5、執行 rs = ps.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { //this.close(); } //========返回值 return rs; } //========增刪改 public boolean cudExecute(String sql, String []paras) { boolean b = true; try { //========1、加載驅動 Class.forName(driver); //========2、連接 ct = DriverManager.getConnection(url, user, passwd); //========3、創建PreparedStatement ps = ct.prepareStatement(sql); //========4、給問號賦值 for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } //========5、執行 if(ps.executeUpdate() != 1) b = false; } catch (Exception e) { // TODO: handle exception b = false; e.printStackTrace(); } finally { this.close(); } //========返回值 return b; } //========關閉資源 public void close() { try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(ct!=null) ct.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } }學生管理界面有查詢功能、修改信息功能、刪除功能
/*** @author 逸軒* https://www.jianshu.com/u/1b1f7b0b32d6*/ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class StudentManage extends JFrame implements ActionListener { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new StudentManage(); } //========面板控件 private JLabel queryLab = null; private JTextField queryTxt = null; private JButton queryBtn = null; private JButton allBtn = null; private JTable resultTb = null; private JScrollPane jsp = null; private JButton addBtn = null; private JButton deleteBtn = null; private JButton updateBtn = null; private JPanel top = null; private JPanel bottom = null; //======== private StuModel sm = null; //構造函數 public StudentManage() { super("學生管理系統");/***************************初始化面板控件***********************/ //========查詢欄 queryLab = new JLabel("請輸入姓名:"); queryTxt = new JTextField(10); queryBtn = new JButton("查詢"); allBtn = new JButton("全部"); //......添加查詢欄監聽 queryBtn.addActionListener(this); queryBtn.setActionCommand("query"); allBtn.addActionListener(this); allBtn.setActionCommand("all"); //========增刪改欄 addBtn = new JButton("添加"); deleteBtn = new JButton("刪除"); updateBtn = new JButton("修改"); //......添加增刪改欄監聽 addBtn.addActionListener(this); addBtn.setActionCommand("add"); deleteBtn.addActionListener(this); deleteBtn.setActionCommand("delete"); updateBtn.addActionListener(this); updateBtn.setActionCommand("update"); //========創建窗口整體布局 //......頂層查詢欄 top = new JPanel(); top.add(queryLab); top.add(queryTxt); top.add(queryBtn); top.add(allBtn); //......底層增刪改欄 bottom = new JPanel(); bottom.add(addBtn); bottom.add(deleteBtn); bottom.add(updateBtn); //......中間層顯示欄 sm = new StuModel(); String sql = "select * from stu"; sm.queryStu(sql, null); resultTb = new JTable(sm); jsp = new JScrollPane(resultTb); //......構建整體布局 this.add(top,BorderLayout.NORTH); this.add(jsp,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========設置窗口屬性 this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); } //監聽 @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("query")) { /*********************查詢***********************/ //========獲取輸入學生的姓名 String name = queryTxt.getText().trim(); if(name.length() != 0) { //========姓名輸入有效時,執行查詢 //......定義參數 String sql = "select * from stu where stuName=?"; String []paras = {name}; //......更新模型 jtableUpdate(sql, paras); } else { //========姓名為空時,設置提醒 JOptionPane.showMessageDialog(this, "姓名輸入不能為空"); } } else if(e.getActionCommand().equals("add")) { /*********************添加***********************/ new StuAddDialog(this, "添加學生信息", true); String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("all")) { /*********************全部顯示***********************/ String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("delete")) { /*********************刪除***********************/ //========獲取選擇行號 int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "請選擇一行"); return ; } //========獲取學生ID號 String stuId = (String)sm.getValueAt(rowNum, 0); //========刪除學生 String sql = "delete from stu where stuId=?"; String []paras = {stuId}; StuModel tmp = new StuModel(); tmp.cudStu(sql, paras); //========更新模型 sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("update")) { /*********************修改***********************/ //========獲取選擇行號 int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "請選擇一行"); return ; } new StuUpdateDialog(this, "修改學生信息", true, sm, rowNum); String sql = "select * from stu"; jtableUpdate(sql, null); } } //========更新JTable內數據 public void jtableUpdate(String sql, String[] paras) { //......創建模型 sm = new StuModel(); sm.queryStu(sql, paras); //......更新顯示 resultTb.setModel(sm); } }修改學生信息界面設計
/*** @author 逸軒* https://www.jianshu.com/u/1b1f7b0b32d6*/ import java.awt.BorderLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.table.AbstractTableModel; public class StuUpdateDialog extends JDialog implements ActionListener{ //=========面板控件 //......左側標題欄 private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......右側信息選擇填寫欄 private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //......添加和取消按鈕 private JButton addBtn,cancelBtn; //......布局控件 private JPanel left,center,bottom; //構造函數 public StuUpdateDialog(Frame owner, String title, boolean modal, StuModel sm, int rowNum) { //========重寫父類方法 super(owner, title, modal); //========左側標簽欄 idLab = new JLabel("學號: "); nameLab = new JLabel("姓名: "); sexLab = new JLabel("性別: "); ageLab = new JLabel("年齡: "); jgLab = new JLabel("籍貫: "); deptLab = new JLabel("系別: "); //========右側信息填寫欄 idTxt = new JTextField(); idTxt.setText((String)sm.getValueAt(rowNum, 0)); idTxt.setEditable(false); nameTxt = new JTextField(); nameTxt.setText((String)sm.getValueAt(rowNum, 1)); sexTxt = new JTextField(); sexTxt.setText((String)sm.getValueAt(rowNum, 2)); ageTxt = new JTextField(); ageTxt.setText((String)sm.getValueAt(rowNum, 3)); jgTxt = new JTextField(); jgTxt.setText((String)sm.getValueAt(rowNum, 4)); deptTxt = new JTextField(); deptTxt.setText((String)sm.getValueAt(rowNum, 5)); //========添加和取消按鈕 addBtn = new JButton("修改"); cancelBtn = new JButton("取消"); //......添加監聽 addBtn.addActionListener(this); addBtn.setActionCommand("update"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //========創建布局 //......創建左邊欄 left = new JPanel(); left.setLayout(new GridLayout(6, 1)); left.add(idLab); left.add(nameLab); left.add(sexLab); left.add(ageLab); left.add(jgLab); left.add(deptLab); //......創建右邊欄 center = new JPanel(); center.setLayout(new GridLayout(6, 1)); center.add(idTxt); center.add(nameTxt); center.add(sexTxt); center.add(ageTxt); center.add(jgTxt); center.add(deptTxt); //========底層添加和取消按鈕 bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //========整體布局 this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========設置窗口屬性 this.setSize(300, 250); this.setResizable(false); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("update")) { /***********************修改學生信息**************************/ StuModel tmp = new StuModel(); String sql = "update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?"; String []paras = {nameTxt.getText(),sexTxt.getText(),ageTxt.getText(), jgTxt.getText(),deptTxt.getText(),idTxt.getText()}; if(!tmp.cudStu(sql, paras)) JOptionPane.showMessageDialog(this, "修改學生信息失敗"); //========關閉窗口 this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //========關閉窗口 this.dispose(); } } }查詢學生功能
/*** @author 逸軒* https://www.jianshu.com/u/1b1f7b0b32d6*/ import java.sql.ResultSet; import java.util.Vector; import javax.swing.table.AbstractTableModel; public class StuModel extends AbstractTableModel{ private Vector columnNames; private Vector rowDates; // public StuModel() { String sql = "select * from stu"; String []paras = {}; } //========增刪改學生 public boolean cudStu(String sql, String []paras) { return new SqlHelper().cudExecute(sql, paras); } //========查詢學生 public void queryStu(String sql, String []paras) { SqlHelper sqlHelper = null; //========初始化JTable信息 columnNames = new Vector(); rowDates = new Vector(); columnNames.add("學號"); columnNames.add("名字"); columnNames.add("性別"); columnNames.add("年齡"); columnNames.add("籍貫"); columnNames.add("系別"); try { sqlHelper = new SqlHelper(); ResultSet rs = sqlHelper.queryExecute(sql, paras); while(rs.next()) { Vector row = new Vector(); row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); row.add(rs.getString(4)); row.add(rs.getString(5)); row.add(rs.getString(6)); rowDates.add(row); } } catch (Exception e) { // TODO: handle exception } finally { sqlHelper.close(); } } @Override public int getColumnCount() { // TODO Auto-generated method stub return this.columnNames.size(); } @Override public int getRowCount() { // TODO Auto-generated method stub return this.rowDates.size(); } @Override public Object getValueAt(int row, int col) { // TODO Auto-generated method stub if(!rowDates.isEmpty()) return ((Vector)this.rowDates.get(row)).get(col); else return null; } @Override public String getColumnName(int column) { // TODO Auto-generated method stub return (String)this.columnNames.get(column); } }截圖演示
鏈接下載
https://download.csdn.net/download/qq_40861561/11286135
總結
以上是生活随笔為你收集整理的java学生管理系统界面设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二维数组数和指针操作的理解
- 下一篇: rabbitmq消息队列,消息发送失败,