Pane中的JavaFX画布大小

时间:2016-06-26 13:59:38

标签: java canvas javafx pane

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

我的代码:

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

屏幕类:

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

1 个答案:

答案 0 :(得分:1)

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

primaryStage.sizeToScene();之后放置setResizable(false);似乎解决了这个问题。