Java 为什么不';我的代码中某些文本字段的背景颜色是否发生变化?

Java 为什么不';我的代码中某些文本字段的背景颜色是否发生变化?,java,javafx,textfield,Java,Javafx,Textfield,我正在用GUI做数独项目。我有一个二维数组来保存9x9网格中的数字。我想更改包含非0数组值的文本字段的背景色。这是我的代码的一部分,我在这里操作文本字段和GUI的图片。例如,在第一个3x3网格中,5、4和1位背景灰,但9不是。有什么问题?请帮忙。多谢各位 public class Controller { private Board board; private TextField[] tfs; @FXML GridPane gridPane; @FXML Bu

我正在用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();
    }
}
公共类控制器{
私人董事会;
私有文本字段[]tfs;
@FXML网格窗格;
@FXML按钮开始按钮;
@FXML按钮停止按钮;
@FXML标签;
private final IntegerProperty timeSeconds=新的SimpleIntegerProperty(0);
私人时间表;
public void initialize()引发IOException{
线路板=新线路板();
tfs=新文本字段[81];
int c=0;
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
tfs[c]=新文本字段();
tfs[c].setFont(新字体(24));
tfs[c].setPrefWidth(50);
tfs[c].setPrefHeight(50);
tfs[c].setText(board.getValue(i,j)+”);
tfs[c].setDisable(真);
if(board.getValue(i,j)!=0)
tfs[c].setStyle(“-fx背景色:#ebe6e6”);
如果(i==2 | | i==5)
tfs[c].setStyle(“-fx边框宽度:1;3;”);
如果(j==2 | | j==5)
tfs[c].setStyle(“-fx边框宽度:1 3 1;”);
如果((i==2 | | i==5)和&(j==2 | | j==5))
tfs[c].setStyle(“-fx边框宽度:1 3;”);
添加(tfs[c],j,i);
int finalI=i;
int finalJ=j;
tfs[c].textProperty().addListener((bean\u p,old\u p,new\u p)->{
if(newValue.matches(“[0-9]”){
inta=Integer.parseInt(newValue);
布尔值b=线路板设置值(finalI,finalJ,a);
如果(!b)
showAlert(“错误”、“无效移动”)、Alert.AlertType.Error;
}否则
showAlert(“错误”、“无效输入”)、Alert.AlertType.Error);
if(board.isGameFinished()和&board.isBoardCorrect())
showAlert(“已解决”,“祝贺你,你解决了数独问题。”,Alert.AlertType.INFORMATION);
});
C++;
}
}
}
public void onPressedStartButton(){
stopButton.setDisable(假);
activateTextFields();
startButton.setDisable(true);//防止多次启动
时间线=新的时间线(新的关键帧(Duration.seconds(1),evt->updateTime());
timeline.setCycleCount(Animation.unfinite);//反复重复
if(timeSeconds.get()!=0){
}
timeline.play();
}
私有void updateTime(){
int seconds=timeSeconds.get();
时间秒设置(秒+1);
timeLabel.setText(“经过的时间:+timeSeconds.get()+sn”);
}
按停止按钮()时公共无效{
如果(时间线!=null){
timeline.stop();
}
被动文本字段();
startButton.setDisable(假);
stopButton.setDisable(真);
}
公共无效onPressedResetButton(){
timeSeconds.set(0);
timeLabel.setText(“经过的时间:0 sn”);
}
public void activateTextFields(){
对于(int i=0;i<81;i++){
if(tfs[i].getText().equals(“0”))
tfs[i].setDisable(false);
}
}
public void passiveateTextFields(){
对于(int i=0;i<81;i++){
if(tfs[i].getText().equals(“0”))
tfs[i].setDisable(真);
}
}
public void showAlert(字符串标题、字符串消息、Alert.AlertType AlertType){
警报警报=新警报(警报类型);
警报。设置标题(标题);
alert.setHeaderText(消息);
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);

请提供详细信息。哪些文本字段不变?有没有一种模式是它们不会改变的?并发布完整的代码我添加了完整的代码。我提到哪个文本字段不改变。我想将不包含0的文本字段绘制为灰色背景色。