Java Reference
In-Depth Information
Table 5-4. ( continued )
Name
Description
CENTER
Aligns to the center of the container
CENTER_RIGHT
Aligns to the middle of the right side of the container
BOTTOM_LEFT
Aligns to the bottom-left of the container
BOTTOM_CENTER
Aligns to the bottom-center of the container
BOTTOM_RIGHT
Aligns to the bottom-right of the container
BASELINE_LEFT
Aligns to the left side along the baseline (bottom of text and components)
BASELINE_CENTER
Aligns to the center along the baseline (bottom of text and components)
BASELINE_RIGHT
Aligns to the right along the baseline (bottom of text and components)
We take advantage of the Pos constants of BASELINE_LEFT and BASELINE_RIGHT to align half the text to the
right and the other half to the left, as shown in Listing 5-4.
Listing 5-4. Example Showing How to Use a TilePane and StackPane to Align Nodes
public class AlignUsingStackAndTile extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
StackPane left = new StackPane();
left.setStyle("-fx-background-color: black");
Text text = new Text("JavaFX");
text.setFont(Font.font(null, FontWeight.BOLD, 18));
text.setFill(Color.WHITE);
StackPane.setAlignment(text, Pos.BASELINE_RIGHT);
left.getChildren().add(text);
Text right = new Text("Reversi");
right.setFont(Font.font(null, FontWeight.BOLD, 18));
TilePane tiles = new TilePane();
tiles.setSnapToPixel(false);
TilePane.setAlignment(right, Pos.BASELINE_LEFT);
tiles.getChildren().addAll(left, right);
Scene scene = new Scene(tiles, 400, 100);
left.prefWidthProperty().bind(scene.widthProperty().divide(2));
left.prefHeightProperty().bind(scene.heightProperty());
primaryStage.setScene(scene);
primaryStage.show();
}
}
 
Search WWH ::




Custom Search