gpt4 book ai didi

javafx - 标签文字位置

转载 作者:行者123 更新时间:2023-12-04 00:09:46 25 4
gpt4 key购买 nike

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

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);

我得到这个作为视觉结果:

enter image description here

如何更改文本位置?我想在图片下方设置文字?

最佳答案

label.setContentDisplay(ContentDisplay.TOP);

玩这个以查看不同对齐设置的效果:
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);
}
}

关于javafx - 标签文字位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22609672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com