在java FX中打开一个新场景

时间:2014-01-18 09:45:26

标签: javafx scenebuilder

我有两个FXML文件和一个Controller。

我已经发布了我试图创建第二阶段的代码(并且失败了)。

错误信息是:

javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader 

我该如何解决?

@FXML private Scene trial_scene;
@FXML public  Stage m;     

@FXML public void click(Stage stage) throws IOException {
    m = new Stage();
    openWindow(); 
}

@FXML private void openWindow() throws IOException {
    FXMLLoader root =FXMLLoader.load(
        SampleController.class.getResource(
            "settings.fxml"
        )
    );
    m.initModality(Modality.WINDOW_MODAL);
    m.setTitle("My modal window");
    m.setScene(trial_scene);
    m.show();
}

1 个答案:

答案 0 :(得分:1)

静态方法FXMLLoader.load返回.fxml文件中定义的根节点(以及objet层次结构)。在你的情况下,它似乎是一个AnchorPane。

你的第一行应该是

AnchorPane root = FXMLLoader.load(SampleController.class.getResource("settings.fxml"));

使用此对象构建新场景,例如

Scene trial_scene = new Scene(root, 300, 300, Color.BLACK);

然后将此场景传递到新阶段

m.setScene(trial_scene); 
相关问题