java實現簡易的學籍管理系統
本文實例為大傢分享瞭java實現簡易的學籍管理系統的具體代碼,供大傢參考,具體內容如下
一、 代碼
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class TestStudentManager { private int rows = 0; private String[][] unit = new String[rows][5]; private String[] name = {"姓名", "語文", "數學", "外語", "總分"}; public JTable table; public static void main( String[] args ) { new TestStudentManager(); } TestStudentManager() { JFrame frame = new JFrame("模擬學生管理系統"); table = new JTable(unit, name); JPanel southPanel = new JPanel(); southPanel.add(new JLabel("添加學生數")); JButton calc = new JButton("計算成績"); JButton save = new JButton("保存學生信息"); JTextField input = new JTextField(5); southPanel.add(input); southPanel.add(calc); southPanel.add(save); frame.add(new JLabel("歡迎訪問學生管理系統"), BorderLayout.NORTH); frame.add(southPanel, BorderLayout.SOUTH); frame.add(new JScrollPane(table), BorderLayout.CENTER); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); input.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent e ) { rows = Integer.valueOf(input.getText()); unit = new String[rows][5]; table = new JTable(unit, name); System.out.println("xx"); frame.getContentPane().removeAll(); frame.add(new JScrollPane(table), BorderLayout.CENTER); frame.add(southPanel, BorderLayout.SOUTH); frame.add(new JLabel("歡迎訪問學生管理系統"), BorderLayout.NORTH); frame.validate(); table.setRowHeight(25); } }); calc.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent e ) { for (int i = 0; i < rows; i++) { double sum = 0; boolean flag = true; for (int j = 1; j <= 3; j++) { try { sum += Double.valueOf(unit[i][j].toString()); } catch (Exception ee) { flag = false; table.repaint(); } if (flag) { unit[i][4] = "" + sum; table.repaint(); } } } } }); save.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent e ) { try { write(); } catch (IOException e1) { e1.printStackTrace(); } } }); } void write() throws IOException { File f = new File("學生信息.txt"); FileWriter fw = new FileWriter(f); for (int i = 0; i < 5; i++) { fw.write(name[i] + "\t"); } fw.write("\r\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < 5; j++) { fw.write(unit[i][j] + "\t"); } fw.write("\r\n"); } fw.close(); JOptionPane.showMessageDialog(null, "保存成功,存放至:學生信息.txt"); } }
二、運行
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。