JavaFX FXML-使用FXML元素将侦听器添加到属性

时间:2019-02-19 18:19:21

标签: java javafx listener fxml


我未成功在控制器文件上添加侦听器以处理宽度属性对场景根元素的更改,我不知道该怎么做。这是我的FXML文件:

// imports omitted
<AnchorPane fx:id="Body" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.myname.myapp.Standard">
    <stylesheets>
        <URL value="@Standard.css"/>
    </stylesheets>

    <children>
        <GridPane fx:id="PannelloCentrale" prefWidth="${Body.width*0.9}" prefHeight="${Body.height*0.3}" translateX="${Body.width*0.05}" translateY="${Body.height*0.30}">
            <HBox styleClass="RigaTabella" GridPane.rowIndex="0" prefHeight="54">
                <Button styleClass="Bottone" prefWidth="100" prefHeight="50">Carica file</Button>
                <Button styleClass="Bottone" prefWidth="100" prefHeight="50">Salva output</Button>
            </HBox>
            <HBox fx:id="ContenitoreBarra" GridPane.rowIndex="1" prefWidth="${PannelloCentrale.width*0.9}" prefHeight="${PannelloCentrale.height*0.2}">
                <Pane fx:id="BarraCaricamento" prefWidth="10" prefHeight="${ContenitoreBarra.height}"></Pane>
            </HBox>
        </GridPane>
    </children>
</AnchorPane>

在这里我没有收到错误,我在控制器上收到了错误(下面的代码):

// imports omitted
public class Standard {
    @FXML
    private AnchorPane Body;

    Body.widthProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue obs, Object oldVal, Object newVal) {
            // mycode
        }
    });
}

为什么不能在Controller文件中将侦听器添加到对象的属性?有其他方法可以做到吗?我希望我没有做过拼写错误,但是我不认为这是因为我想整天解决这个问题,而且没有找到答复。
IDE向我抛出一个错误,说我错过了;在该行代码(我尝试在其中将侦听器添加到widthProperty)。它还说该标识符在预期中不存在,希望对您有所帮助,并且该错误可以使您了解更多:

C:\mypath\Standard.java:21: error: <identifier> expected
    Body.widthProperty().addListener(new ChangeListener() {
                      ^
C:\mypath\Standard.java:21: error: ';' expected
    Body.widthProperty().addListener(new ChangeListener() {
                        ^

编辑1

我不认为这是重复的,因为焦点不在预期的错误上,焦点和主要topix是添加侦听器事实。完成了更好的组织和布局。

0 个答案:

没有答案