JavaFX写一个教室管理系统代码
时间: 2023-12-25 15:03:02 浏览: 82
这是一个简单的JavaFX教室管理系统示例代码,包括学生和课程信息的管理:
```
import javafx.application.Application;
import javafx.collections.FXCollections;
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.stage.Stage;
public class ClassroomManagementSystem extends Application {
private TableView<Student> studentTable;
private TableView<Course> courseTable;
private TextField studentIdField;
private TextField studentNameField;
private TextField courseIdField;
private TextField courseNameField;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("教室管理系统");
// 创建学生表格
studentTable = new TableView<>();
studentTable.setEditable(false);
TableColumn<Student, String> studentIdCol = new TableColumn<>("学号");
studentIdCol.setCellValueFactory(cellData -> cellData.getValue().idProperty());
TableColumn<Student, String> studentNameCol = new TableColumn<>("姓名");
studentNameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
studentTable.getColumns().addAll(studentIdCol, studentNameCol);
// 创建课程表格
courseTable = new TableView<>();
courseTable.setEditable(false);
TableColumn<Course, String> courseIdCol = new TableColumn<>("课程编号");
courseIdCol.setCellValueFactory(cellData -> cellData.getValue().idProperty());
TableColumn<Course, String> courseNameCol = new TableColumn<>("课程名称");
courseNameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
courseTable.getColumns().addAll(courseIdCol, courseNameCol);
// 创建添加学生和课程的表单
Label studentLabel = new Label("添加学生");
Label studentIdLabel = new Label("学号:");
Label studentNameLabel = new Label("姓名:");
studentIdField = new TextField();
studentNameField = new TextField();
Button addStudentButton = new Button("添加");
addStudentButton.setOnAction(e -> addStudent());
HBox studentHBox = new HBox(10);
studentHBox.setAlignment(Pos.CENTER_LEFT);
studentHBox.getChildren().addAll(studentIdLabel, studentIdField, studentNameLabel, studentNameField, addStudentButton);
Label courseLabel = new Label("添加课程");
Label courseIdLabel = new Label("课程编号:");
Label courseNameLabel = new Label("课程名称:");
courseIdField = new TextField();
courseNameField = new TextField();
Button addCourseButton = new Button("添加");
addCourseButton.setOnAction(e -> addCourse());
HBox courseHBox = new HBox(10);
courseHBox.setAlignment(Pos.CENTER_LEFT);
courseHBox.getChildren().addAll(courseIdLabel, courseIdField, courseNameLabel, courseNameField, addCourseButton);
// 创建主界面
VBox mainVBox = new VBox(10);
mainVBox.setPadding(new Insets(10, 10, 10, 10));
mainVBox.getChildren().addAll(studentLabel, studentHBox, studentTable, courseLabel, courseHBox, courseTable);
Scene scene = new Scene(mainVBox, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addStudent() {
String id = studentIdField.getText();
String name = studentNameField.getText();
if (!id.isEmpty() && !name.isEmpty()) {
Student student = new Student(id, name);
studentTable.getItems().add(student);
studentIdField.setText("");
studentNameField.setText("");
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("警告");
alert.setHeaderText(null);
alert.setContentText("学号和姓名不能为空!");
alert.showAndWait();
}
}
private void addCourse() {
String id = courseIdField.getText();
String name = courseNameField.getText();
if (!id.isEmpty() && !name.isEmpty()) {
Course course = new Course(id, name);
courseTable.getItems().add(course);
courseIdField.setText("");
courseNameField.setText("");
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("警告");
alert.setHeaderText(null);
alert.setContentText("课程编号和课程名称不能为空!");
alert.showAndWait();
}
}
public class Student {
private final String id;
private final String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public StringProperty idProperty() {
return new SimpleStringProperty(id);
}
public StringProperty nameProperty() {
return new SimpleStringProperty(name);
}
}
public class Course {
private final String id;
private final String name;
public Course(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public StringProperty idProperty() {
return new SimpleStringProperty(id);
}
public StringProperty nameProperty() {
return new SimpleStringProperty(name);
}
}
}
```
该示例使用了JavaFX的表格视图和表单控件来管理学生和课程信息,并包含添加学生和课程的表单和简单的输入验证。您可以根据需要进行修改和扩展。
阅读全文