JavaFX矩形不会接触右侧

JavaFX矩形不会接触右侧,java,animation,javafx,Java,Animation,Javafx,我用JavaFX构建了一个矩形。我的场景宽度为300,矩形宽度为80 setX设置矩形左上角的位置。我将X设置为淫秽。getWidth-carRightSide,但它不接触右侧 我做错了什么 package assign3; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Flo

我用JavaFX构建了一个矩形。我的场景宽度为300,矩形宽度为80

setX设置矩形左上角的位置。我将X设置为淫秽。getWidth-carRightSide,但它不接触右侧

我做错了什么

    package assign3;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Question4 extends Application
{

    public int carRightSide;

    @Override
    public void start( Stage obPrimeStage ) throws Exception
    {

        Pane obPane = new Pane();

        Circle obWheelOne = new Circle(20, Color.BLACK);
        obWheelOne.setRadius(20);
        Circle obWheelTwo = new Circle(20, Color.BLACK);
        obWheelTwo.setRadius(20);

        Rectangle obBody = new Rectangle(80, 40, Color.LIGHTBLUE);


        obPane.getChildren().add(obWheelOne);
        obPane.getChildren().add(obBody);

        Scene obScene = new Scene(obPane, 300, 350);

        carRightSide = 80;
        obBody.setX(obScene.getWidth() - carRightSide);
        obBody.setY(40);

        obPrimeStage.setTitle("Driving Cars");
        obPrimeStage.setScene(obScene);
        obPrimeStage.setResizable(false);
        obPrimeStage.show();


    }

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

    }

}
您必须调用obPrimeStage.show;在使用场景的.GetWidth之前

也可以在设置场景后调整舞台的大小

public class Question4 extends Application
{

    public int carRightSide = 80;

    @Override
    public void start( Stage obPrimeStage ) throws Exception
    {

        Pane obPane = new Pane();           

        Scene obScene = new Scene(obPane, 300, 350, Color.ANTIQUEWHITE);
        obPrimeStage.setScene(obScene);//Add scene here
        obPrimeStage.setTitle("Driving Cars");
        obPrimeStage.setResizable(false);
        obPrimeStage.show();//Show Stage so that the size will be calculated

        Circle obWheelOne = new Circle(20, Color.BLACK);
        obWheelOne.setRadius(20);
        Circle obWheelTwo = new Circle(20, Color.BLACK);
        obWheelTwo.setRadius(20);

        Rectangle obBody = new Rectangle(carRightSide, 40, Color.LIGHTBLUE);
        obBody.setX(obScene.getWidth() - carRightSide);
        obBody.setY(40);

        obPane.getChildren().add(obBody);
        obPane.getChildren().add(obWheelOne);
        obPane.getChildren().add(obWheelTwo);
    }

    public static void main( String[] args )
    {
        Application.launch(args);
    }
}
Scene obScene = new Scene(obPane, 300, 350, Color.ANTIQUEWHITE);
obPrimeStage.setScene(obScene);//Add scene here
obPrimeStage.setWidth(obScene.getWidth());
obPrimeStage.setHeight(obScene.getHeight());

//Circles
//Rectangle
//Adding components
obPrimeStage.setTitle("Driving Cars");
obPrimeStage.setResizable(false);
obPrimeStage.show();//Show Stage so that the size will be calculated