如何将组合框的字体大小应用于标签?

How to apply font sizes from combobox to label?

我正在使用 JavaFX 和 Scene Builder。
我在 window 中有带有字体大小和标签的组合框。

如何将所选尺寸应用于标签?

@FXML  private Label fontLabel;
@FXML  private ComboBox<String> size;

//create array of font sizes
ObservableList<String> fontSizes= FXCollections.observableArrayList("8", "10", 
"11", "12", "14", "16", "18","20", "24", "30", "36", "40", "48", "60", 
72");   


@Override
public void initialize(URL location, ResourceBundle resources) {
   size.setItems(fontSizes);
}

试试这个让你开始:

@FXML  private Label fontLabel;
@FXML  private ComboBox<String> size;

//create array of font sizes
ObservableList<String> fontSizes= FXCollections.observableArrayList("8", "10",
"11", "12", "14", "16", "18","20", "24", "30", "36", "40", "48", "60",
"72");


@FXML
public void initialize() {
    size.setItems(fontSizes);
    size.setOnAction(e-> updateLabel());
}

private void updateLabel() {
    double fontSize = Double.valueOf(size.getSelectionModel().getSelectedItem());
    fontLabel.setFont(  new Font(fontSize ));
}