TextField类是JavaFX包的一部分。它是一个允许用户输入一行未格式化文本的组件,不允许多行输入,只允许用户输入一行文本。然后可以根据需要使用文本。
null
TextField类的构造函数 :
- TextField() :创建包含空文本内容的新文本字段
- 文本字段(字符串s) :创建带有初始文本的新文本字段。
常用方法 :
方法 | 解释 |
---|---|
setPrefColumnCount(int v) | 设置属性prefColumnCount的值。 |
setOnAction(EventHandler值) | 设置属性onAction的值。 |
设置对齐(位置v) | 设置特性路线的值。 |
prefColumnCountProperty() | 首选的文本列数 |
onActionProperty() | 与此文本字段关联的操作处理程序,如果未分配任何操作处理程序,则为null。 |
getPrefColumnCount() | 获取属性prefColumnCount的值。 |
getOnAction() | 获取属性onAction的值。 |
getAlignment() | 获取属性对齐的值。 |
getCharacters() | 返回支持文本字段内容的字符序列。 |
以下程序说明了文本字段的使用:
- Java程序创建文本字段并将其添加到stage :该程序将创建一个名为b的文本字段。文本字段将在场景中创建,而场景又将托管在舞台(顶层JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法将文本字段附加到场景中,以及代码中(200200)指定的分辨率。最后调用show()方法来显示最终结果。
// Java program to create a textfield and add it to stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.StackPane;
import
javafx.stage.Stage;
public
class
Textfield
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating TextField"
);
// create a textfield
TextField b =
new
TextField();
// create a stack pane
StackPane r =
new
StackPane();
// add textfield
r.getChildren().add(b);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- Java程序创建一个带有初始文本的文本字段,并添加一个事件处理程序 :此程序创建一个由名称b指示的文本字段。我们将创建一个标签,当按下enter键时,该标签将显示文本。我们将创建一个事件处理程序来处理文本字段的事件,并使用setOnAction()方法将事件处理程序添加到文本字段中。文本字段将在场景中创建,而场景又将托管在一个stage(顶层JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法将文本字段和标签以及代码中(200200)指定的分辨率附加到场景中。最后,调用show()方法来显示最终结果。
// Java program to create a textfield and add a
// event handler to handle the event of textfield
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public
class
Textfield_1
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating textfield"
);
// create a textfield
TextField b =
new
TextField(
"initial text"
);
// create a tile pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"no text"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(b.getText());
}
};
// when enter is pressed
b.setOnAction(event);
// add textfield
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- Java程序创建一个带有初始文本的文本字段,并添加一个事件处理程序 :此程序创建一个名为b的文本字段。我们将通过使用字符串调用其构造函数来设置初始文本,并使用setPrefColumnCount()方法设置首选列计数。我们将创建一个标签,按下enter键时显示文本。我们将创建一个事件处理程序来处理文本字段的事件,并使用setOnAction()方法将事件处理程序添加到文本字段中。文本字段将在场景中创建,而场景又将托管在一个stage(顶层JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法将文本字段和标签以及代码中(200200)指定的分辨率附加到场景中。最后,调用show()方法来显示最终结果。
// Java program to create a textfield with a initial text
// and preferred column count and add a event handler to
// handle the event of textfield
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
public
class
TextField_2
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating textfield"
);
// create a textfield
TextField b =
new
TextField(
"initial text"
);
// set preffered column count
b.setPrefColumnCount(
7
);
// create a tile pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"no text"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(b.getText());
}
};
// when enter is pressed
b.setOnAction(event);
// add textfield
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
- Java程序创建一个文本字段,其中包含初始文本和文本的中心对齐,并添加一个事件处理程序 :此程序创建一个由名称b指示的文本字段。我们将通过使用字符串调用其构造函数来设置初始文本,并使用setAlignment()方法设置对齐方式。我们将创建一个标签,按下enter键时显示文本。我们将创建一个事件处理程序来处理文本字段的事件,并使用setOnAction()方法将事件处理程序添加到文本字段中。文本字段将在场景中创建,而场景又将托管在一个stage(顶层JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法将文本字段和标签以及代码中(200200)指定的分辨率附加到场景中。最后,调用show()方法来显示最终结果。
// Java program to create a textfield with a initial text and center alignment of text
// and add a event handler to handle the event of textfield
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.control.Label;
import
javafx.stage.Stage;
import
javafx.geometry.*;
public
class
TextField_4
extends
Application {
// launch the application
public
void
start(Stage s)
{
// set title for the stage
s.setTitle(
"creating textfield"
);
// create a textfield
TextField b =
new
TextField(
"initial text"
);
// set alignment of text
b.setAlignment(Pos.CENTER);
// create a tile pane
TilePane r =
new
TilePane();
// create a label
Label l =
new
Label(
"no text"
);
// action event
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
l.setText(b.getText());
}
};
// when enter is pressed
b.setOnAction(event);
// add textfield
r.getChildren().add(b);
r.getChildren().add(l);
// create a scene
Scene sc =
new
Scene(r,
200
,
200
);
// set the scene
s.setScene(sc);
s.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出 :
注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。
参考 : https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextField.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END