繁体   English   中英

如何在JavaFX中将按钮对齐到右下角?

[英]How to align a button into the bottom right corner in JavaFX?

我是JavaFX的新手,我正试图在应用程序的右下角找到一个按钮(特别是scrapeBtn )。 这是我到目前为止:

package main;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Driver extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Button scrapeBtn = new Button();
        scrapeBtn.setText("Scrape!");
        scrapeBtn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                System.out.println("Scrape button pressed.");
            }
        });

        TextField console = new TextField();

        GridPane root = new GridPane();    

        GridPane.setConstraints(scrapeBtn, 2, 2, 1, 1);
        root.getChildren().add(scrapeBtn);

        root.getChildren().add(console);

        Scene scene = new Scene(root, 600, 400);

        primaryStage.setTitle("Wiki Scraper");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

关于如何实现这一目标的任何想法? 一般来说,使用JavaFX对齐和格式化事物的一些技巧也非常受欢迎。

谢谢。

我经常使用BorderPane用于类似目的(例如,在中心有一些文本和控件等的对话框,在底部有一个或多个按钮)。 因此,我将BorderPane用作root,将HBox用作底部的“按钮容器”。 最后,我将botton对齐设置为“RIGHT”。

这是一个基于您的代码的示例:

@Override
public void start(Stage primaryStage) {

    // center
    VBox vbCenter = new VBox(); // use any container as center pane e.g. VBox
    TextField console = new TextField();
    vbCenter.getChildren().add(console);

    // bottom respectively "button area"
    HBox hbButtons = new HBox();
    Button scrapeBtn = new Button();
    scrapeBtn.setText("Scrape!");
    scrapeBtn.setOnAction(new EventHandler<ActionEvent>() {

      public void handle(ActionEvent event) {
        System.out.println("Scrape button pressed.");
      }
    });
    hbButtons.getChildren().add(scrapeBtn);
    hbButtons.setAlignment(Pos.CENTER_RIGHT);

    // root
    BorderPane root = new BorderPane();
    root.setPadding(new Insets(20)); // space between elements and window border
    root.setCenter(vbCenter);
    root.setBottom(hbButtons);

    Scene scene = new Scene(root, 600, 400);

    primaryStage.setTitle("Wiki Scraper");
    primaryStage.setScene(scene);
    primaryStage.show();
}

此代码导致此问题(在稍微调整窗口大小后):

结果

您可以使用两个BorderPanes将控件右下角放置

@Override
public void start(Stage primaryStage) throws Exception {
    BorderPane root = new BorderPane();

    BorderPane bottom = new BorderPane();
    bottom.setRight(new Button("I am placed bottom right"));
    root.setBottom(bottom);

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);

    primaryStage.setWidth(400);
    primaryStage.setHeight(400);
    primaryStage.show();
}

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM