简体   繁体   中英

Positon of element - GridPane in JavaFX

I'am beginner student java developer and I have to creating simple game in java fx. I want learn this, not using template or game another autors. I have a problem with GridPane. so I have 8 elements in my application ex. text, label and image, but my problem is arranging elements on the stage. For example if I try position one element other elements lost position. What I doing wrong?

Code:

    grid.setHgap(20);
    grid.setVgap(20);
    grid.setPadding(new Insets(10, 10, 10 ,10));
    grid.add(welcome, 10, 0);

btw. gridPane is good for simple checkers game? Thanks

So, you've set padding on each of your columns and rows, which means there will be 10px of space on all sides of each element in the grid. Then you've added something into the 10th column. This creates all the columns 0-9, with width and height 0, because they have no content. However, they each take up about a 20x20 pixel square, because of their padding. When you add things into these spaces, of course they will move your welcome object over, because they are now taking up space.

Instead of using padding to create your column and row widths and heights, consider ColumnConstraints and RowConstraints . I've added the documentation for their use below.

From the GridPane documentation :

Row/Column Sizing

By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.However, if an application needs to explicitly control the size of rows or columns, it may do so by adding RowConstraints and ColumnConstraints objects to specify those metrics. For example, to create a grid with two fixed-width columns:

GridPane gridpane = new GridPane();
gridpane.getColumnConstraints().add(new ColumnConstraints(100)); // column 0 is 100 wide
gridpane.getColumnConstraints().add(new ColumnConstraints(200)); // column 1 is 200 wide

By default the gridpane will resize rows/columns to their preferred sizes (either computed from content or fixed), even if the gridpane is resized larger than its preferred size. If an application needs a particular row or column to grow if there is extra space, it may set its grow priority on the RowConstraints or ColumnConstraints object. For example:

GridPane gridpane = new GridPane();
ColumnConstraints column1 = new ColumnConstraints(100,100,Double.MAX_VALUE);
column1.setHgrow(Priority.ALWAYS);
ColumnConstraints column2 = new ColumnConstraints(100);
gridpane.getColumnConstraints().addAll(column1, column2); // first column gets any extra width

Note: Nodes spanning multiple rows/columns will be also size to the preferred sizes. The affected rows/columns are resized by the following priority: grow priorities, last row. This is with respect to row/column constraints.

Percentage Sizing

Alternatively, RowConstraints and ColumnConstraints allow the size to be specified as a percentage of gridpane's available space:

 GridPane gridpane = new GridPane();
 ColumnConstraints column1 = new ColumnConstraints();
 column1.setPercentWidth(50);
 ColumnConstraints column2 = new ColumnConstraints();
 column2.setPercentWidth(50);
 gridpane.getColumnConstraints().addAll(column1, column2); // each get 50% of width

If a percentage value is set on a row/column, then that value takes precedent and the row/column's min, pref, max, and grow constraints will be ignored. Note that if the sum of the widthPercent (or heightPercent) values total greater than 100, the values will be treated as weights. eg if 3 columns are each given a widthPercent of 50, then each will be allocated 1/3 of the gridpane's available width (50/(50+50+50)).

Mixing Size Types

An application may freely mix the size-types of rows/columns (computed from content, fixed, or percentage). The percentage rows/columns will always be allocated space first based on their percentage of the gridpane's available space (size minus insets and gaps). The remaining space will be allocated to rows/columns given their minimum, preferred, and maximum sizes and grow priorities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM