[英]JavaFX - Making GridPane take up all available space in BorderPane
[英]GridPane not binding to BorderPane
我是JavaFX和FXML的新手。 我編寫了一些java swing,但這是很久以前的事了。 為了學習新東西,我正在創建一個小小的棋盤游戲,但我似乎無法將GridPane與Rectangle正確排列在一起(GridPane位於BorderPane內)。 我的意思是(我相信)我的GridPane沒有正確綁定。 正如您在屏幕截圖中看到的那樣,當我調整窗口大小時,GridPane不會隨BorderPane移動。
這些是我的課程:
主要
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
BorderPane root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 800, 900);
primaryStage.setTitle("Testing");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.toFront();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
public class Controller implements Initializable {
@FXML private GridPane gridBoard;
@FXML private BorderPane borderPane;
public void initialize(java.net.URL location,
java.util.ResourceBundle resources) {
gridBoard.prefHeightProperty().bind(borderPane.heightProperty());
gridBoard.prefWidthProperty().bind(borderPane.widthProperty());
}
public void fillBoardEvent(ActionEvent event) {
for(int i = 0; i < 50; i++) {
for(int j = 0; j < 50; j++) {
InitiateCells n;
// This is for creating the rectangles
// and to give some specific cells
// different color
if(i == 24) {
if(j == 19 || j == 20 || j == 21 || j == 22 || j == 23 || j == 24 || j == 25 || j == 26 || j == 27 ||
j == 28 || j == 29) {
n = new InitiateCells("" + i + "/" + j, 12, 12, i, j, 0);
} else {
n = new InitiateCells("" + i + "/" + j, 12, 12, i, j);
}
} else {
n = new InitiateCells("" + i + "/" + j, 12, 12, i, j);
}
}
}
}
public void exitEvent(ActionEvent event) {
System.exit(0);
}
public class InitiateCells extends Rectangle {
private String name;
private double width;
private double height;
public InitiateCells(String name, double width, double height, int rowIndex, int columnIndex) {
super(width, height);
this.name = name;
this.width = width;
this.height = height;
setFill(Color.web("#282828"));
setStroke(Color.web("#3b3b3b"));
setStrokeType(StrokeType.OUTSIDE);
setStrokeWidth(2.0);
gridBoard.add(this, columnIndex, rowIndex);
}
public InitiateCells(String name, double width, double height, int rowIndex, int columnIndex, int value) {
super(width, height);
this.name = name;
this.width = width;
this.height = height;
setFill(Color.web("#282828"));
setStroke(Color.web("#3b3b3b"));
setStrokeType(StrokeType.OUTSIDE);
setStrokeWidth(2.0);
gridBoard.add(this, columnIndex, rowIndex);
}
Sample.fxml
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0"
prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller" fx:id="borderPane">
<center>
<GridPane prefHeight="770.0" prefWidth="800.0" BorderPane.alignment="CENTER" fx:id="gridBoard" >
<children>
</children>
</GridPane>
</center>
<top>
<VBox prefHeight="30.0" prefWidth="800.0" BorderPane.alignment="TOP_CENTER">
<children>
<MenuBar>
<menus>
<Menu text="File">
<items>
<MenuItem text="New Game" onAction="#fillBoardEvent"/>
<MenuItem text="Save as GIF"/>
<MenuItem text="Export statistics"/>
<MenuItem text="Exit" onAction="#exitEvent"/>
</items>
</Menu>
<Menu text="Help">
<items>
<MenuItem text="Game of Life"/>
<MenuItem text="How to play"/>
<MenuItem text="Open Javadoc"/>
</items>
</Menu>
<Menu text="About">
<MenuItem text="Game of Life"/>
<MenuItem text="How to play"/>
<MenuItem text="Open Javadoc"/>
</Menu>
</menus>
</MenuBar>
</children>
</VBox>
</top>
<bottom>
<VBox prefHeight="70.0" prefWidth="800.0" BorderPane.alignment="BOTTOM_CENTER">
<children>
<ProgressBar prefHeight="70.0" prefWidth="800.0" progress="50.0" />
</children>
</VBox>
</bottom>
</BorderPane>
這就是問題; 當我嘗試調整窗口大小時,我的GridPane和VBox保持在同一個位置。 當我調整窗口大小時,它應該從邊緣到邊緣。 這是它的樣子:
問題不在於網格窗格沒有調整大小:問題是當網格窗格調整大小時,列和行不會調整大小。 因此,在由列占據的水平空間之后以及在由行占據的垂直空間之后,網格窗格具有大量空白空間。 (這也解釋了為什么它沒有出現居中:網格窗格本身在邊框窗格中居中,但行和列顯示在網格窗格本身的左上角。)
您可以通過在網格窗格上設置列約束和行約束來輕松地解決此問題:
public void fillBoardEvent(ActionEvent event) {
int numRows = 50 ;
int numColumns = 50 ;
for (int i = 0 ; i < numRows; i++) {
RowConstraints row = new RowConstraints();
row.setVgrow(Priority.ALWAYS);
gridBoard.getRowConstraints().add(row);
}
for (int j = 0 ; j < numColumns; j++) {
ColumnConstraints col = new ColumnConstraints();
col.setHgrow(Priority.ALWAYS);
gridBoard.getColumnConstraints().add(col);
}
for(int i = 0; i < numRows; i++) {
for(int j = 0; j < numColumns; j++) {
InitiateCells n;
// code as before...
}
}
}
現在列和行將增長,但其內容不會增長。 所以你最終得到了單元格之間的空間,如下所示:
你無法使用Rectangle
輕松解決這個問題,因為Rectangle
不可調整大小,因此無論你在網格窗格上放置什么設置,它都無法為你調整矩形的大小。 (您可以在網格窗格的大小和矩形的寬度和高度之間引入綁定,但這會非常快速地變得非常丑陋。)我將使用Region
s而不是Rectangle
s,因為Region
s可以調整大小。 請注意,您指定為單元格寬度和高度的值實際上是首選寬度和高度,因為您希望它們隨窗口大小調整。
一個Region
沒有fill
, stroke
等,但它可以通過CSS設置樣式,所以我會這樣做
public class InitiateCells extends Region {
private String name;
private double width;
private double height;
public InitiateCells(String name, double width, double height, int rowIndex, int columnIndex) {
this.name = name;
this.width = width;
this.height = height;
setPrefSize(width, height);
getStyleClass().add("board-cell");
gridBoard.add(this, columnIndex, rowIndex);
}
public InitiateCells(String name, double width, double height, int rowIndex, int columnIndex, int value) {
this(name, width, height, rowIndex, columnIndex);
pseudoClassStateUpdated(PseudoClass.getPseudoClass("on"), true);
}
}
現在在您的主類中,添加一個外部樣式表:
scene.getStylesheets().add(getClass().getResource("board-style.css").toExternalForm());
並添加一個board-style.css文件
.board-cell {
-fx-background-color: #3b3b3b, #282828 ;
-fx-background-insets: 0, 2 ;
}
.board-cell:on {
-fx-background-color: #3b3b3b, white ;
-fx-background-insets: 0, 2 ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.