使用FXMLLoader动态创建后,我很难让FXML Text组件更新。这个假设简单的程序要求用户输入掷骰数,根据此输入创建新骰子,使用随机数生成器滚动它们,对这些值求和,并提供按钮以重新滚动任何骰子想。每个模具都添加到主窗口(使用FXMLoader和自定义模具组件),显示其索引,滚动值以及要重新滚动特定模具时按下的按钮。模具的值存在并且更新很好。重新滚动按钮效果很好。但是,出于某种原因,每个骰子的索引都没有显示在窗口中 - 甚至最初都没有。有问题的Text组件(不显示任何值的那个)在自定义FXML文档中被标识为 fx:id =“number”,并且被标识为@ FXML私有文本号在自定义控制器中。
我假设问题与Text组件的getter和setter有关,我很幸运,名为 value 的Text组件有效。但是,如果是这种情况,由于我在名称等中检查了许多涉及 Property 的替代方案,因此我提到了正确的名称。
注意:我理解当按下重新滚动按钮时,不会更新骰子值的总和。我现在只关心正在更新的模具值。
该程序的主要FXML窗口是:
<GridPane fx:id="mainPane"
styleClass="mainFxmlClass"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.lgdor.dieSim.MainWindowController"
xmlns="http://javafx.com/javafx/8.0_40"
prefHeight="800.0"
prefWidth="800.0"
hgap="10"
vgap="10" >
<stylesheets>
<URL value="@mainwindow.css"/>
</stylesheets>
<padding>
<Insets top="25" right="25" bottom="10" left="25"/>
</padding>
<Text fx:id="welcomeText" text="Welcome to DieSim"
GridPane.columnIndex="0"
GridPane.rowIndex="0"
GridPane.columnSpan="5"/>
<Label fx:id="spinnerText"
text="Please input the number of dice you want to roll:"
GridPane.columnIndex="0"
GridPane.rowIndex="1"
GridPane.columnSpan="2"/>
<Spinner fx:id="spinner" editable="true"
GridPane.columnIndex="3"
GridPane.rowIndex="1" />
<HBox fx:id="buttonBox"
spacing="10"
alignment="center"
GridPane.columnIndex="5"
GridPane.rowIndex="1">
<Button text="Roll Dice" onAction="#createDice"/>
</HBox>
<Label fx:id="dieNumberText" text="The number of dice is: "
GridPane.columnIndex="0"
GridPane.rowIndex="2"
GridPane.columnSpan="2"/>
<Text fx:id="dieNumber" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Label fx:id="dieSumText" text="The sum of the dice is: "
GridPane.columnIndex="0"
GridPane.rowIndex="3"
GridPane.columnSpan="2"/>
<Text fx:id="sumNumber" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<VBox fx:id="rowBox"
spacing="10"
alignment="center"
GridPane.columnIndex="0"
GridPane.rowIndex="4"
GridPane.columnSpan="5" >
<Text text="This is where the list of dice goes..."/>
</VBox>
主要控制器是:
public class MainWindowController implements Initializable {
@FXML
Label spinnerText;
@FXML
Spinner spinner;
@FXML
HBox buttonBox;
@FXML
Text dieNumber;
@FXML
Text sumNumber;
@FXML
int numberOfDice = 0;
@FXML
int sumOfDice = 0;
@FXML
VBox rowBox;
private List<Die> dieList = new ArrayList<>();
private List<GridPane> rowList = new ArrayList<>();
public void createDice() throws IOException{
numberOfDice = (int)spinner.getValue();
for (int i=0;i<numberOfDice;i++){
Die die = new Die(i);
die.roll(null);
sumOfDice = sumOfDice + Integer.parseInt(die.getValue());
dieList.add(die);
rowBox.getChildren().add(die);
}
dieNumber.setText(String.valueOf(numberOfDice));
sumNumber.setText(String.valueOf(sumOfDice));
spinner.setVisible(false);
spinnerText.setVisible(false);
buttonBox.setVisible(false);
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 20));
dieNumber.setText("0");
sumNumber.setText(String.valueOf("0"));
spinnerText.setVisible(true);
spinner.setVisible(true);
buttonBox.setVisible(true);
}
}
在上面的for循环中动态创建的模具自定义组件是(问题组件是 fx:id =“number”):
<fx:root type="javafx.scene.layout.GridPane"
xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0_40" >
<HBox spacing="30" alignment="center" GridPane.columnIndex="0" GridPane.rowIndex="0" >
<Text fx:id="number" /><!--This is the problem component-->
<Text fx:id="value"/>
<Button fx:id="dieButton" text="Roll Dice" onAction="#roll" />
</HBox>
</fx:root>
及其动态创建的控制器是:
public class Die extends GridPane implements Initializable {
@FXML
private Text number;//This is the problem component ...
public String getNumber() {
return numberTextProperty().get();
}
public void setNumber(String number) {
numberTextProperty().set(number);
}
public StringProperty numberTextProperty() {
return number.textProperty();
}
@FXML
private Text value;
public String getValue() {
return valueTextProperty().get();
}
public void setValue(String value) {
valueTextProperty().set(value);
}
public StringProperty valueTextProperty() {
return value.textProperty();
}
public Die(int i) throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("DieGUI.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
this.number = new Text(String.valueOf(i));
this.setNumber(String.valueOf(1));
this.value = new Text();
this.setValue(String.valueOf(1));
fxmlLoader.load();
}
@FXML
public void roll(ActionEvent event){
this.setValue(String.valueOf((int)(6*Math.random()+1)));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
答案 0 :(得分:0)
问题是您创建了一个新的Text
实例,而不是配置已在FXML中创建的实例:
this.number = new Text(String.valueOf(i));
@FXML
注释的全部目的是指示该字段由FXMLLoader
初始化,因此总是错误地初始化注释的字段{{1 }}
请注意,这些字段在调用@FXML
期间由FXMLLoader
初始化,因此您需要在之前调用load()
方式。
所以你的构造函数应该是这样的:
load()