将标签定位到父窗格中的不同侧 [英] Position labels to different sides in the parent pane

查看:107
本文介绍了将标签定位到父窗格中的不同侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将文字定位到条形区域的问题。

I have a question about positioning Text into Bar area.

我创建了这个例子:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MainApp extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        FlowPane flow = new FlowPane();

        flow.setPrefSize(900, 30);

        Label label = new Label("Zoom 1.5");
        Label labelStat = new Label("Users 7");
        Label labelSec = new Label("Connected");

        flow.getChildren().addAll(label, labelStat, labelSec);

        HBox hb = new HBox();
        hb.getChildren().add(flow);

        Scene scene = new Scene(hb);

        stage.setTitle("JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }

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

如何获得这种视觉效果:

How I can get this visual result:

在我的情况下,我想调整主舞台的大小并保留文本的相对位置。如果有更合适的组件,我可以使用不同的布局更改FlowPane。

In my case I want to resize the main stage and preserve the relative position of the Text. I can change FlowPane with different Layout if there is more suitable component.

推荐答案

您可以选择使用 StackPane 并以不同方式对齐其子项:

You may choose to use StackPane and align its children differently:

@Override
public void start( Stage stage )
{

    StackPane flow = new StackPane();

    flow.setPrefSize(900, 30);

    Label label = new Label("Zoom 1.5");
    Label labelStat = new Label("Users 7");
    Label labelSec = new Label("Connected");

    StackPane.setAlignment( label, Pos.CENTER_LEFT );
    StackPane.setAlignment( labelStat, Pos.CENTER );
    StackPane.setAlignment( labelSec, Pos.CENTER_RIGHT );

    flow.getChildren().addAll(label, labelStat, labelSec);

    Scene scene = new Scene(flow);

    stage.setScene(scene);
    stage.show();
}

这篇关于将标签定位到父窗格中的不同侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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