JavaFX中的TextField类概述

The TextField class in JavaFX is used to create a control that allows the user to enter in a single line of text. It supports having prompt text (i.e., text that informs the user what the TextField is meant to be used for).

JavaFX中TextField类用于创建允许用户输入单行文本的控件。 它支持具有提示文本(即,通知用户TextField打算用于的文本)。

Note: If you need a multi-line text input control then have a look at the TextArea class. Alternatively, if you want the text to be formatted then have a look at the HTMLEditor class.

注意:如果需要多行文本输入控件,请查看TextArea类。 另外,如果要格式化文本,请查看HTMLEditor类。

进口声明 ( Import Statement )

import javafx.scene.control.TextField;

建设者 ( Constructors )

The TextField class has two constructors depending on whether you want to create an empty TextField or one with some default text:

TextField类具有两个构造函数,具体取决于您要创建一个空的TextField还是创建一个带有默认文本的构造器:

  • To create an empty TextField object:

    要创建一个空的TextField对象:

    TextField txtFld= new TextField();
  • To create a TextField with some default text use a String literal:

    要使用一些默认文本创建TextField ,请使用String文字

    TextField txtFld = new TextField("Default Text");

Note: Creating a TextField with default text is not the same as having prompt text. The default text will remain in the TextField when the user clicks on it and when they do will be editable.

注意:使用默认文本创建TextField与使用提示文本不同。 当用户单击默认文本时,默认文本将保留在TextField中 ,并且当它们可以编辑时。

有用的方法 ( Useful Methods )

If you create an empty TextField you can set the text using the setText method:

如果创建一个空的TextField ,则可以使用setText方法设置文本:

txtField.setText("Another String");

To get a String representing the text the user entered into a TextField use the getText method:

要获取表示用户输入到TextField中的文本的字符串 ,请使用getText方法:

String inputText = txtFld.getText();

事件处理 ( Event Handling )

The default event associated with the TextField is the ActionEvent. This is triggered if the user hits ENTER whilst inside the TextField To set up the EventHandler for an ActionEvent use the setOnAction method:

TextField关联的默认事件是ActionEvent 。 如果用户在TextField内部时按ENTER ,则将触发此事件。要为ActionEvent设置EventHandler ,请使用setOnAction方法:

txtFld.setOnAction(new EventHandler{
@Override public void handle(ActionEvent e) {
//Place the code you want to execute on the press of the ENTER key.
}
});

使用技巧 ( Usage Tips )

Take advantage of the ability to set prompt text for the TextField if you need to help the user understand what the TextField is for. Prompt text appears in the TextField as slightly greyed out text. If the user clicks on the TextField the prompt text disappears and they have an empty TextField in which to input their own text. If the TextField is empty when it loses focus the prompt text will reappear. The prompt text will never be the String value returned by the getText method.

如果需要帮助用户了解TextField的用途,请利用为TextField设置提示文本的功能。 提示文本在文本字段中显示为浅灰色的文本。 如果用户单击TextField,则提示文本消失,并且他们有一个空的TextField可在其中输入自己的文本。 如果TextField失去焦点时为空,则提示文本将重新出现。 提示文本永远不会是getText方法返回的String值。

Note: If you create a TextField object with default text then setting the prompt text will not overwrite the default text.

注意:如果使用默认文本创建TextField对象,则设置提示文本将不会覆盖默认文本。

To set the prompt text for a TextField use the setPromptText method:

要设置TextField的提示文本,请使用setPromptText方法:

txtFld.setPromptText("Enter Name..");

To find out the value of the prompt text of a TextField object use the getPromptText method:

要查找TextField对象的提示文本的值,请使用getPromptText方法:

String promptext = txtFld.getPromptText();

It is possible to set a value for the number of characters a TextField will show. This is not the same as limiting the number of characters that can be entered into the TextField. This preferred column value is used when calculating the TextField''s preferred width - it is only a preferred value and the TextField might become wider due to layout settings.

可以为TextField将显示的字符数设置一个值。 这与限制可以在TextField中输入的字符数不同。 在计算TextField的首选宽度时,会使用此首选列值-它只是一个首选值,由于布局设置, TextField可能会变宽。

To set the preferred number of text columns use the setPrefColumnCount method:

要设置文本列的首选数量,请使用setPrefColumnCount方法:

txtFld.setPrefColumnCount(25);

翻译自: https://www.thoughtco.com/textfield-overview-2033936

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值