繁体   English   中英

为什么我的代码中某些文本字段的背景颜色没有改变?

[英]Why don't background colour of some of textfields in my code change?

我正在用 GUI 做数独项目。 我有一个二维数组来保存 9x9 网格中的数字。 我想更改保存非 0 数组值的文本字段的背景颜色。 这是我操作文本字段和 GUI 图片的代码的一部分。 例如在第一个 3x3 网格中,5,4 和 1 位数字背景为灰色,但 9 不是。 问题是什么? 请帮忙。 谢谢你。

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

在此处输入图片说明

查看您的代码,我假设您正在使用javafx.scene.control.TextField的 TextField 。 如果您查看图像,则附加了未变灰的行和列,而它们应该是第 3 行和第 6 行/列(索引 2 和 5)。 背景样式被设置的边框宽度覆盖。

尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM