标签文字位置 [英] Label text position

查看:119
本文介绍了标签文字位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带图像和文字的标签

I have a Label with an image and text

final Label label = new Label(labelText);
label.setTextAlignment(TextAlignment.CENTER);

ImageView livePerformIcon = new ImageView(MainApp.class.getResource("/images/Folder-icon.png").toExternalForm());
label.setGraphic(livePerformIcon);

我认为这是一个直观的结果:

I get this as a visual result:

我怎么能改变文字位置?我想在图像下方设置文本?

How I can change the text position? I want to set the text below the Image?

推荐答案

label.setContentDisplay(ContentDisplay.TOP);

使用此功能查看不同对齐设置的效果:

Play with this to see the effect of the different alignment settings:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class LabelGraphicAlignmentTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Label label = new Label("Some\ntext");
        label.setGraphic(new ImageView(getClass().getResource("/images/Folder-icon.png").toExternalForm()));
        label.setMaxWidth(Double.POSITIVE_INFINITY);
        label.setMaxHeight(Double.POSITIVE_INFINITY);
        label.setStyle("-fx-border-color: blue;");
        root.setCenter(label);

        ComboBox<ContentDisplay> contentDisplayBox = new ComboBox<>();
        contentDisplayBox.getItems().addAll(ContentDisplay.values());
        contentDisplayBox.getSelectionModel().select(ContentDisplay.LEFT);
        label.contentDisplayProperty().bind(contentDisplayBox.valueProperty());

        ComboBox<Pos> alignmentBox = new ComboBox<>();
        alignmentBox.getItems().addAll(Pos.values());
        alignmentBox.getSelectionModel().select(Pos.CENTER);
        label.alignmentProperty().bind(alignmentBox.valueProperty());

        ComboBox<TextAlignment> textAlignmentBox = new ComboBox<>();
        textAlignmentBox.getItems().addAll(TextAlignment.values());
        textAlignmentBox.getSelectionModel().select(TextAlignment.LEFT);
        label.textAlignmentProperty().bind(textAlignmentBox.valueProperty());

        GridPane ctrls = new GridPane();
        ctrls.setHgap(5);
        ctrls.setVgap(5);
        ctrls.setPadding(new Insets(10));

        ctrls.addRow(0, new Label("Content display:"), new Label("Alignment:"), new Label("Text Alignment:"));
        ctrls.addRow(1,  contentDisplayBox, alignmentBox, textAlignmentBox);

        root.setTop(ctrls);

        Scene scene = new Scene(root, 600, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

这篇关于标签文字位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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