如何直接消除文本字段 JavaFx 中的字符,以便用户无法输入字符?

How to eliminate chars in textfield JavaFx directly so the user can't type in chars?

你好,我想要一个文本框,用户只能在其中输入数字,而不能输入字符。我很困惑,因为有些人想用 actionlistener 来做,有些人想用 Formatter 和更多选项来做。有人可以告诉我该怎么做吗?以及将它放在代码中的什么位置,它总是检查是否有字母并自动删除它?谢谢

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;

import java.awt.*;
import java.util.function.UnaryOperator;

public class Controller {

    GuessingGame gg = new GuessingGame();
    //Main m = new Main();


    public Button playButton;
    public Button settingsButton;
    public Button exitButton;

    public TextField textfield;

    public Label countText;
    public Label notification;

    public int currentNumber = 0;
    public int numberBetweenLower = 0;
    public int numberBetweenHigher = 100;

    public void clickedOnExit() {
        System.exit(1);
    }

    ;

    public void clickedOnSettings() {
    }

    ;

    public void clickedOnPlay() {
        Main.pStage.setScene(Main.scene2);
    }

    ;

    public void clickedBackToMenu() {
        Main.pStage.setScene(Main.scene1);
    }

    ;

    public void enteredTextfield() {

        currentNumber = Integer.parseInt(textfield.getText());
        textfield.clear();

        GuessingGame.Result checkNumber;
        checkNumber = gg.evaluateEnteredNumber(currentNumber);
        if (checkNumber == GuessingGame.Result.HIGHER) {
            notification.setText("HIGHER");
        }
        ;
        if (checkNumber == GuessingGame.Result.LOWER) {
            notification.setText("LOWER");
        }
        ;
        if (checkNumber == GuessingGame.Result.EQUALS) {
            notification.setText("YOU GOT IT!");
        }
        ;

        countText.setText("" + gg.getCounter());
    }


    @FXML
    public void initialize() {
        // initialize controller and set text shown in the UI
    }

    private static final class NumberFormatUnaryOperator implements UnaryOperator<TextFormatter.Change> {

        /**
         * Applies this function to the given argument.
         *
         * @param change the function argument
         * @return the function result
         */
        @Override
        public TextFormatter.Change apply(TextFormatter.Change change) {
            var String = change.getControlNewText();

            if (!text.matches("\d*")) {
                Toolkit.getDefaultToolkit().beep();
                return null;
            }
            return change;
        }
    }
}

我个人比较喜欢Formatter方式,使用起来很简单。

例如:

public class TodoListController implements Initializable {

    @FXML
    public TextField input;

    @Override
    public void initialize(final URL url, final ResourceBundle resourceBundle) {
        input.setTextFormatter(new TextFormatter<>(new NumberFormatUnaryOperator()));
    }

    private static final class NumberFormatUnaryOperator implements UnaryOperator<TextFormatter.Change> {

       /**
        * Applies this function to the given argument.
        *
        * @param change the function argument
        * @return the function result
        */
        @Override
        public TextFormatter.Change apply(TextFormatter.Change change) {
           String text = change.getControlNewText();

           if (!text.matches("\d*")) {
               Toolkit.getDefaultToolkit().beep();
               return null;
           }

           return change;
        }
    }
}

上面的代码根本无法输入任何字符...

您也可以使用Listener 方式,实际上这两种方式都非常适合您要解决的问题。此外,就 JavaFX API 而言,格式化程序正是为此类用例设计的。欢迎在您的代码中使用它