在单个hbox中将2个元素分开对齐 [英] Giving 2 elements seperate alignments in a single hbox

查看:255
本文介绍了在单个hbox中将2个元素分开对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取2个元素(一个按钮和一个标签),以便在javafx的单个HBox中具有各自的对齐方式。到目前为止,我的代码是:

I am trying to get 2 elements, a button and a label, to have their own individual alignments in a single HBox in javafx. My code thus far:

Button bt1= new Button("left");
bt1.setAlignment(Pos.BASELINE_LEFT);

Label tst= new Label("right");
tst.setAlignment(Pos.BASELINE_RIGHT);

BorderPane barLayout = new BorderPane();
HBox bottomb = new HBox(20);
barLayout.setBottom(bottomb);
bottomb.getChildren().addAll(bt1, tst);

默认情况下,hbox将两个元素都向左推,彼此相邻。

by default, the hbox shoves both elements to the left, next to each other.

我的项目现在必须使用borderpane布局,但是就目前而言,是否有某种方法可以迫使标签tst停留在hbox的最右侧,而bt1留在最左边?

The borderpane layout is right now necessary for my project, but as for right now, is there some way to force the label tst to stay on the far right side of the hbox, and bt1 to stay on the far left?

如果-fx-stylesheet的东西可以这样工作,我也可以做CSS。

I can also do css, if -fx-stylesheet stuff works this way.

推荐答案

您需要将左侧节点添加到AnchorPane,并使该AnchorPane水平增长。

You need to add the left node to an AnchorPane and make that AnchorPane grow horizontally.

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication33 extends Application {

    @Override
    public void start(Stage primaryStage)
    {
        BorderPane bp = new BorderPane();
        HBox hbox = new HBox();
        bp.setBottom(hbox);

        Button btnLeft = new Button("Left");
        Label lblRight = new Label("Right");

        AnchorPane apLeft = new AnchorPane();
        HBox.setHgrow(apLeft, Priority.ALWAYS);//Make AnchorPane apLeft grow horizontally
        AnchorPane apRight = new AnchorPane();
        hbox.getChildren().add(apLeft);
        hbox.getChildren().add(apRight);

        apLeft.getChildren().add(btnLeft);
        apRight.getChildren().add(lblRight);

        Scene scene = new Scene(bp, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

这篇关于在单个hbox中将2个元素分开对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆