java+io+swing實現學生信息管理系統
本文實例為大傢分享瞭java+io+swing實現學生信息管理系統的具體代碼,供大傢參考,具體內容如下
說明:
1.開發環境基於eclipse外加windowbuilder插件。
2.采用io流持續儲存文件到本地磁盤。
3.Arrylist對文件信息進行操作。
一、界面展示
本地儲存學生信息的txt文件,可在此對學生信息增刪改查但需要註意格式。
二、項目構架
三、相關代碼及介紹
1.IO部分
學生實體類
package stuManager; public class StuInfo { private int stuId;// 學號 private String stuName;// 姓名 private int stuAge;// 年齡 private String stuPrefession;// 專業 private int stuHomeNumber;// 宿舍 // Alt+Shift+S快速創建構造方法: public StuInfo() { } public StuInfo(int stuId, String stuName, int stuAge, String stuPrefession, int stuHomeNumber) { this.stuId = stuId; this.stuName = stuName; this.stuAge = stuAge; this.stuPrefession = stuPrefession; this.stuHomeNumber = stuHomeNumber; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getStuPrefession() { return stuPrefession; } public void setStuPrefession(String stuPrefession) { this.stuPrefession = stuPrefession; } public int getStuHomeNumber() { return stuHomeNumber; } public void setStuHomeNumber(int stuHomeNumber) { this.stuHomeNumber = stuHomeNumber; } }
學生信息寫入本地磁盤txt文件類
package stuManager; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import stuView.StuManagerFrm; public class ArrayListToFile { public static void main(String[] args) throws IOException { // 創建輸出緩沖流對象 BufferedWriter bw = new BufferedWriter(new FileWriter("e://a.txt")); for (int i = 0; i < StuManagerFrm.list.size(); i++) { //向txt文件中寫入 bw.write(StuManagerFrm.list.get(i).getStuId() + "," + StuManagerFrm.list.get(i).getStuName() + "," + StuManagerFrm.list.get(i).getStuAge() + "," + StuManagerFrm.list.get(i).getStuPrefession() + "," + StuManagerFrm.list.get(i).getStuHomeNumber()); bw.newLine(); bw.flush(); } // 釋放資源 bw.close(); } }
讀取本地磁盤txt文件信息類
package stuManager; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import stuView.StuAdd; import stuView.StuManagerFrm; public class FileToArrayList { public static boolean a = true; public static void main(String[] args) throws IOException { // 創建一個輸入緩沖對象 BufferedReader br = new BufferedReader(new FileReader("e:\\a.txt")); String line; while ((line = br.readLine()) != null) { // br.read.line讀一行 String[] strArray = line.split(","); StuInfo stu = new StuInfo(); stu.setStuId(Integer.parseInt(strArray[0])); stu.setStuName(strArray[1]); stu.setStuAge(Integer.parseInt(strArray[2])); stu.setStuPrefession(strArray[3]); stu.setStuHomeNumber(Integer.parseInt(strArray[4])); StuManagerFrm.list.add(stu); } // 釋放資源 br.close(); } }
刪除txt文件所有信息類
package stuManager; import java.io.*; public class ClearFile { //清空文件內容 public static void clearInfoForFile(String fileName) { File file = new File("e:\\a.txt"); try { if (!file.exists()) { file.createNewFile(); } FileWriter fileWriter = new FileWriter(file); fileWriter.write(""); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.Arrylist及窗口部分
登陸窗體(初始賬號:admin 密碼:123456)
package stuView; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class StuLogin extends JFrame { static String user = "admin"; static int userPassword = 123456; static String userPassword0 = String.valueOf(userPassword); private JPanel contentPane; static JTextField textField; private JLabel lblNewLabel_2; static JPasswordField passwordField; private JButton btnNewButton; /** * 所有jframe窗體借用eclipse插件Windowbuilder繪制 * */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { StuLogin frame = new StuLogin(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public StuLogin() { setTitle("登陸"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 409, 252); contentPane = new JPanel(); contentPane.setBackground(Color.LIGHT_GRAY); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(114, 28, 165, 35); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(28, 73, 76, 35); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(114, 80, 136, 21); contentPane.add(textField); textField.setColumns(10); lblNewLabel_2 = new JLabel("\u5BC6\u7801"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(28, 129, 76, 35); contentPane.add(lblNewLabel_2); passwordField = new JPasswordField(); passwordField.setBounds(114, 136, 136, 21); contentPane.add(passwordField); btnNewButton = new JButton("\u767B\u9646"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 登陸按鈕監聽 public void actionPerformed(ActionEvent e) { if (textField.getText().equals("admin") && passwordField.getText().equals(userPassword0)) { JOptionPane.showMessageDialog(null, "登陸成功!"); dispose(); StuManagerFrm.main(null); } else { JOptionPane.showMessageDialog(null, "賬號或密碼錯誤"); } } }); btnNewButton.setBounds(133, 182, 97, 23); contentPane.add(btnNewButton); } }
主界面
package stuView; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import stuManager.StuInfo; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import javax.swing.JScrollBar; import java.awt.ScrollPane; import javax.swing.JScrollPane; import java.awt.Color; public class StuManagerFrm extends JFrame { public static List<StuInfo> list = new ArrayList<StuInfo>();// 創建靜態類型的動態數組,方便調用 private JPanel contentPane; //有關面板窗體都設置為靜態,方便調用 static JFrame frame; static JPanel passwordChangePanel; static JPanel stuAddPanel; static JPanel stuQueryPanel; static JPanel stuUpdatePanel; static JPanel stuDeletePanel; static JTextArea textArea; static JPanel userInfoPanel; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { frame = new StuManagerFrm(); frame.setResizable(false);// 窗體不可放大 frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame.(由windowbuilder繪制) */ public StuManagerFrm() { setTitle("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); setForeground(Color.WHITE); setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 672, 440); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu mnNewMenu = new JMenu("\u7528\u6237\u64CD\u4F5C"); menuBar.add(mnNewMenu); JMenuItem mntmNewMenuItem = new JMenuItem("\u4FEE\u6539\u5BC6\u7801"); mntmNewMenuItem.addActionListener(new ActionListener() { // 完成修改密碼監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); passwordChangePanel = new UserNameChange(); passwordChangePanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(passwordChangePanel); passwordChangePanel.updateUI(); } }); mnNewMenu.add(mntmNewMenuItem); JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u9000\u51FA\u767B\u5F55"); mntmNewMenuItem_1.addActionListener(new ActionListener() { // 完成退出登陸監聽: public void actionPerformed(ActionEvent e) { dispose(); JOptionPane.showMessageDialog(null, "退出成功請重新登陸!"); StuLogin.main(null); } }); mnNewMenu.add(mntmNewMenuItem_1); JMenuItem mntmNewMenuItem_8 = new JMenuItem("\u7528\u6237\u4FE1\u606F"); mntmNewMenuItem_8.addActionListener(new ActionListener() { // 用戶信息監聽 public void actionPerformed(ActionEvent e) { //有關移除非選定的面板持續並顯示文本區(以下各個監聽同理) StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); //顯示當前選定元素對應的面板(以下各個監聽同理) userInfoPanel = new UserInfo(); userInfoPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(userInfoPanel); userInfoPanel.updateUI(); UserInfo.lblNewLabel_2.setText(StuLogin.user); UserInfo.lblNewLabel_2_1.setText(String.valueOf(StuLogin.userPassword0)); } }); mnNewMenu.add(mntmNewMenuItem_8); JMenu mnNewMenu_1 = new JMenu("\u5B66\u751F\u4FE1\u606F\u64CD\u4F5C"); menuBar.add(mnNewMenu_1); JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_2.addActionListener(new ActionListener() { // 增添學生信息監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuAddPanel = new StuAdd(); stuAddPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuAddPanel); stuAddPanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_2); JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_3.addActionListener(new ActionListener() { // 修改學生信息監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuUpdatePanel = new StuUpdate(); stuUpdatePanel.setBounds(0, 0, 227, 380); frame.getContentPane().add(stuUpdatePanel); stuUpdatePanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_3); JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_4.addActionListener(new ActionListener() { // 查詢學生信息監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuQueryPanel = new StuQuery(); stuQueryPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuQueryPanel); stuQueryPanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_4); JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u5220\u9664\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_5.addActionListener(new ActionListener() { // 刪除學生信息監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuDeletePanel = new StuDelete(); stuDeletePanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuDeletePanel); stuDeletePanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_5); JMenu mnNewMenu_2 = new JMenu("\u5173\u4E8E\u4F5C\u8005"); menuBar.add(mnNewMenu_2); JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u8054\u7CFB\u6211\u4EEC"); mntmNewMenuItem_6.addActionListener(new ActionListener() { // 關於作者部分監聽 public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "電話:" + "110"); } }); mnNewMenu_2.add(mntmNewMenuItem_6); JMenuItem mntmNewMenuItem_7 = new JMenuItem("\u8054\u7CFB\u5730\u5740"); mntmNewMenuItem_7.addActionListener(new ActionListener() { // 聯系地址監聽 public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "地址:" + "公安局"); } }); mnNewMenu_2.add(mntmNewMenuItem_7); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); // 向窗體添加文本區並添加滑輪 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); } }
修改密碼面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import java.awt.Font; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class UserNameChange extends JPanel { private JPasswordField passwordField; /** * Create the panel. */ public UserNameChange() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u5BC6\u7801\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 73, 99, 32); add(lblNewLabel); passwordField = new JPasswordField(); passwordField.setBounds(83, 79, 122, 21); add(passwordField); JLabel lblNewLabel_1 = new JLabel("\u4FEE\u6539\u5BC6\u7801"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 10, 122, 32); add(lblNewLabel_1); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 確定修改密碼監聽 public void actionPerformed(ActionEvent e) { StuLogin.userPassword0 = passwordField.getText(); StuManagerFrm.frame.dispose(); JOptionPane.showMessageDialog(null, "修改成功,請重新登陸!"); StuLogin.main(null); } }); btnNewButton.setBounds(0, 127, 77, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消修改密碼監聽 public void actionPerformed(ActionEvent e) { // 刪除修改密碼面板 StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.passwordChangePanel); // 重畫窗口 StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(128, 127, 77, 23); add(btnNewButton_1); } }
用戶信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Font; import java.awt.Color; public class UserInfo extends JPanel { static JLabel lblNewLabel_2; static JLabel lblNewLabel_2_1; /** * Create the panel. */ public UserInfo() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u5F53\u524D\u7528\u6237\u4FE1\u606F"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(10, 10, 114, 31); add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(10, 79, 95, 31); add(lblNewLabel_1); JLabel lblNewLabel_1_1 = new JLabel("\u5BC6 \u7801\uFF1A"); lblNewLabel_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1.setBounds(10, 156, 95, 31); add(lblNewLabel_1_1); lblNewLabel_2 = new JLabel("New label"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(81, 83, 93, 23); add(lblNewLabel_2); lblNewLabel_2_1 = new JLabel("New label"); lblNewLabel_2_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2_1.setBounds(81, 160, 93, 23); add(lblNewLabel_2_1); } }
添加學生信息面板
package stuView; import javax.swing.JPanel; import java.awt.Color; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.FileToArrayList; import stuManager.StuInfo; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; public class StuAdd extends JPanel { static JTextField textField; static JTextField textField_1; static JTextField textField_2; static JTextField textField_3; static JTextField textField_4; /** * Create the panel. */ public StuAdd() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66 \u53F7\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(10, 46, 78, 32); add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u59D3 \u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(10, 88, 78, 32); add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("\u5E74 \u9F84\uFF1A"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(10, 130, 78, 32); add(lblNewLabel_2); JLabel lblNewLabel_3 = new JLabel("\u4E13 \u4E1A\uFF1A"); lblNewLabel_3.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_3.setBounds(10, 172, 78, 32); add(lblNewLabel_3); JLabel lblNewLabel_4 = new JLabel("\u5BBF \u820D \u53F7\uFF1A"); lblNewLabel_4.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_4.setBounds(10, 214, 90, 32); add(lblNewLabel_4); textField = new JTextField(); textField.setBounds(90, 52, 130, 21); add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(90, 94, 130, 21); add(textField_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(90, 136, 130, 21); add(textField_2); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(90, 172, 130, 21); add(textField_3); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(90, 220, 130, 21); add(textField_4); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 確定添加學生信息監聽: public void actionPerformed(ActionEvent e) { // 實例化學生對象stu並添加信息 if (textField.getText() == null || textField_1.getText() == null|| textField_2.getText() == null && textField_3.getText() == null|| textField_4.getText() == null) { JOptionPane.showMessageDialog(null, "添加失敗,請檢查輸入!"); } else { // 先把文件讀取出來在進行添加 if (FileToArrayList.a == true) { try { FileToArrayList.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } FileToArrayList.a = false; StuInfo stu = new StuInfo(Integer.parseInt(textField.getText()), textField_1.getText(),Integer.parseInt(textField_2.getText()), textField_3.getText(),Integer.parseInt(textField_4.getText())); StuManagerFrm.list.add(stu); // 寫入到文件 try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //關閉當前面板 StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuAddPanel); StuManagerFrm.frame.repaint(); JOptionPane.showMessageDialog(null, "添加成功!"); } } }); btnNewButton.setBounds(10, 275, 97, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消添加學生信息監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuAddPanel); // 重畫窗口 StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(123, 275, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_5 = new JLabel("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F"); lblNewLabel_5.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_5.setBounds(10, 0, 130, 32); add(lblNewLabel_5); } }
修改學生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.FileToArrayList; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; public class StuUpdate extends JPanel { private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; /** * Create the panel. */ public StuUpdate() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u4F9D\u636E\u5B66\u53F7\u4FEE\u6539\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 70, 106, 27); add(lblNewLabel); textField = new JTextField(); textField.setBounds(102, 73, 118, 21); add(textField); textField.setColumns(10); JLabel lblNewLabel_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u59D3\u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 115, 106, 27); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(102, 118, 118, 21); add(textField_1); JLabel lblNewLabel_1_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u5E74\u9F84\uFF1A"); lblNewLabel_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1.setBounds(0, 165, 106, 27); add(lblNewLabel_1_1); JLabel lblNewLabel_1_1_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u4E13\u4E1A\uFF1A"); lblNewLabel_1_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1_1.setBounds(0, 202, 106, 27); add(lblNewLabel_1_1_1); JLabel lblNewLabel_1_1_2 = new JLabel("\u4FEE\u6539\u540E\u7684\u5BDD\u5BA4\uFF1A"); lblNewLabel_1_1_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1_2.setBounds(0, 239, 106, 27); add(lblNewLabel_1_1_2); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(102, 168, 118, 21); add(textField_2); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(102, 202, 118, 21); add(textField_3); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(102, 242, 118, 21); add(textField_4); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 確定修改學生信息 public void actionPerformed(ActionEvent e) { if (textField.getText() != null && textField_2.getText() != null && textField_1.getText() != null && textField_3.getText() != null || textField_4.getText() != null) { for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (Integer.parseInt(textField.getText()) == StuManagerFrm.list.get(i).getStuId()) { StuManagerFrm.list.get(i).setStuName(textField_1.getText()); StuManagerFrm.list.get(i).setStuAge(Integer.parseInt(textField_2.getText())); StuManagerFrm.list.get(i).setStuPrefession(textField_3.getText()); StuManagerFrm.list.get(i).setStuHomeNumber(Integer.parseInt(textField_4.getText())); } } JOptionPane.showMessageDialog(null, "修改成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "修改失敗,請檢查輸入!"); } StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuUpdatePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton.setBounds(10, 298, 97, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消修改監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuUpdatePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(123, 298, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u4FEE\u6539"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(0, 10, 106, 27); add(lblNewLabel_2); } }
查詢學生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JTextField; import stuManager.FileToArrayList; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Font; public class StuQuery extends JPanel { private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; /** * Create the panel. */ public StuQuery() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u8F93\u5165\u5B66\u53F7\u67E5\u8BE2:"); lblNewLabel.setBounds(0, 78, 86, 34); add(lblNewLabel); textField = new JTextField(); textField.setBounds(81, 85, 57, 21); add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u67E5\u8BE2"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 學號查詢監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuId() == (Integer.parseInt(textField.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton.setBounds(140, 84, 73, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u5168\u90E8\u67E5\u8BE2"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 全部查詢監聽: public void actionPerformed(ActionEvent e) { if (FileToArrayList.a == true) { try { FileToArrayList.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } FileToArrayList.a = false; StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } }); btnNewButton_1.setBounds(10, 297, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_1 = new JLabel("\u8F93\u5165\u59D3\u540D\u67E5\u8BE2:"); lblNewLabel_1.setBounds(0, 122, 86, 34); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(81, 129, 57, 21); add(textField_1); JButton btnNewButton_2 = new JButton("\u67E5\u8BE2"); btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.addActionListener(new ActionListener() { // 姓名查詢監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuName().equals(textField_1.getText())) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2.setBounds(140, 128, 73, 23); add(btnNewButton_2); JLabel lblNewLabel_1_1 = new JLabel("\u8F93\u5165\u5E74\u9F84\u67E5\u8BE2:"); lblNewLabel_1_1.setBounds(0, 166, 86, 34); add(lblNewLabel_1_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(81, 173, 57, 21); add(textField_2); JButton btnNewButton_2_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1.addActionListener(new ActionListener() { // 年齡查詢監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuAge() == (Integer.parseInt(textField_2.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1.setBounds(140, 172, 73, 23); add(btnNewButton_2_1); JLabel lblNewLabel_1_1_1 = new JLabel("\u8F93\u5165\u4E13\u4E1A\u67E5\u8BE2:"); lblNewLabel_1_1_1.setBounds(0, 205, 86, 34); add(lblNewLabel_1_1_1); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(81, 212, 57, 21); add(textField_3); JButton btnNewButton_2_1_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1_1.addActionListener(new ActionListener() { // 專業查詢監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuPrefession().equals(textField_3.getText())) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1_1.setBounds(140, 211, 73, 23); add(btnNewButton_2_1_1); JLabel lblNewLabel_1_1_1_1 = new JLabel("\u8F93\u5165\u5BDD\u5BA4\u67E5\u8BE2:"); lblNewLabel_1_1_1_1.setBounds(0, 242, 86, 34); add(lblNewLabel_1_1_1_1); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(81, 249, 57, 21); add(textField_4); JButton btnNewButton_2_1_1_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1_1_1.addActionListener(new ActionListener() { // 宿舍號查詢監聽: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuHomeNumber() == (Integer.parseInt(textField_4.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1_1_1.setBounds(140, 248, 73, 23); add(btnNewButton_2_1_1_1); JButton btnNewButton_1_1 = new JButton("\u53D6\u6D88\u67E5\u8BE2"); btnNewButton_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1_1.addActionListener(new ActionListener() { // 取消查詢: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuQueryPanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_1_1.setBounds(116, 297, 97, 23); add(btnNewButton_1_1); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u67E5\u8BE2"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(10, 10, 114, 34); add(lblNewLabel_2); } }
刪除學生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.ClearFile; import javax.swing.JButton; import java.awt.Font; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; public class StuDelete extends JPanel { private JTextField textField; private JTextField textField_1; /** * Create the panel. */ public StuDelete() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u901A\u8FC7\u5B66\u53F7\u5220\u9664\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 81, 119, 25); add(lblNewLabel); textField = new JTextField(); textField.setBounds(102, 83, 55, 21); add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u5220\u9664"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 通過學號刪除: public void actionPerformed(ActionEvent e) { boolean a = false;// 判斷是否進行瞭刪除操作 for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (Integer.parseInt(textField.getText()) == StuManagerFrm.list.get(i).getStuId()) { StuManagerFrm.list.remove(i); a = true; } } if (a == true) { JOptionPane.showMessageDialog(null, "刪除成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else JOptionPane.showMessageDialog(null, "刪除失敗,請確認學號是否正確!"); } }); btnNewButton.setBounds(157, 82, 63, 23); add(btnNewButton); JLabel lblNewLabel_1 = new JLabel("\u901A\u8FC7\u59D3\u540D\u5220\u9664\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 125, 119, 25); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(102, 127, 55, 21); add(textField_1); JButton btnNewButton_1 = new JButton("\u5220\u9664"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 通過姓名刪除監聽 public void actionPerformed(ActionEvent e) { boolean a = false; for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (textField_1.getText().equals(StuManagerFrm.list.get(i).getStuName())) { StuManagerFrm.list.remove(i); a = true; } } if (a == true) { JOptionPane.showMessageDialog(null, "刪除成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else JOptionPane.showMessageDialog(null, "刪除失敗,請確認姓名是否正確!"); } }); btnNewButton_1.setBounds(157, 126, 63, 23); add(btnNewButton_1); JButton btnNewButton_2 = new JButton("\u5168\u90E8\u5220\u9664"); btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.addActionListener(new ActionListener() { // 全部刪除監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.list.clear(); ClearFile.clearInfoForFile(null); JOptionPane.showMessageDialog(null, "刪除成功!"); } }); btnNewButton_2.setBounds(0, 253, 97, 23); add(btnNewButton_2); JButton btnNewButton_3 = new JButton("\u53D6\u6D88"); btnNewButton_3.setBackground(Color.LIGHT_GRAY); btnNewButton_3.addActionListener(new ActionListener() { // 取消刪除監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuDeletePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_3.setBounds(123, 253, 97, 23); add(btnNewButton_3); JButton btnNewButton_4 = new JButton("\u5237\u65B0"); btnNewButton_4.setBackground(Color.LIGHT_GRAY); btnNewButton_4.addActionListener(new ActionListener() { // 刪除頁面刷新數據監聽 public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("學號" + "\t" + "姓名" + "\t" + "年齡" + "\t" + "專業" + "\t" + "宿舍號" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 學號 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年齡 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 專業 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍號 StuManagerFrm.textArea.append("\n"); } } }); btnNewButton_4.setBounds(123, 184, 97, 23); add(btnNewButton_4); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u5220\u9664"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(0, 22, 119, 25); add(lblNewLabel_2); } }
總結
1.增刪改查的基本操作都能正常完成。
2.對於窗口界面部分的開發運用eclipse插件WindowBuilder對項目效果上有瞭很大的幫助,使得可以自由設計自己想要的界面。
3.設計框架不夠清晰,部分學生信息操作在監聽內完成。
4.沒有采用數據庫存儲數據而是通過io流在本地完成對文件的存儲。
改進點:
1.采用MVC設計方式完成本項目。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。