控制显示多行文字? [英] Control for displaying multiline text?

查看:118
本文介绍了控制显示多行文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示多行只读文本-可以使用哪个控件?它应该只显示文本,就像Label一样,但是Label不支持多行?

I need to display a multiline, read-only text - which control can be used for that? It should only display the text, like a Label does, however Label does not support multiline?

感谢任何提示:-)

推荐答案

您可以在 标签

如果文本中包含 \n (换行符)字符,则标签将换行到换行符。

If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.

如果标签具有 wrapText 设置为true,并且没有足够的空间来显示单行文本,那么标签会将文本换行到新行。

If the label has wrapText set to true and there is not enough space to display a single line of text, then the label will wrap the text to a new line.

如果要使用不同样式的文本,则在使用JavaFX 2的情况下,请将多个标签放在 FlowPane ;或者,如果您使用的是Java 8+,则将标签放在 TextFlow 组件。

If you want text in different styles, then, if using JavaFX 2, place multiple labels in a FlowPane or, if you are using Java 8+, place the labels in a TextFlow component.

由以下代码产生:

import javafx.scene.Scene;

import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class LoveMeNot extends Application {

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

  @Override public void start(final Stage stage) throws Exception {
    Label label = new Label(WORDS);
    label.setWrapText(true);
    label.setStyle("-fx-font-family: \"Comic Sans MS\"; -fx-font-size: 20; -fx-text-fill: darkred;");

    ImageView image = new ImageView(IMAGE);
    image.setOpacity(0.3);

    StackPane layout = new StackPane();
    layout.setStyle("-fx-background-color: mistyrose; -fx-padding: 10;");
    layout.getChildren().setAll(image, label);

    stage.setTitle("Love Me Not");
    stage.setScene(new Scene(layout));
    stage.show();
  }

  // creates a triangle.
  private static final String WORDS = 
    "Love not me for comely grace,\n" +
    "For my pleasing eye or face,\n" +
    "Nor for any outward part,\n" +
    "No, nor for my constant heart,\n" +
    "For these may fail, or turn to ill.\n" +
    "So thou and I must sever.\n" +
    "Keep therefore a true woman’s eye,\n" +
    "And love me still, but know not why,\n" +
    "So hast thou the same reason still\n" +
    "To doat upon me ever.";

  private static final Image IMAGE = 
    new Image("http://icons.iconarchive.com/icons/artdesigner/gentle-romantic/256/rose-icon.png");
}

尝试运行上述程序并调整窗口大小,以查看<标签文本中的code> \n 新行值以及标签上的 wrapText 属性。将 wrapText 的设置从true更改为false,以查看打开和关闭 wrapText 的区别。

Try running the above program and resizing the window to see the effect of the \n new line values in the label's text as well as the wrapText property on the label. Vary the wrapText setting from true to false to see the difference between having wrapText switched on and off.

这篇关于控制显示多行文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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