JavaFX canvas 窗格大小

JavaFX canvas size in Pane

我刚开始使用 JavaFX,我似乎无法解决这个问题。 我的画布似乎没有填满他们可用的完整 space。 我为我的画布设置了一个大小,它们确实有这个大小,但它周围的窗格变大了。这会导致右侧和窗格底部出现白色边框。

我的代码:

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("ShellShock");
    primaryStage.setResizable(false);

    final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();

    Screen screen = new Screen(SCREEN_SIZE.getWidth() * 0.9, SCREEN_SIZE.getHeight() * 0.9);

    Scene scene = new Scene(screen);

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

屏幕class:

public class Screen extends Pane {

private Canvas[] canvases;

public Screen(double width, double height) {
    this.setPrefSize(width, height);

    this.canvases = new Canvas[2];

    Canvas background = new Canvas(width, height);
    GraphicsContext gcb = background.getGraphicsContext2D();
    gcb.setFill(new LinearGradient(0, 0, 0, 1, true, CycleMethod.REFLECT, new Stop(0.4, Color.BLUE.darker()),
            new Stop(1.0, Color.BLUE.brighter().brighter())));
    gcb.fillRect(0, 0, width, height);
    Canvas foreground = new Canvas(width, height);

    this.canvases[0] = background;
    this.canvases[1] = foreground;

    this.getChildren().add(background);
    this.getChildren().add(foreground);
}
}

删除 setResizable(false); 行似乎可以解决问题。

同时将 primaryStage.sizeToScene(); 放在 setResizable(false) 之后;似乎解决了问题。