TextField
用于单行文本输入。请看下面的示例 -
import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.Label
import javafx.scene.control.TextField
import javafx.scene.layout.GridPane
import javafx.stage.Stage
// at W w w .y I Iba I .C o m
public class Main extends Application {
public static void main(String[] args) {
launch(args)
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250)
TextField notification = new TextField ()
notification.setText("Label")
notification.clear()
GridPane grid = new GridPane()
grid.setVgap(4)
grid.setHgap(10)
grid.setPadding(new Insets(5, 5, 5, 5))
grid.add(new Label("To: "), 0, 0)
grid.add(notification, 1, 0)
Group root = (Group) scene.getRoot()
root.getChildren().add(grid)
stage.setScene(scene)
stage.show()
}
}
TextField
和Password
字段扩展了TextInput
类,它是JavaFX中所有文本控件的超类。
上面的代码生成以下结果。
创建文本域
我们可以使用TextField
类的构造函数来创建文本字段。
TextField
只是一个带有光标的文本输入框,通常我们需要一个Label
控件来告诉文本字段的目的。以下代码创建一个Label
控件来标记对应的文本字段是用于名称输入。然后它创建一个TextField
对象。之后,它使用HBox布局Label
和TextField
。
Label label1 = new Label("Name:")
TextField textField = new TextField ()
HBox hb = new HBox()
hb.getChildren().addAll(label1, textField)
hb.setSpacing(10)
使用预定义文本创建文本字段。
TextField textField = new TextField("yiibai.com")
TextField文本
要从文本字段获取值,请调用getText()
方法。
从TextInput
的setPrefColumnCount
方法设置文本字段的大小。 通过设置一次可以显示的最大字符数。
我们可以使用提示字幕通知用户文本字段的用途。setPromptText()
方法定义显示在文本字段中的字符串。无法通过getText()
方法获取提示文本。
以下代码显示如何设置TextField
的提示文本
import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.TextField
import javafx.scene.layout.GridPane
import javafx.stage.Stage
// by w W w. y I i b A i. c O M
public class Main extends Application {
@Override
public void start(Stage stage) {
Group root = new Group()
Scene scene = new Scene(root, 300, 150)
stage.setScene(scene)
stage.setTitle("Text Field Sample")
GridPane grid = new GridPane()
grid.setPadding(new Insets(10, 10, 10, 10))
grid.setVgap(5)
grid.setHgap(5)
scene.setRoot(grid)
final TextField name = new TextField()
name.setPromptText("Enter your first name.")
name.setPrefColumnCount(10)
name.getText()
GridPane.setConstraints(name, 0, 0)
grid.getChildren().add(name)
stage.show()
}
public static void main(String[] args) {
launch(args)
}
}
以下列表有一些有用的方法,可以使用它们在文本字段中进行文本编辑。
copy()
- 将所选文本设置为剪贴板。cut()
- 将所选文本设置为剪贴板并删除当前选择。selectAll()
- 选择文本输入中的所有文本。paste()
- 将剪贴板中的内容设置为此文本并替换当前选择。
上面的代码生成以下结果。
示例-1
以下代码显示如何将字符串值从TextField
绑定到Stage Title。
import javafx.application.Application
import javafx.beans.property.SimpleStringProperty
import javafx.beans.property.StringProperty
import javafx.scene.Scene
import javafx.scene.control.Label
import javafx.scene.control.TextField
import javafx.scene.layout.HBox
import javafx.stage.Stage
public class Main extends Application {
StringProperty title = new SimpleStringProperty()
public static void main(String[] args) {
Application.launch(args)
}
@Override
public void start(Stage stage) {
TextField titleTextField
titleTextField = new TextField()
titleTextField.setText("Stage Coach")
titleTextField.setPrefColumnCount(15)
HBox hBox = new HBox()
hBox.setSpacing(10)
hBox.getChildren().add(new Label("title:"))
hBox.getChildren().add(titleTextField)
Scene scene = new Scene(hBox,270,270)
title.bind(titleTextField.textProperty())
stage.setScene(scene)
stage.titleProperty().bind(title)
stage.show()
}
}
上面的代码生成以下结果。
实例-2
以下代码显示了如何将ContextMenu
添加到TextField
。
import javafx.application.Application
import javafx.event.ActionEvent
import javafx.event.EventHandler
import javafx.geometry.Insets
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.ContextMenu
import javafx.scene.control.Label
import javafx.scene.control.MenuItem
import javafx.scene.control.TextField
import javafx.scene.layout.GridPane
import javafx.stage.Stage
import javafx.stage.WindowEvent
public class Main extends Application {
public static void main(String[] args) {
launch(args)
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250)
TextField notification = new TextField()
final ContextMenu contextMenu = new ContextMenu()
contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing")
}
})
contextMenu.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown")
}
})
MenuItem item1 = new MenuItem("About")
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About")
}
})
MenuItem item2 = new MenuItem("Preferences")
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences")
}
})
contextMenu.getItems().addAll(item1, item2)
notification.setContextMenu(contextMenu)
GridPane grid = new GridPane()
grid.setVgap(4)
grid.setHgap(10)
grid.setPadding(new Insets(5, 5, 5, 5))
grid.add(new Label("To: "), 0, 0)
grid.add(notification, 1, 0)
Group root = (Group) scene.getRoot()
root.getChildren().add(grid)
stage.setScene(scene)
stage.show()
}
}
上面的代码生成以下结果。
实例-3
覆盖replaceText
和replaceSelection
以创建自定义的TextField
,如下所示 -
import javafx.application.Application
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.TextField
import javafx.scene.paint.Color
import javafx.stage.Stage
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args)
}
@Override
public void start(Stage primaryStage) {
Group root = new Group()
Scene scene = new Scene(root, 300, 250, Color.WHITE)
TextField field = new TextField() {
@Override
public void replaceText(int start, int end, String text) {
if (!text.matches("[a-z]")) {
super.replaceText(start, end, text)
}
}
@Override
public void replaceSelection(String text) {
if (!text.matches("[a-z]")) {
super.replaceSelection(text)
}
}
}
root.getChildren().add(field)
primaryStage.setScene(scene)
primaryStage.show()
}
}
上面的代码生成以下结果,只能输入数字值 -