【发布时间】:2016-02-25 16:23:44
【问题描述】:
我正在使用 javafx 8 制作一个垄断游戏,我偶然发现了一个让我忙了几个小时的问题。我使用以下类来渲染我的游戏板的视图:
public class SpelbordView extends GridPane {
public Pane[] panes = new Pane[40];
public SpelbordView() {
initNodes();
layoutNodes();
}
private void initNodes(){
}
private void layoutNodes(){
RowConstraints rowsEdge = new RowConstraints();
rowsEdge.setPercentHeight(14);
RowConstraints rowsMid = new RowConstraints();
rowsMid.setPercentHeight(8);
ColumnConstraints colEdge = new ColumnConstraints();
colEdge.setPercentWidth(14);
ColumnConstraints colMid = new ColumnConstraints();
colMid.setPercentWidth(8);
this.getColumnConstraints().addAll(colEdge, colMid,
colMid, colMid, colMid, colMid, colMid, colMid, colMid, colMid, colEdge);
this.getRowConstraints().addAll(rowsEdge, rowsMid,
rowsMid,rowsMid,rowsMid,rowsMid,rowsMid,rowsMid, rowsMid,rowsMid, rowsEdge);
BorderPane image = new BorderPane();
this.add(image,1,1,9,9);
image.maxHeightProperty().bind(this.heightProperty().multiply(0.72));
image.maxWidthProperty().bind(this.widthProperty().multiply(0.72));
ImageView imageView = new ImageView("monopoly/view/beginscherm/images/mlogo.png");
imageView.setPreserveRatio(true);
imageView.fitWidthProperty().bind(image.widthProperty().divide(2));
imageView.autosize();
image.setBackground(new Background(new BackgroundFill(MonopolyGUIItems.getMonopolyRed(),null,null)));
image.setCenter(imageView);
GridPane.setHalignment(image, HPos.CENTER);
GridPane.setValignment(image, VPos.CENTER);
BorderPane.setAlignment(image, Pos.CENTER);
this.setGridLinesVisible(true);
image.setPrefSize(getHeight()*0.72, getWidth()*0.72);
image.autosize();
}
}
它几乎完美无缺。但是当我将视图放到舞台上时,出现了问题。
如您所见,我放置图像的边框与其他网格重叠。这不应该发生。奇怪的是,当我手动调整窗口大小时,边框会立即调整其尺寸以适合网格窗格的中心,如下所示:
有人知道这是什么原因吗?或者我的代码中缺少的任何内容。正如我所说,边框的尺寸仅在第一次渲染舞台时是错误的。每当我手动调整舞台大小时,无论我选择什么尺寸,中心的边框都会完美调整大小。
【问题讨论】: