版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 中北大學(xué)軟件學(xué)院 實 驗 報 告 專 業(yè) 軟件工程 課程名稱 JAVA面對對象程序設(shè)計 學(xué) 號 1414011529 姓 名 劉宇 輔導(dǎo)教師 薛海麗 成績 實驗日期實驗時間1實驗名稱 學(xué)生成績管理系統(tǒng)實驗?zāi)康?1)、掌握面向?qū)ο蟪绦蜷_發(fā)思想。(2)、掌握使用圖形用戶界面對文件進(jìn)行訪問的程序開發(fā)機(jī)制。實驗內(nèi)容用面向?qū)ο蟪绦蛟O(shè)計思想完善之前的學(xué)生成績管理系統(tǒng),將之前學(xué)生成績管理系統(tǒng)的業(yè)務(wù)邏輯事件和實驗七設(shè)計的主窗體界面進(jìn)行連接,完成圖形用戶界面下的學(xué)生成績管理系統(tǒng),要包含文件的讀寫。4、實驗過程或源代碼import javax.swing.*;import javax.swing.table.*
2、;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;public class StudentManager extends JFrame private Container currentContainer = this; private String title = 學(xué)生成績管理系統(tǒng); private Font headFont = new Font(黑體,Font.PLAIN,18); private File data = new File(data.txt); private Da
3、taTable dataModel = new DataTable(); private JTable dataTable = new JTable(dataModel); private JLabel extraLabel = new JLabel(); private JPanel welcomePanel = createWelcomePanel(); private JPanel insertPanel = createInsertPanel(); private JPanel displayPanel = createDisplayPanel(); private JPanel mo
4、difyPanel = createModifyPanel(); public StudentManager() this.setLocation(300, 300); this.setSize(475,450); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setTitle(title); this.createMenu(); this.add(welcomePanel); if(!data.exists() try new FileWriter(data); catc
5、h (IOException e) this.setVisible(true); private void createMenu() JMenuBar menubar = new JMenuBar(); JMenu manageMenu = new JMenu(管理); menubar.add(manageMenu); JMenu helpMenu = new JMenu(幫助); menubar.add(helpMenu); JMenuItem m_add = new JMenuItem(添加); JMenuItem m_manage = new JMenuItem(查看); JMenuIt
6、em m_modify = new JMenuItem(修改); JMenuItem m_export = new JMenuItem(導(dǎo)出); JMenuItem m_exit = new JMenuItem(退出); manageMenu.add(m_add); manageMenu.add(m_modify); manageMenu.add(m_manage); manageMenu.addSeparator(); manageMenu.add(m_export); manageMenu.addSeparator(); manageMenu.add(m_exit); m_add.addA
7、ctionListener(new ActionListener() public void actionPerformed(ActionEvent e) resetPanel(insertPanel); ); m_modify.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) resetPanel(modifyPanel); ); m_manage.addActionListener(new ActionListener() public void actionPerformed
8、(ActionEvent e) resetPanel(displayPanel); ); m_export.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JFileChooser chooser = new JFileChooser(); int choose = chooser.showSaveDialog(currentContainer); if(choose=JFileChooser.APPROVE_OPTION) boolean flag = exportData(c
9、hooser.getSelectedFile(); if(flag) JOptionPane.showMessageDialog(currentContainer, 數(shù)據(jù)已經(jīng)成功導(dǎo)出到指定文件!,導(dǎo)出成功,JOptionPane.PLAIN_MESSAGE); else JOptionPane.showMessageDialog(currentContainer, 無法導(dǎo)出到指定文件!,導(dǎo)出失敗,JOptionPane.ERROR_MESSAGE); ); m_exit.addActionListener(new ActionListener() public void actionPerfo
10、rmed(ActionEvent e) System.exit(0); ); this.setJMenuBar(menubar); private void resetPanel(JPanel panel) this.setVisible(false); this.getContentPane().removeAll(); if(panel=displayPanel) loadData(); this.getContentPane().add(panel); this.setVisible(true); private JPanel createWelcomePanel() JPanel pa
11、nel = new JPanel(); JLabel title = new JLabel(學(xué)生成績管理系統(tǒng) ); title.setFont(headFont); panel.add(title); panel.setVisible(true); return panel; private JPanel createInsertPanel() JPanel panel = new JPanel(); panel.setLayout(null); panel.setFocusable(true); JLabel title = new JLabel(錄入學(xué)生成績); title.setFont
12、(headFont); panel.add(title); title.setBounds(175, 25, 120, 30); final String label = 學(xué)號,姓名,C語言,Java程序設(shè)計,高數(shù),英語; final JTextField textField = new JTextFieldlabel.length; for(int i=0;ilabel.length;i+) JLabel titleLabel = new JLabel(labeli + :); titleLabel.setBounds(100, 80 + 40*i, 120, 30); panel.add(
13、titleLabel); textFieldi = new JTextField(20); textFieldi.setBounds(220, 80 + 40*i, 150, 24); panel.add(textFieldi); textField0.addFocusListener(new FocusListener() public void focusGained(FocusEvent e) public void focusLost(FocusEvent e) boolean flag = true; String s = ; if(!checkString(textField0.g
14、etText() flag = false; s = 學(xué)號不能為空!n; if(flag) if(!checkStudentNo(textField0.getText().trim() flag = false; s = 系統(tǒng)已存在該學(xué)號的信息!n; if(!flag) JOptionPane.showMessageDialog(currentContainer, s, 錯誤, JOptionPane.ERROR_MESSAGE); ); JButton addBtn = new JButton(錄入信息); panel.add(addBtn); addBtn.setBounds(140, 3
15、40, 105, 30); addBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int choose = JOptionPane.showConfirmDialog(currentContainer, 確認(rèn)要錄入信息嗎?,錄入確認(rèn),JOptionPane.YES_NO_OPTION); if(choose=JOptionPane.NO_OPTION)return; boolean flag = true; String s = 錄入過程中發(fā)生了錯誤!n; for(int
16、 i=0; i2; i+) if(!checkString(textFieldi.getText() flag = false; s = s + labeli + 未填寫!n; for(int i=2; ilabel.length; i+) if(!checkInteger(textFieldi.getText() flag = false; s = s + labeli + 成績未填寫或輸入不是整數(shù)!n; if(flag) Student student = new Student(); student.setNo(textField0.getText().trim(); if(!check
17、StudentNo(student.getNo() flag = false; s = s + 系統(tǒng)中已存在相同學(xué)號的信息!n; else student.setName(textField1.getText().trim(); student.setAsmScore(Integer.parseInt(textField2.getText().trim(); student.setJavaScore(Integer.parseInt(textField3.getText().trim(); student.setNetScore(Integer.parseInt(textField4.getT
18、ext().trim(); student.setOsScore(Integer.parseInt(textField5.getText().trim(); flag = insertData(student); if(flag) JOptionPane.showMessageDialog(currentContainer, 錄入成功!,錄入成功,JOptionPane.PLAIN_MESSAGE); clearForm(textField); else JOptionPane.showMessageDialog(currentContainer, s,錄入失敗,JOptionPane.ERR
19、OR_MESSAGE); ); JButton clearBtn = new JButton(清空); panel.add(clearBtn); clearBtn.setBounds(280, 340, 60, 30); clearBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) clearForm(textField); ); panel.setVisible(true); return panel; private JPanel createDisplayPanel()
20、 JPanel panel = new JPanel(); panel.setVisible(true); panel.add(new JLabel(學(xué)號:); final JTextField textField = new JTextField(10); panel.add(textField); panel.add(new JLabel(); JButton searchBtn = new JButton(查詢); panel.add(searchBtn); searchBtn.addActionListener(new ActionListener() public void acti
21、onPerformed(ActionEvent e) String no = textField.getText(); if(!checkString(no) JOptionPane.showMessageDialog(currentContainer, 未輸入學(xué)號!,查詢失敗,JOptionPane.ERROR_MESSAGE); return; if(checkStudentNo(no) JOptionPane.showMessageDialog(currentContainer, 不存在該學(xué)號的學(xué)生信息!,查詢失敗,JOptionPane.ERROR_MESSAGE); return;
22、loadStudentInfo(no); ); JButton deleteBtn = new JButton(刪除); panel.add(deleteBtn); deleteBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String no = textField.getText(); if(!checkString(no) JOptionPane.showMessageDialog(currentContainer, 未輸入學(xué)號!,刪除失敗,JOptionPane.
23、ERROR_MESSAGE); return; if(checkStudentNo(no) JOptionPane.showMessageDialog(currentContainer, 不存在該學(xué)號的學(xué)生信息!,刪除失敗,JOptionPane.ERROR_MESSAGE); return; int choose = JOptionPane.showConfirmDialog(currentContainer, 確定要刪除學(xué)號為 + no + 的學(xué)生的信息嗎?,刪除確認(rèn),JOptionPane.YES_NO_OPTION); if(choose=JOptionPane.NO_OPTION)r
24、eturn; boolean flag = deleteData(no); if(!flag) JOptionPane.showMessageDialog(currentContainer, 刪除過程中發(fā)生了錯誤!,刪除失敗,JOptionPane.ERROR_MESSAGE); return; resetPanel(displayPanel); JOptionPane.showMessageDialog(currentContainer, 已經(jīng)刪除了該學(xué)號學(xué)生的信息!,刪除成功,JOptionPane.PLAIN_MESSAGE); textField.setText(); ); JButt
25、on allBtn = new JButton(查看所有信息); panel.add(allBtn); allBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textField.setText(); loadData(); ); dataTable.setAutoscrolls(true); dataTable.setPreferredScrollableViewportSize(new Dimension(420, 300); panel.add(new JScroll
26、Pane(dataTable); panel.add(extraLabel); return panel; private void loadData() ArrayList dataList = displayDataList(); dataModel.update(dataList); dataModel.fireTableDataChanged(); dataTable.updateUI(); extraLabel.setText(當(dāng)前系統(tǒng)中共有 + dataList.size() + 條信息); private void loadStudentInfo(String no) dataM
27、odel.update(selectStudent(no); dataModel.fireTableDataChanged(); dataTable.updateUI(); extraLabel.setText(當(dāng)前正在查詢學(xué)號為 + no + 的學(xué)生信息); private JPanel createModifyPanel() JPanel panel = new JPanel(); panel.setLayout(null); panel.setFocusable(true); JLabel title = new JLabel(修改學(xué)生成績); title.setFont(headFon
28、t); panel.add(title); title.setBounds(175, 25, 120, 30); final String label = 學(xué)號,姓名,C語言,Java程序設(shè)計,高數(shù),英語; final JTextField textField = new JTextFieldlabel.length; for(int i=0;ilabel.length;i+) JLabel titleLabel = new JLabel(labeli + :); titleLabel.setBounds(100, 80 + 40*i, 120, 30); panel.add(titleLab
29、el); textFieldi = new JTextField(20); textFieldi.setBounds(220, 80 + 40*i, 150, 24); panel.add(textFieldi); textField0.addFocusListener(new FocusListener() public void focusGained(FocusEvent e) public void focusLost(FocusEvent e) boolean flag = true; String s = ; if(!checkString(textField0.getText()
30、 flag = false; s = 學(xué)號不能為空!n; if(flag) Student student = selectStudent(textField0.getText().trim(); if(student = null) flag = false; s = 系統(tǒng)中不存在該學(xué)號的信息!n; else textField1.setText(student.getName(); textField2.setText(String.valueOf(student.getAsmScore(); textField3.setText(String.valueOf(student.getJav
31、aScore(); textField4.setText(String.valueOf(student.getNetScore(); textField5.setText(String.valueOf(student.getOsScore(); if(!flag) JOptionPane.showMessageDialog(currentContainer, s, 錯誤, JOptionPane.ERROR_MESSAGE); clearForm(textField); ); JButton modifyBtn = new JButton(修改信息); panel.add(modifyBtn)
32、; modifyBtn.setBounds(140, 340, 105, 30); modifyBtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int choose = JOptionPane.showConfirmDialog(currentContainer, 確認(rèn)要修改信息嗎?,修改確認(rèn),JOptionPane.YES_NO_OPTION); if(choose=JOptionPane.NO_OPTION)return; boolean flag = true; S
33、tring s = ; if(!checkString(textField0.getText() flag = false; s = 學(xué)號不能為空!n; if(flag) Student student = selectStudent(textField0.getText().trim(); if(student = null) flag = false; s = 系統(tǒng)中不存在該學(xué)號的信息!n; else student.setName(textField1.getText().trim(); student.setAsmScore(Integer.parseInt(textField2.ge
34、tText().trim(); student.setJavaScore(Integer.parseInt(textField3.getText().trim(); student.setNetScore(Integer.parseInt(textField4.getText().trim(); student.setOsScore(Integer.parseInt(textField5.getText().trim(); flag = modifyData(student); if(flag) JOptionPane.showMessageDialog(currentContainer, 修
35、改成功!,修改成功,JOptionPane.PLAIN_MESSAGE); clearForm(textField); else JOptionPane.showMessageDialog(currentContainer, s,修改失敗,JOptionPane.ERROR_MESSAGE); ); JButton clearBtn = new JButton(清空); panel.add(clearBtn); clearBtn.setBounds(280, 340, 60, 30); clearBtn.addActionListener(new ActionListener() public
36、 void actionPerformed(ActionEvent e) clearForm(textField); ); panel.setVisible(true); return panel; private boolean exportData(File file) try if(file.equals(data)return false; FileWriter writer = new FileWriter(file); BufferedReader reader = new BufferedReader(new FileReader(data); String s; while(s
37、 = reader.readLine()!=null) writer.write(s+rn); reader.close(); writer.close(); return true; catch(Exception e) return false; private boolean insertData(Student student) try String result = student.getNo() + , + student.getName() +, + student.getAsmScore() + , + student.getJavaScore() + , + student.
38、getNetScore() + , + student.getOsScore() + rn; FileWriter writer = new FileWriter(data, true); writer.write(result); /* 關(guān)閉對象 */ writer.close(); return true; catch(Exception e) /* 遇到異常即操作失敗,返回false */ return false; private boolean modifyData(Student student) try BufferedReader reader = new BufferedRe
39、ader(new FileReader(data); String s,result = ; while(s = reader.readLine()!=null) String temp = s.split(,); if(student.getNo().equals(temp0) result = result + student.getNo() + , + student.getName() +, + student.getAsmScore() + , + student.getJavaScore() + , + student.getNetScore() + , + student.get
40、OsScore() + rn; else result = result + s + rn; reader.close(); FileWriter writer = new FileWriter(data); writer.write(result); writer.close(); return true; catch(Exception e) return false; private boolean deleteData(String no) try BufferedReader reader = new BufferedReader(new FileReader(data); Stri
41、ng s,result = ; while(s = reader.readLine()!=null) String temp = s.split(,); if(no.equals(temp0) continue; result = result + s + rn; reader.close(); FileWriter writer = new FileWriter(data); writer.write(result); writer.close(); return true; catch(Exception e) return false; private boolean checkStud
42、entNo(String no) try boolean flag = true; BufferedReader reader = new BufferedReader(new FileReader(data); String s; while(s=reader.readLine()!=null) if(no.equals(s.split(,)0) flag = false; break; reader.close(); return flag; catch(Exception e) return false; private boolean checkString(String text)
43、if(text.trim().length()=0) return false; return true; private boolean checkInteger(String text) if(text.trim().length()=0) return false; try Integer.parseInt(text); catch(Exception e) return false; return true; private void clearForm(JTextField textField) /* 清空傳來的文本框數(shù)組的內(nèi)容 */ for(int i=0; itextField.
44、length; i+) textFieldi.setText(); private Student selectStudent(String no) try BufferedReader reader = new BufferedReader(new FileReader(data); Student student = null; String s; while(s = reader.readLine()!=null) String temp = s.split(,); if(no.equals(temp0) student = new Student(); student.setNo(te
45、mp0); student.setName(temp1); student.setAsmScore(Integer.parseInt(temp2); student.setJavaScore(Integer.parseInt(temp3); student.setNetScore(Integer.parseInt(temp4); student.setOsScore(Integer.parseInt(temp5); break; reader.close(); return student; catch(Exception e) return null; private ArrayList d
46、isplayDataList() ArrayList dataList = new ArrayList(); try BufferedReader reader = new BufferedReader(new FileReader(data); String s; while(s=reader.readLine()!=null) try String temp = s.split(,); Student student = new Student(); student.setNo(temp0); student.setName(temp1); student.setAsmScore(Integer.parseInt(temp2); student.setJavaScore(Integer.parseInt(temp3); student.setNetScore(Integer.parseInt(temp4); student.setOsScore(Integer.parseInt(temp5); dataList.add(student); catch(Exception e) continue
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年(全新版)中國高純硫酸錳行業(yè)競爭格局及前景趨勢預(yù)測報告
- 2025-2030年中國阻燃纖維板行業(yè)市場發(fā)展調(diào)研及投資潛力分析報告
- 二零二五版廠房租賃安全協(xié)議模板(含消防安全)3篇
- 公路養(yǎng)護(hù)工程養(yǎng)護(hù)技術(shù)創(chuàng)新實踐考核試卷
- 園林綠化金屬工具的節(jié)能效果評估考核試卷
- 噴槍在城市雕塑涂裝的應(yīng)用考核試卷
- 寶石開采的智能化與自動化考核試卷
- 養(yǎng)鴨場環(huán)境監(jiān)測與調(diào)控考核試卷
- 2025年度運動場地租賃合同(詳細(xì))3篇
- 勞務(wù)派遣服務(wù)的市場需求預(yù)測與應(yīng)對措施考核試卷
- 2024年南京鐵道職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及答案解析
- 2023年信息處理技術(shù)員教程
- 稽核管理培訓(xùn)
- 電梯曳引機(jī)生銹處理方案
- 電力電纜故障分析報告
- 中國電信網(wǎng)絡(luò)資源管理系統(tǒng)介紹
- 2024年浙江首考高考選考技術(shù)試卷試題真題(答案詳解)
- 《品牌形象設(shè)計》課件
- 倉庫管理基礎(chǔ)知識培訓(xùn)課件1
- 藥品的收貨與驗收培訓(xùn)課件
- GH-T 1388-2022 脫水大蒜標(biāo)準(zhǔn)規(guī)范
評論
0/150
提交評論