在 Java 中使用 JPanels 的数独板 [英] Sudoku Board Using JPanels in Java

查看:19
本文介绍了在 Java 中使用 JPanels 的数独板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有几篇关于数独相关问题的帖子,但我不确定其中是否有我想要的内容......

I know there have been several posts already about Sudoku-related questions, but I am not sure any of them have exactly what I am looking for...

我正在尝试使用 JPanels 和 JTextfields 在 Java 中构建一个空的数独板.我还需要使用另一个 JPanel 在右侧创建一个菜单.

I am trying to build an empty Sudoku board in Java using JPanels and JTextfields. I also need to create a menu on the right hand side with another JPanel.

棋盘本身是一个 9 x 9 的正方形,分为 9 个 3x3 的正方形.请注意,每个较小的方块都由比常规方块间边框更重的边框分隔.每个方块都是一个文本字段.编写程序,使文本字段中没有任何内容.用户可以根据需要在文本字段中输入,如果输入,则会显示数字.侧面有四个按钮,可让您解决、获得新谜题、获得提示或重置谜题.

The board itself is a 9 x 9 square, divided into 9 3x3 squares. Note that each smaller square is set off by a heavier border than the regular intersquare borders. Each square is a text field. Write the program so that nothing is in a text field. Users can type in the text field if they want, and if they do, numbers will show up. On the side there are four buttons that allow you to solve, get a new puzzle, get a hint, or reset puzzle.

任何想法都会很棒.我无法理解如何嵌套 for 循环来创建板.这是我的代码...

Any ideas would be great. I am having trouble understanding how to nest the for loops to create the board. Here is my code...

    import javax.swing.*;
    import javax.swing.border.Border;
    import java.awt.*;

    public class ArrayTest extends JFrame {

        public ArrayTest() {

    JPanel board = new JPanel(new GridLayout(9, 9));
    add(board);

    JPanel[][] squares = new JPanel[9][9];

    Border border = BorderFactory.createLineBorder(Color.BLACK);


    for (int row = 1; row < 9; row++) {

        for (int col = 1; col < 9; col++) {
            squares[row][col] = new JPanel();
            board.add(squares[row][col]);

        }

    }



    JPanel menu = new JPanel();
    menu.add(new JButton("Reset"));
    menu.add(new JButton("Hint"));
    menu.add(new JButton("Solve"));
    menu.add(new JButton("New Puzzle"));



    add(menu);

}
public static void main(String[] args) {
    // TODO Auto-generated method stub

    /** Create a frame and set its properties*/
    JFrame frame = new ArrayTest();
    frame.setTitle("Sudoku");
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null); //Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

}

推荐答案

我看到的一些事情:

  • 我认为您不需要像现在这样的 9x9 JPanel,而是 9x9 JTextField.您可能需要 3x3 JPanel 以便您可以使每个部分的边框更粗.与尝试在循环中进行相比,明确地列出这些内容可能更容易.

  • I don't think you want 9x9 JPanels as you have now, but 9x9 JTextFields. You may want 3x3 JPanels so you can make the borders of each section bolder. It might be easier to just lay these out explicitly instead of trying to do it in a loop.

你的循环计数器(和数组索引)应该从 0 开始,而不是 1.按照你现在的方式,循环只会执行 8 次.

Your loop counters (and array indices) should start at 0, not 1. The way you have it now, the loops will only execute 8 times.

您将要跟踪每行、每列和每个 3x3 子组中的值.行和列很简单,就像在二维数组中一样.您可能会考虑另一个数组数组,用于保存每个 3x3 区域中的值.这使您可以在需要时更轻松地浏览这些值,并且如果您采用该路线,则可能有助于将值放置在较小的 3x3 JPanel 中.

You are going to want to keep track of values in each row, column, and in each 3x3 sub-group. Rows and columns are easy the way you have it in a 2D array. You might consider another array of arrays that hold the values in each 3x3 area. This makes it easier to scan through these values when you need to, and might be useful for placing values in the smaller 3x3 JPanels, if you go that route.

这篇关于在 Java 中使用 JPanels 的数独板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆