1つの解決策(多くあります)は、右にあるボタンを別のボタンでラップするだけですHBox
:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
HBox hbox = new HBox();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
HBox rightButtons = new HBox(deleteButton, showAllButton);
rightButtons.setAlignment(Pos.CENTER_RIGHT);
HBox.setHgrow(rightButtons, Priority.ALWAYS);
hbox.getChildren().addAll(backButton, rightButtons);
hbox.setPadding(new Insets(2));
root.setBottom(hbox);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

別の解決策は、スペーサーとして機能する a を追加し、Pane
できるだけ大きくすることです。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
HBox hbox = new HBox();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
Pane spacer = new Pane();
HBox.setHgrow(spacer, Priority.ALWAYS);
hbox.getChildren().addAll(backButton, spacer, deleteButton, showAllButton);
hbox.setPadding(new Insets(2));
root.setBottom(hbox);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
AnchorPane
3 番目の解決策は、 の代わりにを使用HBox
し、右側の 2 つのボタンを でラップすることHBox
です。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
AnchorPane anchorPane = new AnchorPane();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
HBox rightButtons = new HBox(deleteButton, showAllButton);
anchorPane.getChildren().addAll(backButton, rightButtons);
AnchorPane.setBottomAnchor(rightButtons, 2.0);
AnchorPane.setBottomAnchor(backButton, 2.0);
AnchorPane.setLeftAnchor(backButton, 2.0);
AnchorPane.setRightAnchor(rightButtons, 2.0);
root.setBottom(anchorPane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
そして 4 番目の解決策は、次を使用することGridPane
です。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
GridPane gridPane = new GridPane();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
gridPane.add(backButton, 0, 0);
gridPane.add(deleteButton, 1, 0);
gridPane.add(showAllButton, 2, 0);
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().addAll(leftCol, new ColumnConstraints(), new ColumnConstraints());
gridPane.setPadding(new Insets(2));
root.setBottom(gridPane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}