Original address http://download.oracle.com/javafx/2.0/layout/size_align.htm
The biggest benefit of using the javafx built-in layout pane is that it helps you manage the node size and its nature. When the size of the pane changes, the size of the node changes greatly depending on the nature of the pane. Note that not all node classes can change the size. The UI control and layout pane can be, but the shape,Text object,
Group objects are not allowed. They are rigid objects in the layout. If you want more controls on the widget size, specify the size directly. The layout pane determines the widget Size Based on your settings.
Change node size
Layout pane by callingPrefwidth (height) and
prefHeight(width)
Method to query the preferred size of a node. By default, the UI controls calculate their preferred sizes based on their content. For example,Button
The object size is calculated based on the text length and the font size (and images may exist) in the label. Generally, the calculated size is just large enough to make the labels fully visible.
The UI control also provides the default minimum and maximum sizes Based on typical usage. For example,Button
The maximum size of an object is the preferred size, because it is unlikely to make it any larger. However,ScrollPane
The maximum size of objects is uncertain, because they always want to grow with the content.
You can use the default size of the node or set it as needed. For example, Figure 2-1 is the default size of some buttons in the border pane and the list view.
Figure 2-1 computed sizes
Description of "Figure 2-1 computed sizes" What if you want the effect in Figure 2-2? The size of the UI control is preset.
Figure 2-2 desired sizes
Description of "Figure 2-2 desired sizes"
Applications usually need to directly set the maximum, minimum, and preferred size of controls. The following describes how to modify the appearance of a widget to make it satisfactory.
Make the buttons as big as possible
To achieve this goal, you can specify the height and width for each button, and then change their preferred size to the maximum width and height. A simpler way is to make the layout pane complete the work. The layout pane is determined by the desired effect.
Use vertical box
The scenario in Figure 2-1 is that the button is used.A vbox layout pane with the default button size. The button has the same height, so you only need to change its width.
Scenario 2-2 usesThe vbox pane uses the default behavior:
The width of the vbox pane is the same as that of the widest element. Both the buttons and
If the vbox pane has the same width
Set the maximum button widthDouble. max_value constant, so that the control changes are not limited. Of course, you can specify the maximum value as a specific parameter, for example
80.0.
Example 2-1 implements anyThe buttons in the vbox pane are all the same width.
Example 2-1 set a column of buttons to the same width
BorderPane root = new BorderPane();root.setPadding(new Insets(20, 0, 20, 20));Button btnAdd = new Button("Add");Button btnDelete = new Button("Delete");Button btnMoveUp = new Button("Move Up");Button btnMoveDown = new Button("Move Down");btnAdd.setMaxWidth(Double.MAX_VALUE);btnDelete.setMaxWidth(Double.MAX_VALUE);btnMoveUp.setMaxWidth(Double.MAX_VALUE);btnMoveDown.setMaxWidth(Double.MAX_VALUE);VBox vbButtons = new VBox();vbButtons.setSpacing(10);vbButtons.setPadding(new Insets(0, 30, 10, 25)); vbButtons.getChildren().addAll(btnAdd, btnDelete, btnMoveUp, btnMoveDown);root.setright(vbButtons);
TIPS: Placing the button column in the right of the border pane limits the preferred width of the widest button. The middle part of the border pane will try to fill up the control, so if you place the vbox in the middle, it and the button will expand. |
Use tile pane
The scenario in Figure 2-1 uses the button at the bottom.An hbox layout pane with the default button size.
The payment and payment for the button are different.
The scenario in Figure 2-2 uses a levelThe tistmane layout pane uses its default behavior: Each cell or tile is of the same size.
The size of each tile must be the size of the largest node in the tile pane.
To make the buttons and tiles of the same size, set their maximum width and heightDouble. max_value constant.
Example
2-2.
Example 2-2 set a row of buttons to the same size
Button btnApply = new Button("Apply");Button btnContinue = new Button("Continue");Button btnExit = new Button("Exit");btnApply.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);btnContinue.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);btnExit.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);btnExit.setStyle("-fx-font-size: 14pt;");TilePane tileButtons = new TilePane(Orientation.HORIZONTAL);tileButtons.setPadding(new Insets(20, 10, 20, 0));tileButtons.setHgap(10.0);tileButtons.getChildren().addAll(btnApply, btnContinue, btnExit);
Even if the window size is changed, the tiles will not change, so the buttons in the tile pane will not change.
Keep the preferred node size
When the stage changes, the layout pane may generate too much or insufficient space for the contained controls. Each layout pane has its own space allocation principle, which is based on the minimum, maximum, and preferred size of the control. Generally, the maximum size isIf it is double. max_value, the control will expand to fill the space, but the space with a limited size will not. For example,
The maximum value of the listview object cannot be determined. If you want to limit the preferred height, you can set it
Control.USE_PREF_SIZE
Constant, see Example
2-3.
Example 2-3 set maximum height to preferred height
ListView<String> lvList = new ListView<String>();ObservableList<String> items = FXCollections.observableArrayList ( "Hot dog", "Hamburger", "French fries", "Carrot sticks", "Chicken salad");lvList.setItems(items);lvList.setMaxHeight(Control.USE_PREF_SIZE);
The button is expanded to the preferred size by default. However, if the minimum width is not specified, three points (...) may appear (...). To prevent the button from being smaller than the preferred width, set the minimum width to the preferred width. See example 2-4.
Example 2-4 set minimum width to preferred width
Button btnMoveDown = new Button("Move Down");btnMoveDown.setMinWidth(Control.USE_PREF_SIZE);
The preferred size of the control is based on the calculated value (content. You can overwrite the preferred value for your choice. The following statement overwrites the list width.
Lvlist. setprefwidth (150.0 );
Block changing the size
If you do not want to change the node size, set the maximum, minimum, and preferred values to the same. To only prevent the width or height from changing, set the width or height to the same. In Example 2-5, the limit value is the same when the list is created, so the size of the window remains unchanged even if it is changed. One button also limits the same width of three values.
Example 2-5 set size constraints to prevent resizing
ListView<String> lvList = new ListView<String>();lvList.setMinSize(150.0, Control.USE_PREF_SIZE);lvList.setMaxSize(150.0, Control.USE_PREF_SIZE;Button btnDelete = new Button("Delete");btnDelete.setMinWidth(80.0);btnDelete.setPrefWidth(80.0);btnDelete.setMaxWidth(80.0);
Text
Each layout pane has its own method for its node. For example,Hbox and
VBox
In the layout pane, nodes are aligned at the top and left.Tiancane and
In the flowpane pane, nodes are centered. The top and left alignment of the pane by default.
AvailableThe setalignment () method sets the method of the node and pane. The constant is located in
In the javafx. geometry package
In the enum type.
HPos
-Horizontal alignment Value
Pos
-Specifies the vertical and horizontal alignment values. Vertical Alignment is specified on the left side of the underline, and horizontal alignment is specified on the right side of the underline. For examplePOs. bottom_left: the vertical direction of the node is at the bottom, and the horizontal direction is at the left.
VPos
-Specifies the vertical alignment value.
Figure 2-3 is the code example
Because there is no alignment restriction, the layout pane is in the upper left corner.
Figure 2-3 default positions
Description of "Figure 2-3 default positions"
Example 2-6 create a UI with default alignment
public void start(Stage primaryStage) { GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(12); HBox hbButtons = new HBox(); hbButtons.setSpacing(10.0); Button btnSubmit = new Button("Submit"); Button btnClear = new Button("Clear"); Button btnExit = new Button("Exit"); btnSubmit.setStyle("-fx-font-size: 11pt;"); Label lblName = new Label("User name:"); TextField tfName = new TextField(); Label lblPwd = new Label("Password:"); PasswordField pfPwd = new PasswordField(); hbButtons.getChildren().addAll(btnSubmit, btnClear, btnExit); grid.add(lblName, 0, 0); grid.add(tfName, 1, 0); grid.add(lblPwd, 0, 1); grid.add(pfPwd, 1, 1); grid.add(hbButtons, 0, 2, 2, 1); Scene scene = new Scene(grid, 250, 250); primaryStage.setTitle("Layout Positioning"); primaryStage.setScene(scene); primaryStage.show();}
What if I want to see the results in Figure 2-4? The pane is in the center of the screen.
Figure 2-4 desired positions
Description of "Figure 2-4 desired positions"
The following describes any settings that overwrite the default position.
Center the grid
To center the grid in example 2-6, do the following:
Grid. setalignment (Pos. center );
Align controls in the column
In our ideal layout, labels should be right aligned while text boxes should be left aligned. To achieve this goal in the grid, useColumnconstraints class and set the horizontal method for it.
Example
2-7 defines column attributes for the grid in example 2-6.
Example 2-7 define the columns in the grid
GridPane grid = new GridPane();grid.setAlignment(Pos.CENTER);grid.setHgap(10);grid.setVgap(12);ColumnConstraints column1 = new ColumnConstraints();column1.setHalignment(HPos.RIGHT);grid.getColumnConstraints().add(column1); ColumnConstraints column2 = new ColumnConstraints();column2.setHalignment(HPos.LEFT);grid.getColumnConstraints().add(column2);
UseThe vbox pane can also align the right of the control. Use as follows
setAlignment()
Method:
VBox vbox = new VBox;vbox.setAlignment(Pos.CENTER_RIGHT);
Center button
The buttons in the hbox span the columns in the grid. The following code centers the buttons:
HBox hbButtons = new HBox()hbButtons.setAlignment(Pos.CENTER);
HBox
OfsetAlignment()
The method is centered.The hbox pane and its nodes. If you prefer
The hbox pane is centered and the bottom of the button is aligned, just like
Figure
2-5.
Figure 2-5 override positions and bottom-justify the buttons
Description of "Figure 2-5 override positions and bottom-justify the buttons"
To make this arrangementHBox
The pane is placed in an inner grid with only one cell and placed in the third row of the outer grid. Set the inner mesh to center, Set the hbox pane to bottom alignment, see
Example
2-8.
Example 2-8 center and bottom-justify the buttons
HBox hbButtons = new HBox();hbButtons.setSpacing(10.0);hbButtons.setAlignment(Pos.BOTTOM_CENTER);hbButtons.getChildren().addAll(btnSubmit, btnClear, btnExit);GridPane innergrid = new GridPane();innergrid.setAlignment(Pos.CENTER);innergrid.add(hbButtons, 0, 0);grid.add(innergrid, 0, 2, 2, 1);