清除TextField JavaFX的内容

清除TextField JavaFX的内容,javafx,textfield,Javafx,Textfield,我在使用setText(“”)方法清除TextField时遇到问题。 我试图检查是否可以在平铺中输入特定值,如果不能,我只想将其设置为空白。如果我把任何其他值放在那里,它实际上是有效的,所以我认为这不是逻辑问题?只有当我想将它设置为空白时,它才起作用 瓷砖类 这就是问题发生的班级 package com.company; import javafx.application.Platform; import javafx.scene.control.TextField; import javaf

我在使用setText(“”)方法清除TextField时遇到问题。 我试图检查是否可以在平铺中输入特定值,如果不能,我只想将其设置为空白。如果我把任何其他值放在那里,它实际上是有效的,所以我认为这不是逻辑问题?只有当我想将它设置为空白时,它才起作用

瓷砖类

这就是问题发生的班级

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

    }


}
package.com公司;
导入javafx.application.Platform;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TextFormatter;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.scene.text.Font;
导入静态com.company.Game.TILE\u大小;
公共类平铺扩展StackPane{
私人INTX;
私营企业;
私有静态int[][]选项卡=新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}
};
私有实用工具=新实用工具();
私有矩形边框=新矩形(瓷砖大小-8,瓷砖大小-8);
public TextField text=new TextField();
公共地砖(整块x,整块y){
实用工具=新实用工具();
这个.x=x;
这个。y=y;
边框。设定行程(颜色。黑色);
text.setStyle(“-fx文本内部颜色:黑色;”+
“-fx控件内部背景:绿色;”
+“-fx显示插入符号:false;”;
text.setTextFormatter(新的TextFormatter(c->{
如果(c.getControlNewText()匹配(“^\\d{1,2}”)){
返回c;
}否则{
返回null;
}
}));
text.focusedProperty().addListener((arg0,oldValue,newValue)->
{
如果(!newValue){
if(text.getText().length()>0)
{
字符串cos=text.getText();
int cos2=整数.parseInt(cos);
if(utility.isWon(选项卡))
{
Platform.exit();
系统出口(0);
}
其他的
{
如果(!utility.isMoveValid(tab,this.y,this.x,cos2))
{
text.setText(“”;//如果它不是空字符串,则可以使用
}否则{
tab[this.y][this.x]=cos2;
}
}
}
}
});
text.setFont(Font.Font(50));
getChildren().addAll(边框、文本);
setTranslateX(此.x*平铺大小);
setTranslateY(此.y*平铺大小);
}
}

在文本格式设置程序中为文本字段定义的lambda中的正则表达式阻止将文本字段设置为空值

从javadoc:

筛选器本身是一个接受
TextFormatter.Change
对象的
UnaryOperator
。它应该返回一个
TextFormatter.Change
对象,该对象包含实际的(过滤的)更改。返回null将拒绝更改

您将格式化程序筛选器定义为需要匹配的
“^\\d{1,2}”
,而该匹配不会匹配空字符串
,因此不允许将文本字段设置为空字符串:

text.setTextFormatter (new TextFormatter<Integer>(c -> {
    if (c.getControlNewText().matches("^\\d{1,2}")) {
        return c ;
    } else {
        return null ;
    }
}));
text.setTextFormatter(新的TextFormatter(c->{
如果(c.getControlNewText()匹配(“^\\d{1,2}”)){
返回c;
}否则{
返回null;
}
}));
您需要修复匹配的正则表达式以允许空字符串,或者提供一些其他修复以允许字段中的空字符串值。例如:

text.setTextFormatter (new TextFormatter<Integer>(c -> {
    String newText = c.getControlNewText();
    if ("".equals(newText) || newText.matches("^\\d{1,2}")) {
        return c ;
    } else {
        return null ;
    }
}));
text.setTextFormatter(新的TextFormatter(c->{
字符串newText=c.getControlNewText();
如果(“.equals(newText)| newText.matches(“^\\d{1,2}”)){
返回c;
}否则{
返回null;
}
}));

提供一个(最小的、完整的程序,可以复制和粘贴,以便在不做任何更改的情况下再现问题)。如果您使用的是JavaFX,请不要看基于awt的解决方案。欢迎使用堆栈溢出,MichalM。请不要仅仅为了满足添加太多代码时有更多描述的要求而添加重复文本。请考虑删除不必要的代码,只关注有问题的部分。