JavaFX - 在修改子节点的大小后更新 BorderPane 的大小
JavaFX - Updating size of BorderPane after modifying size of child node
我有一个中间有 Canvas 的 BorderPane,当我更改 canvas 的大小时,我希望 BorderPane 始终包围 canvas。以这段代码为例:
public class Test extends Application {
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Canvas canvas = new Canvas(10, 10);
root.setCenter(canvas);
Scene s = new Scene(root);
primaryStage.setScene(s);
primaryStage.show();
canvas.setWidth(100);
canvas.setHeight(100);
}
public static void main(String[] args) {launch(args);}
我希望在 canvas 上调用 setWidth 和 setHeight 方法后 BorderPane 改变它的大小,但它只是保持相同的大小,就好像 canvas 仍然是 (10,10)大的。我该怎么做?
问题是您的 BorderPane
是应用程序 Scene
的根目录。 Scene
s 根容器不会(至少不会自动)变得比它包含的 Scene
.
更大
看看这个示例应用程序:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(0, 0);
Button button = new Button("test");
button.setOnAction(ev -> {
canvas.setWidth(canvas.getWidth() + 10);
canvas.setHeight(canvas.getHeight() + 10);
canvas.getGraphicsContext2D().clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.getGraphicsContext2D().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
});
BorderPane canvasBorderPane = new BorderPane(canvas);
canvasBorderPane.setPadding(new Insets(10));
canvasBorderPane.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(0), Insets.EMPTY)));
BorderPane root = new BorderPane(canvasBorderPane);
root.setPadding(new Insets(10));
root.setBackground(new Background(new BackgroundFill(Color.BLUE, new CornerRadii(0), Insets.EMPTY)));
root.setBottom(button);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我把两个 BorderPane
叠在一起,把 Canvas
放在最里面,应用了一些背景颜色,这样你就可以看到发生了什么。
我有一个中间有 Canvas 的 BorderPane,当我更改 canvas 的大小时,我希望 BorderPane 始终包围 canvas。以这段代码为例:
public class Test extends Application {
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Canvas canvas = new Canvas(10, 10);
root.setCenter(canvas);
Scene s = new Scene(root);
primaryStage.setScene(s);
primaryStage.show();
canvas.setWidth(100);
canvas.setHeight(100);
}
public static void main(String[] args) {launch(args);}
我希望在 canvas 上调用 setWidth 和 setHeight 方法后 BorderPane 改变它的大小,但它只是保持相同的大小,就好像 canvas 仍然是 (10,10)大的。我该怎么做?
问题是您的 BorderPane
是应用程序 Scene
的根目录。 Scene
s 根容器不会(至少不会自动)变得比它包含的 Scene
.
看看这个示例应用程序:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(0, 0);
Button button = new Button("test");
button.setOnAction(ev -> {
canvas.setWidth(canvas.getWidth() + 10);
canvas.setHeight(canvas.getHeight() + 10);
canvas.getGraphicsContext2D().clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.getGraphicsContext2D().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
});
BorderPane canvasBorderPane = new BorderPane(canvas);
canvasBorderPane.setPadding(new Insets(10));
canvasBorderPane.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(0), Insets.EMPTY)));
BorderPane root = new BorderPane(canvasBorderPane);
root.setPadding(new Insets(10));
root.setBackground(new Background(new BackgroundFill(Color.BLUE, new CornerRadii(0), Insets.EMPTY)));
root.setBottom(button);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我把两个 BorderPane
叠在一起,把 Canvas
放在最里面,应用了一些背景颜色,这样你就可以看到发生了什么。