AnchorPane homePage =(AnchorPane)loader.load();不工作

时间:2016-07-18 12:28:12

标签: javafx fxmlloader

以下是代码:

final BeaconState.TypeFilter typeFilter = BeaconState.TypeFilter.with("abc", "def");
    final AwarenessFence beaconFence = BeaconFence.found(typeFilter);
    Awareness.FenceApi.updateFences(
            client,
            new FenceUpdateRequest.Builder()
                    .addFence(BEACON_FENCE, beaconFence, pi)
                    .build())
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Fence was successfully registered.");
                    } else {
                        Log.e(TAG, "Fence could not be registered: " + status);
                    }
                }
            });

以下是错误消息:

 @Override
 public void start(Stage primaryStage) {
     System.out.println("Enter start method");
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Bank Account App");
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/Overview.fxml"));
        System.out.println("Location is set");
        AnchorPane homePage = (AnchorPane) loader.load();

        OverviewController controller = loader.getController();
        controller.setMainApp(this);

        // Show the scene containing the root layout.
        Scene scene = new Scene(homePage);
        primaryStage.setScene(scene);
        primaryStage.show();
        System.out.println("Showed successfully");
        this.primaryStage.setWidth(800);
    } catch (IOException e) {
        System.out.println("Not Working");;
    }


 }

为什么第Caused by: java.lang.IllegalStateException: Location is not set. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409``) at Question2.Controller.MainApp.start(MainApp.java:40) 行不起作用? 我确信我的fxml文件有锚窗格作为其父窗口。

2 个答案:

答案 0 :(得分:0)

在屏幕的FXML文件中的最高容器中添加以下开头行

fx:controller="com.fxtest.MyController"

其中 com.fxtest.MyController 是包含类似方法的类:

void buttonOnAction(ActionEvent event) throws IOException{

负责捕捉,即鼠标点击按钮......

您还可以在以下位置添加控制器方法:

SceneBuilder - &gt;属性(请求对象的) - &gt;的OnAction

答案 1 :(得分:-1)

检查MainApp.class.getResource("view/Overview.fxml")实际返回的内容。