javafx.fxml.LoadException:StackPane 不是有效类型

VA *_*ash -1 java javafx fxml scenebuilder

我需要将 BorderPane 包含在 StackPane 内。用于使用 JFX 对话框。JFXDialog() 的第一个参数必须是 StackPane 变量。我无法传递 BorderPane 变量来代替它。所以我尝试围绕 BorderPane 创建一个新的 StackPane 标签

示例.fxml

<StackPane fx:id="rootPane" xmlns="http://javafx.com/javafx/8.0.112-ea"
    xmlns:fx="http://javafx.com/fxml/1">
    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity"
        minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
        prefWidth="600.0" style="-fx-background-color: #181818;" xmlns="http://javafx.com/javafx/16"
        xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
        <left>
          .........
          .........
    </BorderPane>
</StackPane>
Run Code Online (Sandbox Code Playgroud)

但是在添加此标签后,Sample.fxml 无法使用 SceneBuilder 打开,当我尝试运行 JavaFx Main 类时,我收到此错误。

javafx.fxml.LoadException: StackPane is not a valid type.
Run Code Online (Sandbox Code Playgroud)

示例控制器.java

....
public class SampleController implements Initializable {

...
@FXML
private StackPane rootPane; 

@FXML
    private void migrateButtonAction(ActionEvent event){
        JFXDialogLayout  dialogLayout = new JFXDialogLayout();
        JFXDialog dialog = new JFXDialog(rootPane,dialogLayout, JFXDialog.DialogTransition.TOP);

        if(inputLocationField.getText() == ""){
            JFXButton button = new JFXButton("Okay..I'll Check");
            button.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent mouseEvent) ->{
                dialog.close();
            });

            dialogLayout.setBody(new Text("Please Fill all the text boxes"));
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*esh 5

大多数 IDE 在设计时都会添加一组最少的导入。这意味着它将仅显式导入表单中正在使用的布局集。

重构布局时,最容易忘记的事情之一就是向.fxml文件添加导入以支持新的布局类型。

在这种情况下,支持 StackPane 所需的最小更改是添加一条 import 语句:

<?import javafx.scene.layout.StackPane?>
Run Code Online (Sandbox Code Playgroud)

到文件的导入部分.fxml