使用Grid-Layout和JLabels

时间:2017-03-26 18:01:26

标签: java swing jpanel jbutton jlabel

我正在为我的Sudoku Solver开发GUI,使用Grid-Layout和JLabel来显示电路板。我能够成功地显示初始板,但是当我点击我的" Solve"按钮,我不确定如何使用解决方案重新绘制电路板。

这是我在构造函数中调用的draw方法,并成功地将初始值绘制到主板上。

  public void drawGame(){
    String s = "";
    for(int z = 0; z < ninePuzzle.length; z++){
        int row, col;
        if((z%dimension) > 0){
            row = z / dimension;
            col = z % dimension;
            board[row][col] = ninePuzzle[z];
        }
        else{
            row = z / dimension;
            col = 0;
            board[row][col] = ninePuzzle[z];
        }
    }

    for(int i = 0; i < board.length; i++){

        for(int j = 0; j < board[i].length; j++){
            if(board[i][j] == -1){

                gamePanel.add(new JLabel(" "));
            }
            else{
                s = "";
                s = "" + board[i][j];
                gamePanel.add(new JLabel(s));
            }
        }
    }
}

当单击Solve JButton时我会重新使用&#34;重新使用&#34;这部分drawGame()方法可行。我在这里做错了什么?

@Override
public void actionPerformed(ActionEvent e) {
    if("solve".equals(e.getActionCommand())) {
        if (solvePuzzle(board)) {
            String s = "";
            for (int i = 0; i < board.length; i++) {

                for (int j = 0; j < board[i].length; j++) {
                    if (board[i][j] == -1) {

                        gamePanel.add(new JLabel(" "));
                    } else {
                        s = "";
                        s = "" + board[i][j];
                        gamePanel.add(new JLabel(s));
                    }
                }
            }
        } 
        else {

        }
    }       
}

注意:我暂时将其保留为空,此特定数独数组[] []的初始值始终具有正确的soln。

2 个答案:

答案 0 :(得分:0)

您的问题是,当您单击按钮时添加新标签,您应该更好地定义全局9x9 JLabel矩阵,然后将其传递给drawGame并将它们添加到面板。
现在点击按钮,你只想使用nameOfMatrix [i] [j] .setText更新它们的值(&#34;你的文字&#34;);

答案 1 :(得分:0)

更新代码

在drawGame方法中使用了JLabel [] []:

public void drawGame(){
    String s = "";
    for(int z = 0; z < ninePuzzle.length; z++){
        int row, col;
        if((z%dimension) > 0){
            row = z / dimension;
            col = z % dimension;
            board[row][col] = ninePuzzle[z];
        }
        else{
            row = z / dimension;
            col = 0;
            board[row][col] = ninePuzzle[z];
        }
    }

    for(int i = 0; i < board.length; i++){

        for(int j = 0; j < board[i].length; j++){
            if(board[i][j] == -1){
                game[i][j] = new JLabel(" ");
                gamePanel.add(game[i][j]);
            }
            else{
                s = "";
                s = "" + board[i][j];
                game[i][j] = new JLabel(s);
                gamePanel.add(game[i][j]);
            }
        }
    }
}

动作执行代码,用于&#34;解决&#34;单击按钮。

@Override
public void actionPerformed(ActionEvent e) {
    if("solve".equals(e.getActionCommand())) {
        if (solvePuzzle(board)) {

            String s = "";
            for (int i = 0; i < board.length; i++) {

                for (int j = 0; j < board[i].length; j++) {
                    s = "" + board[i][j];

                    game[i][j].setText(s);

                }
            }
        }

    }
    if("clear".equals(e.getActionCommand())){
        for(int z = 0; z < ninePuzzle.length; z++){
            int row, col;
            if((z%dimension) > 0){
                row = z / dimension;
                col = z % dimension;
                board[row][col] = ninePuzzle[z];
            }
            else{
                row = z / dimension;
                col = 0;
                board[row][col] = ninePuzzle[z];
            }
        }
        String s = "";
        for(int i = 0; i < board.length; i++){

            for(int j = 0; j < board[i].length; j++){
                if(board[i][j] == -1){
                    game[i][j].setText(" ");

                }
                else{
                    s = "";
                    s = "" + board[i][j];
                    game[i][j].setText(s);

                }
            }
        }
    }
}