简体   繁体   English

我怎么做 textField 在 Java 中只接受字符“N 或 E”?

[英]How i can do textField accepts only character "N or E" in Java?

How i can do textField accepts only letter "N or E" in Java?我怎么做 textField 在 Java 中只接受字母“N 或 E”? doesn't accept number and another characters.不接受数字和其他字符。

        textField.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() > 1) textField.setText(oldValue);
            if (newValue.matches("[^\\d]")) return;
            textField.setText(newValue.replaceAll("\\d*", ""));
        });

i tryed this, this worked for maxValue.我试过了,这对 maxValue 有效。 But i am need textField accept only "N" and "E" characters.但我需要 textField 只接受“N”和“E”字符。 So how i can do this?那么我该怎么做呢?

Use a TextFormatter .使用TextFormatter You can modify or veto the proposed change to the text.您可以修改或否决对文本的建议更改。 This version:这个版本:

  • Only accepts changes where the text typed (or pasted) is "N" or "E" (either upper or lower case)仅接受键入(或粘贴)的文本为“N”或“E”(大写或小写)的更改
  • Makes the text upper case使文本大写
  • Changes the proposed change so that it replaces existing text, instead of adding to it更改提议的更改以替换现有文本,而不是添加到其中
  • Allows for deleting the current text允许删除当前文本

Your exact requirements may differ slightly.您的确切要求可能略有不同。 See the Javadocs for TextFormatter.Change for more details.有关更多详细信息,请参阅TextFormatter.ChangeJavadoc

import java.util.function.UnaryOperator;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class NorETextField extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField textField = new TextField();
        UnaryOperator<TextFormatter.Change> filter = c -> {
            if (c.getText().matches("[NnEe]")) {
                c.setText(c.getText().toUpperCase());
                c.setRange(0, textField.getText().length());
                return c ;
            } else if (c.getText().isEmpty()) {
                return c ;
            }
            return null ;
        };
        textField.setTextFormatter(new TextFormatter<String>(filter));
        BorderPane root = new BorderPane(textField);
        Scene scene = new Scene(root, 400, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

You can try this.你可以试试这个。 This accept only character N这仅接受字符 N

 Pattern pattern = Pattern.compile("N");
        UnaryOperator<TextFormatter.Change> filter = c -> {
            if (pattern.matcher(c.getControlNewText()).matches()) {
                return c ;
            } else {
                return null ;
            }
        };
        TextFormatter<String> formatter = new TextFormatter<>(filter);
        textField.setTextFormatter(formatter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能创建一个只接受字母退格和空格键的文本字段 - how can i make a textfield that only accepts letters backspace and spacebar only 如何验证只能接受整数的空textField? - How to validate an empty textField that can accepts only integers? 如何在文本字段中选择字符? - How do i select a character in a textfield? 如何确保Java Play应用程序仅接受来自特定主机的HTTP请求? - How do I ensure my Java Play application only accepts HTTP requests from a particular host? Java:如何为TextField做“ onclick”操作? - Java: How do I do a “onclick” for TextField? 如何限制TextField,使其只能包含一个&#39;。&#39; 字符? JavaFX的 - How to restrict TextField so that it can contain only one '.' character? JavaFX 如何在Java中验证文本字段 - How do I validate a textfield in java 在TextField中键入每个字符时,如何显示在TextArea中? - How do I show each character in TextArea when it is typed in TextField? 我如何在Kotlin中声明一个仅接受枚举的参数 - How can I declare an argument in Kotlin that accepts (only) enums 在 Java 正则表达式中,如何获取字符类(例如 [az])以匹配 - 减号? - In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM