我有这个代码(简化/缩短/伪):
StackPane background = new StackPane();
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane(background);
pane.setLeft(overlay);
我希望背景/堆栈 Pane 覆盖整个窗口(在背景中,令人惊讶),并且覆盖/网格覆盖背景的一部分。问题是,当我将 ImageView 添加到 StackPane 时,只显示图像而没有其他内容。覆盖层不可见。我尝试了overlay.toFront、overlay.preferredHeight和background.toBack,但没有成功。
最佳答案
尝试将边框 Pane 放入堆栈 Pane 中,而不是相反,然后确保将图像添加为堆栈 Pane 子项的第一个元素。
GridPane overlay = new GridPane();
BorderPane pane = new BorderPane();
pane.setLeft(overlay);
StackPane background = new StackPane(pane);
// ...
ImageView imageView = ... ;
background.getChildren().add(0, imageView);
// ...
scene.setRoot(background);
或者,只需使用 BorderPane
作为根并以通常的方式添加节点。然后,在外部样式表中,执行
.root {
-fx-background-image: url(relative/path/to/image);
}
关于Javafx BorderPane 强制重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26264844/