【问题标题】:changing hbox nodes position更改 hbox 节点位置
【发布时间】:2017-04-20 03:51:52
【问题描述】:

我想改变hbox中每个按钮的位置:

HBox buttom = new HBox();

Button delete_button = new Button("Delete");
Button showAll_button = new Button("Show All");
Button back_button = new Button("Back");

buttomPaneRight_deleteScene.getChildren().addAll(back_button,showAll_button,delete_button);

BorderPane basePane = new BorderPane();
basePane.setButtom(buttomPaneRight_deleteScene):

我想更改每个按钮的位置,例如 back_button 位于左角,delete_button、showAll_button 位于右角。 我检查了setAlignment(Pos.Value); 但这改变了整个hbox pos

【问题讨论】:

  • 你为什么不尝试锚窗格而不是 Hbox ?还是这些的混合?
  • 您可以将 Pane 设置为 GridPane,并将 ColumnConstraints 设置为 GridPane 中的所需位置,或者,您可以在第一个 Hbox 下方的边框窗格中为您的按钮嵌套另一个 HBox
  • 您希望deleteButton 在哪里?正如其他人所建议的那样,您(可能)使用了错误的布局窗格,但除非您更准确地指定您想要的内容,否则很难说出正确的布局窗格是什么。
  • @James_D delete_button 和 showAll_button 在右上角,back_button 在左角

标签: java javafx hbox


【解决方案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);
    }
}

另一种解决方案是添加一个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 而不是HBox,并将右侧的两个按钮包裹在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);
    }
}

第四个解决方案是使用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);
    }
}

【讨论】:

    【解决方案2】:

    尝试使用不同的窗格选项,正如我之前提到的,GridPane 应该可以解决您的问题,或者嵌套另一个 HBox

    看到这个http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm

    【讨论】:

    • 这更适合评论而不是答案。
    • 不值得投反对票,考虑到 oracle 页面应该解释 JavaFX 中的布局...编辑:谢谢
    • 本网站不鼓励仅链接的答案。虽然您确实建议了一种替代布局,但您可能应该至少总结该页面中显示的内容,以了解如何使用GridPane 来解决这个特定问题(这不是特别明显)。 (您还应该链接到文档的当前版本,而不是旧的 2.x 版本。)
    【解决方案3】:

    基本上不要使用HBox。 AnchorPane 更适合您想要实现的目标。

    AnchorPane holder = new AnchorPane();
    
    Button delete_button = new Button("Delete");
    Button showAll_button = new Button("Show All");
    Button back_button = new Button("Back");
    
    // Add buttons to holder
    holder.getChildren().add(delete_button);
    holder.getChildren().add(showAll_button);
    holder.getChildren().add(back_button);
    
    // Delete top left
    AnchorPane.setTopAnchor (delete_button, 0.0);
    AnchorPane.setLeftAnchor(delete_button, 0.0);
    
    // Show all top right
    AnchorPane.setTopAnchor  (showAll_button, 0.0);
    AnchorPane.setRightAnchor(showAll_button, 0.0);
    
    // Back bottom right
    AnchorPane.setBottomAnchor(back_button, 0.0);
    AnchorPane.setRightAnchor (back_button, 0.0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多