如何将内部类控制器绑定到FXML?

我想将内部类“ FinishDialogController”控制器绑定到FXML。 这是一个真正的难题。

但是fx:controller="app.view.MainlayoutController.FinishDialogController"是错误的。

谁知道正确的方法?

我在Introduction to FXML中搜索“内部”,但未找到。

这是完整的FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="108.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.view.MainlayoutController.FinishDialogController">
   <children>
      <TextField fx:id="textField" layoutX="25.0" layoutY="50.0" onaction="#handleTextField" prefHeight="23.0" prefWidth="314.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="25.0" />
      <Button fx:id="okButton" layoutX="219.0" layoutY="78.0" mnemonicParsing="false" onaction="#handleOkButton" text="OK" />
      <Button fx:id="deleteButton" layoutX="271.0" layoutY="78.0" mnemonicParsing="false" onaction="#handleDeleteButton" text="Delete" />
   </children>
</AnchorPane>

kingdee624 回答:如何将内部类控制器绑定到FXML?

  

听起来不错,但这并不能解决我的问题。

这是什么意思?到底是什么问题?

public class Parent {
    public static class Child {

        @FXML
        private Label label;

        @FXML
        private void initialize() {
            System.out.println("initialize " + getClass().getName());
            System.out.println("label =  " + label);
        }
    }
}

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="sample.Parent$Child">

    <Label fx:id="label" text="Test"/>

</AnchorPane>

public class Sample extends Application {

    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/sample/test.fxml"));

        Parent root = loader.load();
        Scene scene = new Scene(root,400,200);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

运行上面的示例时,控制器成功初始化:

initialize sample.Parent$Child
label =  Label[id=label,styleClass=label]'Test'
本文链接:https://www.f2er.com/3145388.html

大家都在问