java FX VBox布局

时间:2016-03-08 20:25:25

标签: java javafx

我试图了解javafx中的布局,我不明白,为什么这段代码不起作用:

public class AppWindow extends Application {

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Car Race");
    VBox root = new VBox();
    Scene scene = new Scene(root, 800, 600);

    // menu construction not required for minimal code example

    SubScene layer0 = new SubScene(root, 0, 0);

    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, layer0);

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

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

我已经找到了使用subcene的问题(没有它运行良好)。

对于要完成的问题,我包括所有运行时错误的列表:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: VBox@1537c6b[styleClass=root]is already set as root of another scene or subScene
    at javafx.scene.SubScene$1.invalidated(SubScene.java:255)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.SubScene.setRoot(SubScene.java:213)
    at javafx.scene.SubScene.<init>(SubScene.java:154)
    at javafx.scene.SubScene.<init>(SubScene.java:115)
    at carRace.AppWindow.start(AppWindow.java:45)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more

我无法从我找到的教程中找到答案。

1 个答案:

答案 0 :(得分:0)

基本上你希望subcene占用场景的一部分,而其余部分是菜单栏,所以将它们放在VBox中是有意义的,只要subcene被接受为VBox的节点就没有问题(我之前没有使用过subcenes,所以我不知道。)

然而,正如其中一位评论员提到的那样,你将VBox放在子场景中,你将整个布局放在布局的一部分内。你希望将subcene基于一些将在其中的元素,而不是它周围。

显而易见的选择是将子场景基于新窗格,您可以从那里将所有元素添加到该窗格。