如何在JavaFX中包装标签的文本?
为避免这种情况,您可以将文本包装在指定的宽度内,您需要调用setWrapText()方法。将true 用作此方法的参数时,超过指定宽度的标签内容将被包装到下一行。
示例
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class LabelWrapExample extends Application {
public void start(Stage stage) {
String text = "nhooo.com originated from the idea that there exists a class
of readers who respond better to online content and prefer to learn new skills at
their own pace from the comforts of their drawing rooms.";
//创建标签
Label label = new Label(text);
//包装标签
label.setWrapText(true);
//将对齐方式设置为标签
label.setTextAlignment(TextAlignment.JUSTIFY);
//设置标签的最大宽度
label.setMaxWidth(200);
//设置标签的位置
label.setTranslateX(25);
label.setTranslateY(25);
Group root = new Group();
root.getChildren().add(label);
//设置舞台
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Label Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
热门推荐
10 生日祝福语朋友简短的话
11 宝贝的人生祝福语简短
12 新年有哪些祝福语简短
13 简短又洋气祝福语大全
14 简短祝福语除夕快乐英文
15 生日贺卡祝福语毕业简短
16 战友聚会祝福语幽默简短
17 最新中秋祝福语简短英语
18 夏日升学宴祝福语简短