JavaFX基本控件-TextField
- 常用属性
- text
- promptText
- padding
- alignment
- width
- height
- tooltip
- border
- editable
- disable
- visible
- 格式化
- 整形格式化
- 实现方式
- Java实现
- fxml实现
常用属性
text
设置文本内容
textField.setText("测试数据");
promptText
设置文本字段的提示文本,在文本为空时显示
textField.setPromptText("提示信息");
padding
内边距
textField.setPadding(new Insets(5, 10, 5, 10));
alignment
文本对齐方式
textField.setAlignment(Pos.CENTER);
width
textField.setMinWidth(100);
textField.setPrefWidth(100);
textField.setMaxWidth(200);
height
textField.setMinHeight(50);
textField.setPrefHeight(50);
textField.setMaxHeight(100);
tooltip
提示信息,鼠标放上去会给出一个提示
textField.setTooltip(new Tooltip("测试Tooltip"));
border
设置边框
Border border = new Border(new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3)));
textField.setBorder(border);
editable
设置控件是否可编辑(不能编辑,控件没变灰)
textField.setEditable(false);
disable
设置控件是否禁用(不能编辑,控件变灰)
textField.setDisable(false);
visible
设置控件是否可见
textField.setDisable(false);
格式化
textFormatter
用来控制输入内容的格式化
整形格式化
UnaryOperator<TextFormatter.Change> integerFilter = change -> {
String newText = change.getControlNewText();
try {
Integer.parseInt(newText);
return change;
} catch (Exception e) {
return null;
}
};
textField.setTextFormatter(new TextFormatter<Integer>(integerFilter));
实现方式
Java实现
public static VBox demo1() {
VBox vBox = new VBox();
vBox.setPadding(new Insets(10, 10, 5, 20));
TextField textField = build(null, "文字靠右");
vBox.getChildren().add(textField);
textField = build(null, "文字靠左");
textField.setBorder(new Border(new BorderStroke(Paint.valueOf("blue"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
textField.setAlignment(Pos.CENTER_LEFT);
vBox.getChildren().add(textField);
textField = build("内容1", "文字靠右");
textField.setBorder(new Border(new BorderStroke(Paint.valueOf("green"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
textField.setEditable(false);
vBox.getChildren().add(textField);
textField = build("内容2", "文字靠右");
textField.setDisable(true);
textField.setBorder(new Border(new BorderStroke(Paint.valueOf("black"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
vBox.getChildren().add(textField);
textField = build("内容3", "visible");
textField.setVisible(false);
vBox.getChildren().add(textField);
return vBox;
}
private static TextField build(String text, String promptText) {
TextField textField = new TextField();
textField.setBorder(new Border(new BorderStroke(Paint.valueOf("red"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(3))));
textField.setPadding(new Insets(5, 10, 5, 10));
textField.setTooltip(new Tooltip(text + "Tooltip"));
textField.setText(text);
textField.setPromptText(promptText);
textField.setAlignment(Pos.CENTER_RIGHT);
textField.setMinWidth(100);
textField.setPrefWidth(100);
textField.setMaxWidth(200);
textField.setMinHeight(50);
textField.setPrefHeight(50);
textField.setMaxHeight(100);
return textField;
}
fxml实现
<VBox prefHeight="400" prefWidth="600" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextField alignment="CENTER_RIGHT" maxHeight="100" maxWidth="200" prefHeight="50" prefWidth="100"
promptText="文字靠右">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<font>
<Font name="Arial Bold" size="14.0"/>
</font>
</TextField>
<TextField alignment="CENTER_LEFT" maxHeight="100" maxWidth="200" prefHeight="50" prefWidth="100"
disable="true" promptText="文字靠左">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<font>
<Font name="Arial Bold" size="14.0"/>
</font>
</TextField>
<TextField alignment="CENTER" maxHeight="100" maxWidth="200" prefHeight="50" prefWidth="100"
text="测试数据1" disable="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>
</TextField>
<TextField alignment="CENTER" maxHeight="100" maxWidth="200" prefHeight="50" prefWidth="100"
text="测试数据2" disable="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>
</TextField>
</children>
</VBox>