用Javafx写学生管理系统录入学生管理的程序
时间: 2023-10-06 22:06:05 浏览: 66
好的,下面是一个简单的JavaFX学生管理系统的实现,包括学生信息的录入和显示:
```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.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class StudentManagementSystem extends Application {
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 600;
private VBox root;
private GridPane inputPane;
private Label titleLabel;
private Label nameLabel;
private TextField nameField;
private Label idLabel;
private TextField idField;
private Label gradeLabel;
private TextField gradeField;
private Button addButton;
private TableView<Student> studentTable;
private StudentDataModel studentDataModel;
@Override
public void start(Stage primaryStage) throws Exception {
studentDataModel = new StudentDataModel();
initUI();
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
primaryStage.setTitle("学生管理系统");
primaryStage.setScene(scene);
primaryStage.show();
}
private void initUI() {
root = new VBox();
root.setSpacing(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.TOP_CENTER);
titleLabel = new Label("学生管理系统");
titleLabel.setFont(new Font(24));
root.getChildren().add(titleLabel);
inputPane = new GridPane();
inputPane.setHgap(10);
inputPane.setVgap(10);
inputPane.setAlignment(Pos.CENTER);
nameLabel = new Label("姓名:");
nameField = new TextField();
idLabel = new Label("学号:");
idField = new TextField();
gradeLabel = new Label("成绩:");
gradeField = new TextField();
addButton = new Button("添加学生");
addButton.setOnAction(event -> {
Student student = new Student(nameField.getText(), idField.getText(), Double.parseDouble(gradeField.getText()));
studentDataModel.addStudent(student);
nameField.clear();
idField.clear();
gradeField.clear();
});
inputPane.addRow(0, nameLabel, nameField);
inputPane.addRow(1, idLabel, idField);
inputPane.addRow(2, gradeLabel, gradeField);
inputPane.add(addButton, 0, 3, 2, 1);
root.getChildren().add(inputPane);
studentTable = new TableView<>();
TableColumn<Student, String> nameColumn = new TableColumn<>("姓名");
nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
TableColumn<Student, String> idColumn = new TableColumn<>("学号");
idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty());
TableColumn<Student, Double> gradeColumn = new TableColumn<>("成绩");
gradeColumn.setCellValueFactory(cellData -> cellData.getValue().gradeProperty().asObject());
studentTable.getColumns().addAll(nameColumn, idColumn, gradeColumn);
studentTable.setItems(studentDataModel.getStudentList());
HBox tableBox = new HBox();
tableBox.setSpacing(10);
tableBox.setAlignment(Pos.CENTER);
tableBox.getChildren().add(studentTable);
root.getChildren().add(tableBox);
}
public static void main(String[] args) {
launch(args);
}
}
class Student {
private String name;
private String id;
private double grade;
public Student(String name, String id, double grade) {
this.name = name;
this.id = id;
this.grade = grade;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getGrade() {
return grade;
}
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setGrade(double grade) {
this.grade = grade;
}
public StringProperty nameProperty() {
return new SimpleStringProperty(name);
}
public StringProperty idProperty() {
return new SimpleStringProperty(id);
}
public DoubleProperty gradeProperty() {
return new SimpleDoubleProperty(grade);
}
}
class StudentDataModel {
private ObservableList<Student> studentList;
public StudentDataModel() {
studentList = FXCollections.observableArrayList();
}
public void addStudent(Student student) {
studentList.add(student);
}
public ObservableList<Student> getStudentList() {
return studentList;
}
}
```
这个程序界面简单,功能齐全,可以添加学生信息并在表格中显示。你可以根据需要进行修改和扩展。
阅读全文