当前位置: 首页 > 知识库问答 >
问题:

JavaFX FXML文本组件控制器未更新

瞿兴朝
2023-03-14
<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);

}    
}
<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) {
}
}

共有1个答案

雍嘉勋
2023-03-14

问题是您创建了一个新的text实例,而不是配置FXML中已经创建的实例:

this.number = new Text(String.valueOf(i));

@fxml注释的全部用途是指示字段是由FXMLLoader初始化的,因此初始化注释@fxml的字段总是错误的。

这里请注意,这些字段是在调用load()期间由FXMLLoader初始化的,因此在以任何方式配置它们之前都需要调用load()

public Die(int i) throws IOException{
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("DieGUI.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    fxmlLoader.load();  

    // these don't seem to do anything useful?
    // this.setNumber(String.valueOf(1));
    // this.setValue(String.valueOf(1));

    this.number.setText(String.valueOf(i));
    this.value.setText(String.valueOf(i));
}
 类似资料:
  • 问题内容: 我有一个ng- view以外的NavbarCtrl。我有一个与服务对话以使用户登录的登录控制器。用户登录后,我希望导航栏更新为用户的电子邮件地址。但是,对我而言,一旦用户登录,我似乎无法让Navbar范围更新加载到我的“ Auth”服务中的数据。 这是我的主要index.html: 而我的服务: 还有我的Login和Navbar控制器: 从我的研究中,我会认为$ scope.user

  • Easyswoole 提供了高自由度的版本控制插件,版本控制的代码实现主要文件均在CoreComponentVersion目录中; 而版本控制的核心关键点在于对onRequest事件进行全局拦截,再做版本鉴定和请求重新分发。 使用 首先,在App目录下建立Version目录,并在目录内建立如下示例Version类文件,该类主要进行版本设置等。 <?php namespace AppVersion;

  • 问题内容: 我从互联网上找到了下面的代码,可以正常工作,但是它没有将打印控制台写入omt.txt,它仅在第二个catch块之后写入语句。如果您运行该代码,则将理解我的意思。要做的就是将控制台上的所有内容写入“ omt.txt”文件中。 经过一番回答后,我发现我的问题不清楚,对此感到抱歉。我想将控制台输出保存到omt.txt文本文件。如果在控制台上打印了“ Hello 123”,它也应该在omt.t

  • 我试图摆脱这个错误信息,但仍然没有成功。 还有Facebook页面的链接,但我仍然不知道该如何找到它。 在输入似乎总是一个非空值,我如何解决这个问题?

  • 问题内容: 我有这段代码,我看不出问题的根源在哪里,我在控制器的chrome控制台中没有收到任何错误: 并正确加载到div加载的东西中; 控制台中的newMsgs会按照应有的方式进行解析; 我有它适用于其他页面,但似乎我错过了这个页面上的东西。我有标签: 执行此命令时,我在控制台中得到“未定义”: 问题答案: 忽略我在评论中指出的其他体系结构问题,真正的问题是您使用的是而不是Angular的。当您

  • 插件控制器写法: 在插件controller目录下创建IndexController.php文件,内容如下: <?php // +---------------------------------------------------------------------- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ] // +-----------------