У меня проблема с очисткой TextField с помощью метода setText (""). Я пытаюсь проверить, можно ли указать значение c в плитке, а если нет, то я просто хочу установить пустое пространство. Это на самом деле работает, если я добавлю другое значение, так что я думаю, что это не проблема logi c? Это не работает, только если я хочу установить пустое пространство.
Класс плитки
Это класс, в котором возникает проблема.
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);
}
}