JavaFX基本控件-Label
- 常用属性
- text
- padding
- alignment
- textAlignment
- width
- height
- tooltip
- border
- wrapText
- ellipsisString
- underline
- 实现方式
- Java实现
- fxml实现
常用属性
text
设置文本内容
label.setText("这是一个测试数据");
padding
内边距
label.setPadding(new Insets(5, 10, 5, 10));
alignment
文本对齐方式,
单行
显示内容少的时候体现
label.setAlignment(Pos.CENTER);
textAlignment
文本对齐方式,在
多行
文本换行的时候着重体现
label.setTextAlignment(TextAlignment.LEFT);
width
label.setMinWidth(100);
label.setPrefWidth(100);
label.setMaxWidth(200);
height
label.setMinHeight(50);
label.setPrefHeight(50);
label.setMaxHeight(100);
tooltip
提示信息,鼠标放上去会给出一个提示
label.setTooltip(new Tooltip("测试Tooltip"));
border
设置边框
Border border = new Border(new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3)));
label.setBorder(border);
wrapText
设置是否自动换行
label.setWrapText(true);
ellipsisString
设置超长的省略号(自定义字符串)
label.setEllipsisString("^^^^^^");
underline
设置文本下划线
label.setUnderline(true);
实现方式
Java实现
public static VBox demo1() {
VBox vBox = new VBox();
vBox.setPadding(new Insets(10, 10, 5, 20));
Label label = build();
label.setBorder(new Border(new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
label.setText("这是一个测试数据,测试一下换行数据,越野与呜呜呜呜测试,多行越界");
label.setAlignment(Pos.CENTER_RIGHT);
label.setTextAlignment(TextAlignment.RIGHT);
vBox.getChildren().add(label);
label = build();
label.setBorder(new Border(new BorderStroke(Paint.valueOf("blue"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
label.setText("单行数据");
label.setAlignment(Pos.CENTER_LEFT);
label.setTextAlignment(TextAlignment.RIGHT);
vBox.getChildren().add(label);
label = new Label();
label.setBorder(new Border(new BorderStroke(Paint.valueOf("green"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
label.setText("这是一个测试数据,测试数据多行捕不越界");
label.setAlignment(Pos.CENTER_RIGHT);
label.setTextAlignment(TextAlignment.RIGHT);
vBox.getChildren().add(label);
return vBox;
}
private static Label build() {
Label label = new Label();
label.setBorder(new Border(new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
label.setPadding(new Insets(5, 10, 5, 10));
label.setTooltip(new Tooltip("测试Tooltip"));
label.setText("测试数据");
label.setAlignment(Pos.CENTER_RIGHT);
label.setTextAlignment(TextAlignment.RIGHT);
label.setMinWidth(100);
label.setPrefWidth(100);
label.setMaxWidth(200);
label.setMinHeight(50);
label.setPrefHeight(50);
label.setMaxHeight(100);
label.setWrapText(true);
label.setEllipsisString("^^^^^^");
return label;
}
fxml实现
<Label alignment="CENTER" ellipsisString="^^^^^^" maxHeight="100" maxWidth="200" prefHeight="50" prefWidth="100"
text="这是一个测试数据,测试一下换行数据" textAlignment="RIGHT" textFill="#877f3a"
underline="true" wrapText="true">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<font>
<Font name="Arial Bold" size="14.0"/>
</font>
</Label>