【问题标题】:setWrapText(true) is not working for Label in JavaFXsetWrapText(true) 不适用于 JavaFX 中的标签
【发布时间】:2014-04-16 13:02:00
【问题描述】:

我在 JavaFX 中创建了一个 Label,其中包含很多文本。

Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
            + "software to find both the C-Mark and monthly attendance "
            + "of students. Inorder to use the features of this software,"
            + " user has to create an account for him first. Then he should "
            + "login using the username and password. He will be able to "
            + "perform all the operations then. Further details are mentioned"
            + " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);

在此代码中,setWrapText(true) 不起作用。为什么?我怎样才能让它发挥作用?

【问题讨论】:

  • 你是如何确定一个不起作用的?
  • @UlukBiy 因为我的标签内容是单行显示的。我不知道为什么会这样。你能提出一个解决方案吗?
  • 设置标签的首选宽度。或者将标签放入调整其子级大小的窗格中,例如 HBox。
  • @UlukBiy 谢谢...首选宽度有帮助。
  • @TomJ,我也遇到了同样的问题,但没有通过设置首选宽度来解决。有什么想法吗?谢谢。

标签: javafx label javafx-2


【解决方案1】:
Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

这是一个 SSCCE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class WrappedLabelExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

        StackPane root = new StackPane(l1);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

导致

【讨论】:

  • @James_D,你能解释一下这个问题是如何解决的吗?与问题相关的发布代码相比,你的回答代码有什么不同?谢谢。
  • 这篇文章有什么意义?你刚刚复制了问题吗?
  • 否:由于某种原因,OP 在我发布答案后编辑了问题以复制答案(请参阅edit history)。
  • 已编辑以包含 SSCCE 和屏幕截图。
【解决方案2】:

我在 Scene Builder 中遇到了同样的问题,其他解决方案都不起作用。

我终于通过将“最小高度”设置为 USE_PREF_SIZE 并将“首选项高度”保持在 USE_COMPUTED_SIZE 来实现它。

这将转换为 FXML,并将 minHeight 属性设置为 -Infinity

【讨论】:

    【解决方案3】:

    虽然上述解决方案可以工作,但我遇到的一个特殊问题是,如果将标签放置在 VBox 内,它不会包装 尽管设置了标签最大宽度并且 wrapText 是设置为真。在这种情况下,标签必须ALSO设置其首选项和最小高度才能开始换行,如果您在 VBox 中有多个标签并试图让它们工作,这可能会很烦人。

    private VBox myCode() {
        VBox container = new VBox();
        int maxWidth = 200;
        container.setMaxWidth(maxWidth);
    
        Label one = new Label("This is very long");
        one.setMaxWidth(maxWidth);
        one.setPrefWidth(maxWidth);
        one.setWrapText(true);
        // Tailored to the text
        one.setMinHeight(100);
        one.setPrefHeight(100);
    
        Label two = new Label("This is also very long");
        two.setMaxWidth(maxWidth);
        two.setPrefWidth(maxWidth);
        two.setWrapText(true);
        // Tailored to the text
        two.setMinHeight(200);
        two.setPrefHeight(200);
    
        container.getChildren().addAll(one, two);
        return container;
    }
    

    为避免与此相关的麻烦,您可以使用 TextFlow 对象而不是标签,并将一个 Text 对象放入 TextFlow 中,因为它将包裹文本而无需担心它的高度应该是多少。

    private static TextFlow aLabel(String... pString) {
         TextFlow textFlowLabel = new TextFlow();
         textFlowLabel.setMaxWidth(MAX_WIDTH);
         textFlowLabel.setPrefWidth(MAX_WIDTH);
         textFlowLabel.setTextAlignment(TextAlignment.JUSTIFY);
         for (String aString : pString) {
             textFlowLabel.getChildren().add(new Text(aString));
         }
         return textFlowLabel;
    }
    
    private VBox myCode() {
         VBox container = new VBox();
         int maxWidth = 200;
         container.setMaxWidth(maxWidth);
         
         TextFlow labelOne = aLabel("This is very long");
         TextFlow labelTwo = aLabel("This is also very long");
         container.getChildren().addAll(labelOne, labelTwo);
         return container;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 2014-08-17
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      相关资源
      最近更新 更多