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

查看:184
本文介绍了数独板使用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...

我想建立一个空的数独板在Java中使用JPanels和JTextField的。我还需要创建在右手侧与另一个的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×9平方米,分为9个3×3格。请注意,每个小正方形掀起较重的边框比普通intersquare边界。每个方块是一个文本字段。编写程序,这样没有什么是文本字段。用户可以在文本字段中键入如果他们想,如果他们这样做,数字会显示出来。在侧面有四个按钮,让你去解决,获得了新的难题,得到了暗示,或重置的难题。

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循环来创建董事会。这是我的code ...

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

}

}

推荐答案

一对夫妇的事情,我看到:

A couple of things I see:

  • 我不认为你想9x9的的JP​​anel 译文]现在有,但9x9的的JTextField 秒。您可能需要3×3 的JP​​anel 取值这样就可以使每个部分的边界更大胆。它可能会更容易只是躺在这了明确,而不是试图做一个循环。

  • 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.

您会希望跟踪价值在每一行,列和每个3×3分小组。行和列是容易你有一个2D阵列中的方式。你可能会考虑持有的值在每个3×3区域阵列的另一个数组。这使得它更容易通过这些数值进行扫描,当你需要,而可能是有用的放置值较小的3×3 的JP​​anel S,如果你走这条路。

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天全站免登陆