帮我用Javafx编写一个学生信息管理系统可以录入和删除信息
时间: 2023-09-13 12:04:36 浏览: 105
java编写的学生信息管理系统
5星 · 资源好评率100%
好的,我可以为您提供一些示例代码来帮助您编写一个基本的学生信息管理系统。在这里,我将使用JavaFX作为GUI框架。
首先,我们需要创建一个名为“Student”的Java类来表示学生对象,该类应包含以下属性:姓名、年龄、性别、学号、班级和联系方式。以下是示例代码:
```java
public class Student {
private String name;
private int age;
private String gender;
private String id;
private String clazz;
private String phone;
public Student(String name, int age, String gender, String id, String clazz, String phone) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
this.clazz = clazz;
this.phone = phone;
}
// Getter and Setter methods
}
```
接下来,我们需要创建一个名为“StudentManager”的Java类来管理学生对象,该类应包含以下方法:添加学生、删除学生、获取学生列表。以下是示例代码:
```java
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
private List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public List<Student> getStudents() {
return students;
}
}
```
现在,我们可以开始编写GUI代码。以下是示例代码:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class StudentManagementSystem extends Application {
private StudentManager manager = new StudentManager();
private TableView<Student> table = new TableView<>();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Create UI controls
TextField nameField = new TextField();
TextField ageField = new TextField();
TextField genderField = new TextField();
TextField idField = new TextField();
TextField clazzField = new TextField();
TextField phoneField = new TextField();
Button addButton = new Button("添加");
Button removeButton = new Button("删除");
// Create table columns
TableColumn<Student, String> nameColumn = new TableColumn<>("姓名");
nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
TableColumn<Student, Integer> ageColumn = new TableColumn<>("年龄");
ageColumn.setCellValueFactory(cellData -> cellData.getValue().ageProperty().asObject());
TableColumn<Student, String> genderColumn = new TableColumn<>("性别");
genderColumn.setCellValueFactory(cellData -> cellData.getValue().genderProperty());
TableColumn<Student, String> idColumn = new TableColumn<>("学号");
idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty());
TableColumn<Student, String> clazzColumn = new TableColumn<>("班级");
clazzColumn.setCellValueFactory(cellData -> cellData.getValue().clazzProperty());
TableColumn<Student, String> phoneColumn = new TableColumn<>("联系方式");
phoneColumn.setCellValueFactory(cellData -> cellData.getValue().phoneProperty());
table.getColumns().addAll(nameColumn, ageColumn, genderColumn, idColumn, clazzColumn, phoneColumn);
// Add event handlers
addButton.setOnAction(event -> {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = genderField.getText();
String id = idField.getText();
String clazz = clazzField.getText();
String phone = phoneField.getText();
Student student = new Student(name, age, gender, id, clazz, phone);
manager.addStudent(student);
table.getItems().add(student);
clearFields();
});
removeButton.setOnAction(event -> {
Student selectedStudent = table.getSelectionModel().getSelectedItem();
if (selectedStudent != null) {
manager.removeStudent(selectedStudent);
table.getItems().remove(selectedStudent);
}
});
// Create UI layout
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
root.setPadding(new Insets(10));
root.add(new Label("姓名:"), 0, 0);
root.add(nameField, 1, 0);
root.add(new Label("年龄:"), 0, 1);
root.add(ageField, 1, 1);
root.add(new Label("性别:"), 0, 2);
root.add(genderField, 1, 2);
root.add(new Label("学号:"), 0, 3);
root.add(idField, 1, 3);
root.add(new Label("班级:"), 0, 4);
root.add(clazzField, 1, 4);
root.add(new Label("联系方式:"), 0, 5);
root.add(phoneField, 1, 5);
root.add(addButton, 0, 6);
root.add(removeButton, 1, 6);
root.add(table, 0, 7, 2, 1);
// Create scene and show stage
Scene scene = new Scene(root, 800, 600);
primaryStage.setTitle("学生信息管理系统");
primaryStage.setScene(scene);
primaryStage.show();
}
private void clearFields() {
nameField.clear();
ageField.clear();
genderField.clear();
idField.clear();
clazzField.clear();
phoneField.clear();
}
}
```
在这个示例中,我们使用了JavaFX的TableView来显示学生信息列表,并使用GridPane布局来放置其他UI控件。当用户点击“添加”按钮时,我们从文本框中获取学生信息并将其添加到学生管理器中。当用户选择一行学生信息并点击“删除”按钮时,我们从学生管理器中删除该学生并从表格中删除该行。
请注意,这只是一个简单的示例,您可以根据自己的需要进行修改和扩展。
阅读全文