Absolute button layout

I am working on a project and I need to make two buttons that are absolutely centered on the stage. I did not find any embedded panel to do this work, so I had to use a lot of bindings. The code for this is really long, and I'm sure there is an easier way to do this work. Here is a demo: http://screencast.com/t/pvi5WLko

So I want to know if there is an easy way to do the same, and preferably with integrated panels or something like that.

I want the buttons to be centered, regardless of how the window is resized. Example:

enter image description here

+4
source share
1 answer

Probably the easiest way would be to use a VBox:

public void start(final Stage stage) throws Exception { final Button button0 = new Button("Start learning"); final Button button1 = new Button("Customize"); final VBox box = new VBox(); box.setFillWidth(true); box.getChildren().setAll(button0, button1); box.setAlignment(Pos.CENTER); stage.setScene(new Scene(box)); stage.setWidth(200); stage.setHeight(100); stage.show(); } 

Another possible way to do this is with GridPane:

 public void start(final Stage stage) throws Exception { final Button button0 = new Button("Start learning"); final Button button1 = new Button("Customize"); final GridPane cPane = new GridPane(); cPane.getChildren().addAll(button0, button1); GridPane.setConstraints(button0, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER); GridPane.setConstraints(button1, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER); final ColumnConstraints columnn0 = new ColumnConstraints(); columnn0.setPercentWidth(100); cPane.getColumnConstraints().addAll(columnn0); final RowConstraints row0 = new RowConstraints(1); row0.setPercentHeight(50); final RowConstraints row1 = new RowConstraints(1); row1.setPercentHeight(50); cPane.getRowConstraints().addAll(row0, row1); stage.setScene(new Scene(cPane)); stage.setWidth(200); stage.setHeight(100); stage.show(); } 

The idea here was to customize the rows and columns in the grid to fill your scene using objects of appropriate constraints. The above defines one column and two rows. Then you can align your components in the grid cells using GridPane.setConstraints (...).

You might want to change the code a bit so that the top button matches VPos.BOTTOM and the bottom one matches VPos.TOP, depending on whether you want the buttons to stick together (then you will need to define a margin for both, of course).

+1
source

Source: https://habr.com/ru/post/1443801/


All Articles