温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - How to eliminate chars in textfield JavaFx directly so the user can't type in chars?
java javafx textfield

java - 如何在文本字段JavaFx中直接消除字符,以使用户无法输入字符?

发布于 2020-03-28 23:29:42

您好,我想有一个textfield,其中用户只能输入数字,而用户不能输入字符。我很困惑,因为有些人想用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;
        }
    }
}

查看更多

查看更多

提问者
Mindmax
被浏览
133
t-jd 2020-01-31 21:43

我个人更喜欢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而言,格式化程序正是针对此类用例而设计的。随意在您的代码中使用它