清除 TextField JavaFX 的内容

Clearing contents of TextField JavaFX

我在使用 setText(" ") 方法清除 TextField 时遇到问题。 我正在尝试检查是否可以在图块中输入特定值,如果不能,那么我只想将其设置为空白 space。如果我在那里放任何其他值,它实际上会起作用,所以我认为这不是逻辑问题?如果我想将它设置为空白 space,它就不起作用。

平铺class

这就是问题所在class。

package com.company;

import javafx.application.Platform;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;

import static com.company.Game.TILE_SIZE;

public class Tile extends StackPane {

    private int x;
    private int y;
    private static int[][] tab = new int[][]
            {
                    {0, 8, 0, 0, 0, 0, 0},
                    {0, 0, 0, 5, 0, 0, 0},
                    {0, 11, 0, 0, 0, 0, 8},
                    {0, 0, 10, 0, 7, 0, 0},
                    {0, 9, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 6, 0},
                    {0, 11, 0, 0, 0, 7, 0},
                    {0, 0, 10, 0, 0, 0, 6}
            };
    private Utility utility = new Utility();

    private Rectangle border = new Rectangle(TILE_SIZE - 8, TILE_SIZE - 8);
    public TextField text = new TextField();


    public Tile(int x, int y) {
        Utility utility= new Utility();
        this.x = x;
        this.y = y;


        border.setStroke(Color.BLACK);

        text.setStyle("-fx-text-inner-color: black;" +
                "-fx-control-inner-background: green;"
                +"-fx-display-caret: false;");
        text.setTextFormatter (new TextFormatter<Integer>(c -> {
            if (c.getControlNewText().matches("^\d{1,2}")) {
                return c ;
            } else {
                return null ;
            }
        }));


        text.focusedProperty().addListener((arg0, oldValue, newValue) ->
        {
            if (!newValue) {
                if (text.getText().length() > 0)
                {
                    String cos = text.getText();
                    int cos2 = Integer.parseInt(cos);
                    if(utility.isWon(tab))
                    {
                        Platform.exit();
                        System.exit(0);
                    }
                    else
                    {
                        if (!utility.isMoveValid(tab, this.y, this.x, cos2))
                        {
                            text.setText(""); //it works if it's not a blank string

                        } else {
                            tab[this.y][this.x] = cos2;

                        }
                    }

                }
            }
        });

        text.setFont(Font.font(50));

        getChildren().addAll(border, text);
        setTranslateX(this.x*TILE_SIZE);
        setTranslateY(this.y*TILE_SIZE);

    }


}

您在文本字段的文本格式化程序上定义的 UnaryOperator text change filter 的 lambda 正则表达式阻止将文本字段设置为空值。

来自 javadoc:

The filter itself is an UnaryOperator that accepts TextFormatter.Change object. It should return a TextFormatter.Change object that contains the actual (filtered) change. Returning null rejects the change.

您将格式化程序过滤器定义为需要 "^\d{1,2}" 的匹配项,它不会匹配空字符串 "",因此它将不允许将文本字段设置为空字符串:

text.setTextFormatter (new TextFormatter<Integer>(c -> {
    if (c.getControlNewText().matches("^\d{1,2}")) {
        return c ;
    } else {
        return null ;
    }
}));

您需要修复匹配的正则表达式以允许空字符串或提供一些其他修复方法以允许字段中的空字符串值。例如:

text.setTextFormatter (new TextFormatter<Integer>(c -> {
    String newText = c.getControlNewText();
    if ("".equals(newText) || newText.matches("^\d{1,2}")) {
        return c ;
    } else {
        return null ;
    }
}));