JAVA GUI 学生信息管理系统(Swing)(详细)
项目名称: 学生信息管理系统
样品展示:
学生管理系统
系统简介(思维):
该项目用于学习巩固java知识,包含很多知识应用,(注意:本篇文章项目不含数据库内容,采取IO流代替写进文本,保存数据),与君共同成长学习,源代码加微:wxzhangguangpeng
技术需求:
- java基础知识
- 面向对象
- IO流
- 异常处理
- Swing程序设计
实现步骤(并不规范,个人思路):
第一步:注册界面
- 新建类:Register,继承JFrame类
- 窗体基础调整
- 新建标签
- 将标签加入到窗体容器中
- 如下
package StudentManagementSystem12; import javax.swing.*; import java.awt.*; public class Register extends JFrame{ public Register() { setTitle("StudentManagementSystem"); setBounds(600, 200, 350, 300);//窗体标题 setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭方式的设置 Container c1 = getContentPane();//将窗体变为容器 JPanel jPanel1 = new JPanel(new BorderLayout()); // 使用 BorderLayout布局管理器 JLabel jLabel1 = new JLabel("Register");//用标签写出标题 Font font = new Font("Arial", Font.PLAIN, 40);//调整字体大小和样式 jLabel1.setFont(font); jLabel1.setForeground(Color.BLACK); jLabel1.setHorizontalAlignment(SwingConstants.CENTER);//标题位置 jPanel1.add(jLabel1, BorderLayout.NORTH); // 将 JLabel 添加到北部 c1.add(jPanel1); setVisible(true);//显示窗体 } public static void main(String[] args) { new Register(); } }
- 添加用户名和密码的文本框和文字标签,用SpringLayout布局管理器来规划位置
- 添加三个按钮,也用SpringLayout布局管理器来规划位置
- 根据事件监听器的知识进行按钮的制定
- HomePage可以不设置监听器
- 完整的注册注册代码文件如下(账号文件位置需要自己修改)
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.HashMap; import java.util.Map; public class Register extends JFrame { public JTextField textField1; public JPasswordField textField2; public JPasswordField textField3; public Register() { setTitle("StudentManagementSystem"); setBounds(600, 200, 350, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c1 = getContentPane(); JPanel jPanel1 = new JPanel(new BorderLayout()); // 使用 BorderLayout JLabel jLabel1 = new JLabel("Register"); Font font = new Font("Arial", Font.PLAIN, 40); jLabel1.setFont(font); jLabel1.setForeground(Color.BLACK); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jPanel1.add(jLabel1, BorderLayout.NORTH); // 将 JLabel 添加到北部 SpringLayout layout = new SpringLayout(); JPanel panel = new JPanel(layout); JLabel label1 = new JLabel("Enter name:"); textField1 = new JTextField(20); JLabel label2 = new JLabel("Enter password:"); textField2 = new JPasswordField(20); JLabel label3 = new JLabel("Re-entered Password:"); textField3 = new JPasswordField(20); // 添加组件到 SpringLayout 面板 panel.add(label1); panel.add(textField1); panel.add(label2); panel.add(textField2); panel.add(label3); panel.add(textField3); // 定义组件之间的约束关系 layout.putConstraint(SpringLayout.WEST, label1, 5, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, label1, 5, SpringLayout.NORTH, panel); layout.putConstraint(SpringLayout.WEST, textField1, 64, SpringLayout.EAST, label1); layout.putConstraint(SpringLayout.NORTH, textField1, 5, SpringLayout.NORTH, panel); layout.putConstraint(SpringLayout.WEST, label2, 5, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, label2, 10, SpringLayout.SOUTH, textField1); layout.putConstraint(SpringLayout.WEST, textField2, 40, SpringLayout.EAST, label2); layout.putConstraint(SpringLayout.NORTH, textField2, 10, SpringLayout.SOUTH, textField1); layout.putConstraint(SpringLayout.WEST, label3, 5, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, label3, 10, SpringLayout.SOUTH, textField2); layout.putConstraint(SpringLayout.WEST, textField3, 5, SpringLayout.EAST, label3); layout.putConstraint(SpringLayout.NORTH, textField3, 10, SpringLayout.SOUTH, textField2); SpringLayout layout1 = new SpringLayout(); JPanel p1 = new JPanel(layout); JButton jb1 = new JButton("Sign up"); jb1.addActionListener(new jb1action()); JButton jb2 = new JButton("Clean"); jb2.addActionListener(new jb2action()); JButton jb3 = new JButton("HomePage"); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new HomePage(); } }); panel.add(jb1); panel.add(jb2); panel.add(jb3); layout.putConstraint(SpringLayout.WEST, jb1, 30, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, jb1, 10, SpringLayout.SOUTH, textField3); layout.putConstraint(SpringLayout.WEST, jb2, 20, SpringLayout.EAST, jb1); layout.putConstraint(SpringLayout.NORTH, jb2, 10, SpringLayout.SOUTH, textField3); layout.putConstraint(SpringLayout.WEST, jb3, 20, SpringLayout.EAST, jb2); layout.putConstraint(SpringLayout.NORTH, jb3, 10, SpringLayout.SOUTH, textField3); jPanel1.add(panel, BorderLayout.CENTER); // 将 SpringLayout 的 panel 添加到中部 c1.add(jPanel1); setVisible(true); } private class jb1action implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { Map UInfor = new HashMap(); String filePath = "D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\Register.txt"; try { // 创建一个文件读取对象 FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { String[] parts = line.split(" : "); if (parts.length == 2) { String key = parts[0]; String value = parts[1]; UInfor.put(key, value); } } bufferedReader.close(); } catch (IOException ex) { System.out.println("读取文件时出现错误:" + ex.getMessage()); } String getUseName = textField1.getText(); char[] ch1 = textField2.getPassword(); String getGetPassword1 = new String(ch1); char[] ch2 = textField3.getPassword(); String getGetPassword2 = new String(ch2); if (getGetPassword1.equals(getGetPassword2)){ JOptionPane.showMessageDialog(Register.this,"Register successfully!"); UInfor.put(getUseName,getGetPassword1); File file = new File("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\Register.txt"); FileWriter fileWriter = null; try { fileWriter = new FileWriter(file); } catch (IOException ex) { throw new RuntimeException(ex); } BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); for (Map.Entry entry : UInfor.entrySet()) { String line = entry.getKey() + " : " + entry.getValue(); try { bufferedWriter.write(line); } catch (IOException ex) { throw new RuntimeException(ex); } try { bufferedWriter.newLine(); } catch (IOException ex) { throw new RuntimeException(ex); } } try { bufferedWriter.close(); } catch (IOException ex) { throw new RuntimeException(ex); } System.out.println("HashMap内容已写入文件:" + file); dispose(); }else{ JOptionPane.showMessageDialog(Register.this,"Password and Re-entered Password do not match!"); textField2.setText(""); textField3.setText(""); } } } private class jb2action implements ActionListener { @Override public void actionPerformed(ActionEvent e) { textField1.setText(""); textField2.setText(""); textField3.setText(""); } } }
第二步:登陆界面
- 新建类:Login
- 窗体设置,组件设置
- 读取账号文本
- 判断用户名和密码
- 登陆成功,转换界面
- 完整代码如下:
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.HashMap; import java.util.Map; public class Login extends JFrame { public JTextField textField1; public JPasswordField textField2; public Login(){ setTitle("StudentManagementSystem"); setBounds(600, 200, 350, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c1 = getContentPane(); JPanel jPanel1 = new JPanel(new BorderLayout()); // 使用 BorderLayout JLabel jLabel1 = new JLabel("Login"); Font font = new Font("Arial", Font.PLAIN, 40); jLabel1.setFont(font); jLabel1.setForeground(Color.BLACK); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jPanel1.add(jLabel1, BorderLayout.NORTH); // 将 JLabel 添加到北部 SpringLayout layout = new SpringLayout(); JPanel panel = new JPanel(layout); JLabel label1 = new JLabel("Enter name:"); textField1 = new JTextField(20); JLabel label2 = new JLabel("Enter password:"); textField2 = new JPasswordField(20); // 添加组件到 SpringLayout 面板 panel.add(label1); panel.add(textField1); panel.add(label2); panel.add(textField2); // 定义组件之间的约束关系 layout.putConstraint(SpringLayout.WEST, label1, 5, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, label1, 5, SpringLayout.NORTH, panel); layout.putConstraint(SpringLayout.WEST, textField1, 64, SpringLayout.EAST, label1); layout.putConstraint(SpringLayout.NORTH, textField1, 5, SpringLayout.NORTH, panel); layout.putConstraint(SpringLayout.WEST, label2, 5, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, label2, 10, SpringLayout.SOUTH, textField1); layout.putConstraint(SpringLayout.WEST, textField2, 40, SpringLayout.EAST, label2); layout.putConstraint(SpringLayout.NORTH, textField2, 10, SpringLayout.SOUTH, textField1); SpringLayout layout1 = new SpringLayout(); JPanel p1 = new JPanel(layout); JButton jb1 = new JButton("Login up"); jb1.addActionListener(new jb1action()); JButton jb2 = new JButton("Clean"); jb2.addActionListener(new jb2action()); JButton jb3 = new JButton("Sign up"); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Register(); } }); panel.add(jb1); panel.add(jb2); panel.add(jb3); layout.putConstraint(SpringLayout.WEST, jb1, 30, SpringLayout.WEST, panel); layout.putConstraint(SpringLayout.NORTH, jb1, 10, SpringLayout.SOUTH, textField2); layout.putConstraint(SpringLayout.WEST, jb2, 20, SpringLayout.EAST, jb1); layout.putConstraint(SpringLayout.NORTH, jb2, 10, SpringLayout.SOUTH, textField2); layout.putConstraint(SpringLayout.WEST, jb3, 20, SpringLayout.EAST, jb2); layout.putConstraint(SpringLayout.NORTH, jb3, 10, SpringLayout.SOUTH, textField2); jPanel1.add(panel, BorderLayout.CENTER); // 将 SpringLayout 的 panel 添加到中部 c1.add(jPanel1); setVisible(true); } private class jb2action implements ActionListener { @Override public void actionPerformed(ActionEvent e) { textField1.setText(""); textField2.setText(""); } } private class jb1action implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String filePath = "D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\Register.txt"; Map UInfor = new HashMap(); FileReader fileReader; { try { fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { String[] parts = line.split(" : "); if (parts.length == 2) { String key = parts[0]; String value = parts[1]; UInfor.put(key, value); } } bufferedReader.close(); } catch (IOException ex) { System.out.println("读取文件时候出现错误:"+ex.getMessage()); } } String getUseName = textField1.getText(); char[] ch1 = textField2.getPassword(); String getGetPassword1 = new String(ch1); boolean flag = UInfor.containsKey(getUseName); if (flag && UInfor.get(getUseName).equals(getGetPassword1)){ JOptionPane.showMessageDialog(Login.this,"Login successfully!"); new HomePage(); dispose(); }else { JOptionPane.showMessageDialog(Login.this,"User name or password error!"); textField1.setText(""); textField2.setText(""); } } } }
第三步:学生类
package StudentManagementSystem; import java.io.Serializable; public class Student implements Serializable { private String name; private String age; private String id; private String grade; private String gender; public Student() { } public Student(String name, String age, String id, String grade, String gender) { this.name = name; this.age = age; this.id = id; this.grade = grade; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "name='" + name + '\'' + ", age='" + age + '\'' + ", id='" + id + '\'' + ", grade='" + grade + '\'' + ", gender='" + gender + '\''; } }
第四步: 添加学生界面
废话不多说,上代码(关于文件路径,请君自行修改):
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; public class AddStudent extends JFrame { public AddStudent() { setTitle("StudentManagementSystem"); setBounds(600, 200, 400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); Container container = getContentPane(); SpringLayout layout = new SpringLayout(); JPanel mainPanel = new JPanel(layout); JLabel titleLabel = new JLabel("Add Student"); Font titleFont = new Font("Arial", Font.PLAIN, 30); titleLabel.setFont(titleFont); titleLabel.setForeground(Color.BLACK); JLabel nameLabel = new JLabel("Enter your name:"); JTextField nameField = new JTextField(15); JLabel ageLabel = new JLabel("Select your age:"); String[] ages = {"Under 18", "18-30", "30-50", "50+"}; JComboBox ageComboBox = new JComboBox(ages); JLabel idLabel = new JLabel("Enter your ID:"); JTextField idField = new JTextField(15); JLabel gradeLabel = new JLabel("Select your grade:"); String[] grades = {"Freshman", "Sophomore", "Junior", "Senior"}; JComboBox gradeComboBox = new JComboBox(grades); JLabel genderLabel = new JLabel("Select your gender:"); JRadioButton maleRadioButton = new JRadioButton("Male"); JRadioButton femaleRadioButton = new JRadioButton("Female"); ButtonGroup genderGroup = new ButtonGroup(); genderGroup.add(maleRadioButton); genderGroup.add(femaleRadioButton); JPanel genderPanel = new JPanel(); genderPanel.add(maleRadioButton); genderPanel.add(femaleRadioButton); JButton addButton = new JButton("Add Student"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (nameField.getText().isEmpty()){ JOptionPane.showMessageDialog(AddStudent.this,"Name must be filled in!"); }else { String gender = ""; if (maleRadioButton.isSelected()) { gender = "Male"; } else if (femaleRadioButton.isSelected()) { gender = "Female"; } Student student = new Student(nameField.getText(), ageComboBox.getSelectedItem().toString(),idField.getText(),gradeComboBox.getSelectedItem().toString(),gender); System.out.println(student); // 从文件中读取 studentList //读取 ArrayList list; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { list = (ArrayList) ois.readObject(); System.out.println("Student list has been read from the file."); } catch (IOException | ClassNotFoundException xe) { xe.printStackTrace(); list = new ArrayList(); // 如果读取失败,则初始化一个新的列表 } //写入 list.add(student); System.out.println(list.toString()); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { oos.writeObject(list); System.out.println("Student list has been written to the file."); } catch (IOException ex) { ex.printStackTrace(); } JOptionPane.showMessageDialog(AddStudent.this,"Student added successfully!"); } } }); JButton HomePage = new JButton("HomePage"); HomePage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new HomePage(); dispose(); } }); mainPanel.add(titleLabel); mainPanel.add(nameLabel); mainPanel.add(nameField); mainPanel.add(ageLabel); mainPanel.add(ageComboBox); mainPanel.add(idLabel); mainPanel.add(idField); mainPanel.add(gradeLabel); mainPanel.add(gradeComboBox); mainPanel.add(genderLabel); mainPanel.add(genderPanel); mainPanel.add(addButton); mainPanel.add(HomePage); layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, titleLabel, 0, SpringLayout.HORIZONTAL_CENTER, mainPanel); layout.putConstraint(SpringLayout.NORTH, titleLabel, 10, SpringLayout.NORTH, mainPanel); layout.putConstraint(SpringLayout.WEST, nameLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, nameLabel, 50, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, nameField, 10, SpringLayout.EAST, nameLabel); layout.putConstraint(SpringLayout.NORTH, nameField, 46, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, ageLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, ageLabel, 30, SpringLayout.SOUTH, nameLabel); layout.putConstraint(SpringLayout.WEST, ageComboBox, 10, SpringLayout.EAST, ageLabel); layout.putConstraint(SpringLayout.NORTH, ageComboBox, 26, SpringLayout.SOUTH, nameLabel); layout.putConstraint(SpringLayout.WEST, idLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, idLabel, 30, SpringLayout.SOUTH, ageLabel); layout.putConstraint(SpringLayout.WEST, idField, 10, SpringLayout.EAST, idLabel); layout.putConstraint(SpringLayout.NORTH, idField, 26, SpringLayout.SOUTH, ageLabel); layout.putConstraint(SpringLayout.WEST, gradeLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, gradeLabel, 30, SpringLayout.SOUTH, idLabel); layout.putConstraint(SpringLayout.WEST, gradeComboBox, 10, SpringLayout.EAST, gradeLabel); layout.putConstraint(SpringLayout.NORTH, gradeComboBox, 26, SpringLayout.SOUTH, idLabel); layout.putConstraint(SpringLayout.WEST, genderLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, genderLabel, 30, SpringLayout.SOUTH, gradeLabel); layout.putConstraint(SpringLayout.WEST, genderPanel, 10, SpringLayout.EAST, genderLabel); layout.putConstraint(SpringLayout.NORTH, genderPanel, 26, SpringLayout.SOUTH, gradeLabel); layout.putConstraint(SpringLayout.WEST, addButton, 70, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, addButton, 30, SpringLayout.SOUTH, genderPanel); layout.putConstraint(SpringLayout.WEST, HomePage, 130, SpringLayout.WEST, addButton); layout.putConstraint(SpringLayout.NORTH, HomePage, 30, SpringLayout.SOUTH, genderPanel); container.add(mainPanel); setVisible(true); } }
第五步: 展现学生信息界面
废话不多说,上代码(关于文件路径,请君自行修改):
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; public class CheckStudent extends JFrame { private String text = "Show : \n"; private JTextArea textArea; private JTextField nameField; public CheckStudent(){ setTitle("StudentManagementSystem"); setBounds(600, 200, 500, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c1 = getContentPane(); SpringLayout layout = new SpringLayout(); JPanel mainPanel = new JPanel(layout); JLabel titleLabel = new JLabel("Check Student"); Font font = new Font("Arial", Font.PLAIN, 40); titleLabel.setFont(font); titleLabel.setForeground(Color.BLACK); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); JLabel nameLabel = new JLabel("Enter the query name:"); nameField = new JTextField(7); JButton jb1 = new JButton(" Query"); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { text = "Show : \n"; ArrayList list; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { list = (ArrayList) ois.readObject(); System.out.println("Student list has been read from the file."); } catch (IOException | ClassNotFoundException xe) { xe.printStackTrace(); list = new ArrayList(); // 如果读取失败,则初始化一个新的列表 } String namecheck = nameField.getText(); for (Student student : list) { if (student.getName().equals(namecheck)){ text += student.toString() + "\n"; } } if (text.equals("Show : \n")){ JOptionPane.showMessageDialog(CheckStudent.this,"Students with this name can't be found!"); } textArea.setText(text); } }); JButton jb2 = new JButton("Show all"); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ArrayList list; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { list = (ArrayList) ois.readObject(); System.out.println("Student list has been read from the file."); } catch (IOException | ClassNotFoundException xe) { xe.printStackTrace(); list = new ArrayList(); // 如果读取失败,则初始化一个新的列表 } text = "Show : \n"; for (Student student : list) { text += student.toString() + "\n"; } System.out.println(text); textArea.setText(text); } }); JButton jb3 = new JButton("Clean"); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { nameField.setText(""); } }); textArea = new JTextArea(12,51); textArea.setText(text); JScrollPane scrollPane = new JScrollPane(textArea); JButton jb4 = new JButton("HomePage"); jb4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new HomePage(); dispose(); } }); mainPanel.add(titleLabel); mainPanel.add(nameLabel); mainPanel.add(nameField); mainPanel.add(jb1); mainPanel.add(jb2); mainPanel.add(jb3); mainPanel.add(scrollPane); mainPanel.add(jb4); layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, titleLabel, 0, SpringLayout.HORIZONTAL_CENTER, mainPanel); layout.putConstraint(SpringLayout.NORTH, titleLabel, 10, SpringLayout.NORTH, mainPanel); layout.putConstraint(SpringLayout.WEST, nameLabel, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, nameLabel, 70, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, nameField, 10, SpringLayout.EAST, nameLabel); layout.putConstraint(SpringLayout.NORTH, nameField, 70, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, jb1, 230, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, jb1, 65, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, jb2, 10, SpringLayout.EAST, jb1); layout.putConstraint(SpringLayout.NORTH, jb2, 65, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, jb3, 10, SpringLayout.EAST, jb2); layout.putConstraint(SpringLayout.NORTH, jb3, 65, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, scrollPane, 30, SpringLayout.NORTH, nameLabel); layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, jb4, 0, SpringLayout.HORIZONTAL_CENTER, mainPanel); layout.putConstraint(SpringLayout.NORTH, jb4, 200, SpringLayout.NORTH, scrollPane); c1.add(mainPanel); setVisible(true); } }
第六步: 删除学生信息界面
废话不多说,上代码(关于文件路径,请君自行修改):
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; import java.util.Iterator; public class DeleteStudentInfo extends JFrame { private JTextField nameField; public DeleteStudentInfo(){ setTitle("StudentManagementSystem"); setBounds(600, 200, 500, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c1 = getContentPane(); SpringLayout layout = new SpringLayout(); JPanel mainPanel = new JPanel(layout); JLabel titleLabel = new JLabel("Delete Student Info"); Font font = new Font("Arial", Font.PLAIN, 40); titleLabel.setFont(font); titleLabel.setForeground(Color.BLACK); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); JLabel nameLabel = new JLabel("Enter the delete name:"); nameField = new JTextField(17); JButton jb1 = new JButton("Delete"); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ArrayList list; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { list = (ArrayList) ois.readObject(); System.out.println("Student list has been read from the file."); } catch (IOException | ClassNotFoundException xe) { xe.printStackTrace(); list = new ArrayList(); // 如果读取失败,则初始化一个新的列表 } String namedelete = nameField.getText(); Iterator it = list.iterator(); boolean flag = true; while (it.hasNext()){ Student stu = (Student) it.next(); if (namedelete.equals(stu.getName())){ it.remove(); flag = false; try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\jdk\\code\\GUI\\src\\StudentManagementSystem\\students.dat"))) { oos.writeObject(list); System.out.println("Student list has been written to the file."); } catch (IOException ex) { ex.printStackTrace(); } JOptionPane.showMessageDialog(DeleteStudentInfo.this,"Delete succeeded!"); } } if (flag){ JOptionPane.showMessageDialog(DeleteStudentInfo.this,"Delete failed, this person cannot be found!"); } } }); JButton jb2 = new JButton("HomePage"); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new HomePage(); dispose(); } }); mainPanel.add(titleLabel); mainPanel.add(nameLabel); mainPanel.add(nameField); mainPanel.add(jb1); mainPanel.add(jb2); layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, titleLabel, 0, SpringLayout.HORIZONTAL_CENTER, mainPanel); layout.putConstraint(SpringLayout.NORTH, titleLabel, 10, SpringLayout.NORTH, mainPanel); layout.putConstraint(SpringLayout.WEST, nameLabel, 90, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, nameLabel, 70, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, nameField, 10, SpringLayout.EAST, nameLabel); layout.putConstraint(SpringLayout.NORTH, nameField, 70, SpringLayout.NORTH, titleLabel); layout.putConstraint(SpringLayout.WEST, jb1, 140, SpringLayout.WEST, mainPanel); layout.putConstraint(SpringLayout.NORTH, jb1, 70, SpringLayout.NORTH, nameLabel); layout.putConstraint(SpringLayout.WEST, jb2, 40, SpringLayout.EAST, jb1); layout.putConstraint(SpringLayout.NORTH, jb2, 70, SpringLayout.NORTH, nameLabel); c1.add(mainPanel); setVisible(true); } }
第七步: 主页功能界面
废话不多说,上代码(关于文件路径,请君自行修改):
package StudentManagementSystem; import StudentManagementSystem.Register; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class HomePage extends JFrame { public HomePage(){ setTitle("StudentManagementSystem"); setBounds(600, 200, 350, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c1 = getContentPane(); c1.setLayout(new GridLayout(5, 1)); // 设置为2行1列的GridLayout JButton jb1 = new JButton("Register"); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Register(); dispose(); } }); JButton jb2 = new JButton("Login"); jb2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Login(); dispose(); } }); JButton jb3 = new JButton("Add student"); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new AddStudent(); dispose(); } }); JButton jb4 = new JButton("CheckStudent"); jb4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new CheckStudent(); dispose(); } }); JButton jb5 = new JButton("Delete Student Info"); jb5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new DeleteStudentInfo(); dispose(); } }); c1.add(jb1); c1.add(jb2); c1.add(jb3); c1.add(jb4); c1.add(jb5); setVisible(true); } }
第七步: 运行界面
废话不多说,上代码(关于文件路径,请君自行修改):
package StudentManagementSystem; public class Game { public static void main(String[] args) { new Login(); } }
第七步:注意事项
- 将上述界面找一个idea逐一复制新建到一个文件夹中
- 路径记得自己更换
- 要原代码,需要指导弄的可以评论找我哦
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。