简体   繁体   中英

java FX VBox layout

I'm trying to understand layouts in javafx and I don't understand, why this code doesn't work:

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);
    }
}

I've located the problem in the usage of subscene (without it it works well).

For the question to be complete I include list of all the runtime errors:

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

I couldn't figure the answer out from the tutorials I had found.

Basically you want the subscene to take up part of the scene, and menubar for the rest, so it makes sense to put them both in a VBox, no problem there as long as subscene is accepted as a node to the VBox (I haven't used subscenes before so I wouldn't know).

However, as one of the commentators mentioned, you're putting the VBox, in the subscene, you're putting the whole layout inside a part of the layout. You want to base the subscene on some element that is going to be inside it, not around it.

The obvious choice is to base the subscene on a new Pane, and you could go from there adding all your elements to that Pane.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM