JavaFX文本域(输入框)

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;

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("sss")

TextField文本

要从文本字段获取值,请调用getTexfdt()方法。

从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;

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();
    }
}

此处为语雀视频卡片,点击链接查看:Video_2022-04-28_161022.wmv

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值