javafx教室管理系统
时间: 2023-07-14 19:53:37 浏览: 125
JavaFX是一个用于构建富客户端应用程序的UI框架,可以方便地实现一个教室管理系统。下面是一个简单的教室管理系统的示例:
首先,我们需要创建一个教室类Room,包含教室的基本信息,例如名称、位置、容量等:
```
public class Room {
private String name;
private String location;
private int capacity;
// getter and setter methods
}
```
然后,我们需要创建一个UI界面,用来显示教室信息并提供添加、删除、编辑等操作。可以使用JavaFX的TableView组件来显示教室列表:
```
public class ClassroomManagementSystem extends Application {
private TableView<Room> tableView = new TableView<>();
private ObservableList<Room> roomList = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
// create UI components
TableColumn<Room, String> nameColumn = new TableColumn<>("Name");
TableColumn<Room, String> locationColumn = new TableColumn<>("Location");
TableColumn<Room, Integer> capacityColumn = new TableColumn<>("Capacity");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
locationColumn.setCellValueFactory(new PropertyValueFactory<>("location"));
capacityColumn.setCellValueFactory(new PropertyValueFactory<>("capacity"));
tableView.getColumns().addAll(nameColumn, locationColumn, capacityColumn);
Button addButton = new Button("Add");
addButton.setOnAction(e -> addRoom());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteRoom());
Button editButton = new Button("Edit");
editButton.setOnAction(e -> editRoom());
HBox buttonBox = new HBox(addButton, deleteButton, editButton);
VBox root = new VBox(tableView, buttonBox);
// load data
loadData();
// show UI
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addRoom() {
// show a dialog to get room information
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Add Room");
dialog.setHeaderText("Enter room information");
dialog.setContentText("Name:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
Room room = new Room();
room.setName(result.get());
// add room to the list and update UI
roomList.add(room);
tableView.setItems(roomList);
}
}
private void deleteRoom() {
// get selected room and remove it from the list
Room room = tableView.getSelectionModel().getSelectedItem();
if (room != null) {
roomList.remove(room);
}
}
private void editRoom() {
// show a dialog to edit room information
Room room = tableView.getSelectionModel().getSelectedItem();
if (room != null) {
TextInputDialog dialog = new TextInputDialog(room.getName());
dialog.setTitle("Edit Room");
dialog.setHeaderText("Enter new room information");
dialog.setContentText("Name:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
room.setName(result.get());
// update UI
tableView.refresh();
}
}
}
private void loadData() {
// load room data from database or file
Room room1 = new Room();
room1.setName("Room 101");
room1.setLocation("Building A");
room1.setCapacity(30);
Room room2 = new Room();
room2.setName("Room 102");
room2.setLocation("Building A");
room2.setCapacity(25);
roomList.addAll(room1, room2);
tableView.setItems(roomList);
}
public static void main(String[] args) {
launch(args);
}
}
```
在这个示例中,我们使用了TableView、Button、HBox和VBox等JavaFX组件来构建UI界面。当用户点击添加、删除或编辑按钮时,会弹出一个对话框来输入或修改教室信息。教室列表使用了ObservableList来存储数据,并使用PropertyValueFactory来绑定数据到TableView中。在loadData()方法中,我们可以从数据库或文件中加载教室数据。
这只是一个简单的教室管理系统示例,你可以根据自己的需求来扩展它,例如添加搜索、排序、筛选等功能。
阅读全文