[英]Why don't background colour of some of textfields in my code change?
I am doing Sudoku project with a GUI.我正在用 GUI 做数独项目。 I have a two dimensional array to hold digits in 9x9 grids.我有一个二维数组来保存 9x9 网格中的数字。 I want to change background color of textfields that holds the value of the array that are not 0's.我想更改保存非 0 数组值的文本字段的背景颜色。 Here is a part of my code where I manipulate the textfields and a picture of the GUI.这是我操作文本字段和 GUI 图片的代码的一部分。 For example in first 3x3 grid, 5,4 and 1 digits background grey but 9 is not.例如在第一个 3x3 网格中,5,4 和 1 位数字背景为灰色,但 9 不是。 What is the problem?问题是什么? Please help.请帮忙。 Thank you.谢谢你。
public class Controller {
private Board board;
private TextField[] tfs;
@FXML GridPane gridPane;
@FXML Button startButton;
@FXML Button stopButton;
@FXML Label timeLabel;
private final IntegerProperty timeSeconds = new SimpleIntegerProperty(0);
private Timeline timeline;
public void initialize() throws IOException {
board = new Board();
tfs = new TextField[81];
int c = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
tfs[c] = new TextField();
tfs[c].setFont(new Font(24));
tfs[c].setPrefWidth(50);
tfs[c].setPrefHeight(50);
tfs[c].setText(board.getValue(i, j) + "");
tfs[c].setDisable(true);
if (board.getValue(i, j) != 0)
tfs[c].setStyle("-fx-background-color: #ebe6e6");
if (i == 2 || i == 5)
tfs[c].setStyle("-fx-border-width: 1 1 3 1;");
if (j == 2 || j == 5)
tfs[c].setStyle("-fx-border-width: 1 3 1 1;");
if ((i == 2 || i == 5) && (j == 2 || j == 5))
tfs[c].setStyle("-fx-border-width: 1 3 3 1;");
gridPane.add(tfs[c], j, i);
int finalI = i;
int finalJ = j;
tfs[c].textProperty().addListener((bean_p, old_p, new_p) -> {
if (newValue.matches("[0-9]")) {
int a = Integer.parseInt(newValue);
boolean b = board.setValue(finalI, finalJ, a);
if (!b)
showAlert("Error", "Not a valid move.", Alert.AlertType.ERROR);
} else
showAlert("Error", "Invalid input.", Alert.AlertType.ERROR);
if (board.isGameFinished() && board.isBoardCorrect())
showAlert("Solved", "Congratulations. You solved the sudoku.", Alert.AlertType.INFORMATION);
});
c++;
}
}
}
public void onPressedStartButton() {
stopButton.setDisable(false);
activateTextFields();
startButton.setDisable(true); // prevent starting multiple times
timeline = new Timeline(new KeyFrame(Duration.seconds(1), evt -> updateTime()));
timeline.setCycleCount(Animation.INDEFINITE); // repeat over and over again
if (timeSeconds.get() != 0) {
}
timeline.play();
}
private void updateTime() {
int seconds = timeSeconds.get();
timeSeconds.set(seconds + 1);
timeLabel.setText("Elapsed Time: " + timeSeconds.get() + " sn");
}
public void onPressedStopButton() {
if (timeline != null) {
timeline.stop();
}
passivateTextFields();
startButton.setDisable(false);
stopButton.setDisable(true);
}
public void onPressedResetButton() {
timeSeconds.set(0);
timeLabel.setText("Elapsed Time: 0 sn");
}
public void activateTextFields() {
for (int i = 0; i < 81; i++) {
if (tfs[i].getText().equals("0"))
tfs[i].setDisable(false);
}
}
public void passivateTextFields() {
for (int i = 0; i < 81; i++) {
if (tfs[i].getText().equals("0"))
tfs[i].setDisable(true);
}
}
public void showAlert(String title, String message, Alert.AlertType alertType) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(message);
alert.show();
}
}
Looking at your code I am assuming that you're using the TextField from javafx.scene.control.TextField
.查看您的代码,我假设您正在使用javafx.scene.control.TextField
的 TextField 。 If you look at the image you attached the rows and columns that are not greyed out when they should are the 3rd and 6th row/column (index 2 and 5).如果您查看图像,则附加了未变灰的行和列,而它们应该是第 3 行和第 6 行/列(索引 2 和 5)。 The background style is overridden by the set border-widths.背景样式被设置的边框宽度覆盖。
Try this:尝试这个:
String style = "";
if (board.getValue(i, j) != 0) {
style += "-fx-background-color: #ebe6e6;";
}
if ((i == 2 || i == 5) && (j == 2 || j == 5)) {
style += "-fx-border-width: 1 3 3 1;";
} else if (i == 2 || i == 5) {
style += "-fx-border-width: 1 3 1 1;";
} else if (j == 2 || j == 5) {
style += "-fx-border-width: 1 3 1 1;";
}
tfs[c].setStyle(style);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.