Java JButtons不是';t放置在与其JPanel相同的位置

Java JButtons不是';t放置在与其JPanel相同的位置,java,swing,jpanel,jbutton,Java,Swing,Jpanel,Jbutton,我现在正在尝试制作一个数独网格。所以我使用了两个数组(双维),一个包含所有jbutton,另一个包含9个jpanel。我在每个JPanel中放置了9个按钮,在每个JPanel中使用了(3,3)的GridLayout,所有JPanel都在一个更大的JPanel中,这个JPanel也使用了GridLayout。问题在于按钮位于窗口底部,因此并非所有按钮都显示出来。我为第一个JPanel(包含前9个按钮)设置了一个绿色背景,以查看它与其按钮的比较位置。我无法调整按钮和面板的大小 下面是我的班级代码:

我现在正在尝试制作一个数独网格。所以我使用了两个数组(双维),一个包含所有jbutton,另一个包含9个jpanel。我在每个JPanel中放置了9个按钮,在每个JPanel中使用了(3,3)的GridLayout,所有JPanel都在一个更大的JPanel中,这个JPanel也使用了GridLayout。问题在于按钮位于窗口底部,因此并非所有按钮都显示出来。我为第一个JPanel(包含前9个按钮)设置了一个绿色背景,以查看它与其按钮的比较位置。我无法调整按钮和面板的大小

下面是我的班级代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Sudoku extends JFrame implements ActionListener {


    public Sudoku(){
        //Initialise the window and show it
        this.setTitle("Sudoku Solver");
        this.setSize(1000, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(3,3));

        //Initialise an array of JPanel
        JPanel[][] jPanelTab = new JPanel[3][3];
        for(int i = 0; i < 3;i++){
            for(int j = 0; j < 3;j++){
                jPanelTab[i][j] = new JPanel();
                jPanelTab[i][j].setLayout(new GridLayout(3,3));
            }
        }


        //Initialise the JButtons
        JButton[][] boutonTab = new JButton[9][9];
        for(int i = 0; i < 9;i++){
            for(int j = 0; j < 9;j++){
                boutonTab[i][j] = new JButton();
                boutonTab[i][j].setText(""+i+"/"+j);
            }
        }


        for(int h = 0; h < 3; h++) {
            int n = 0;
            for (int i = 0; i < 3; i++) {
                int column = 0;
                for (int j = 0; j < 9; j++) {
                    jPanelTab[h][column].add(boutonTab[n][j]);
                    if (j == 2 || j == 5) {
                        column++;
                    }
                }
                n++;
            }
        }

        //Add the 9 JPanels to the JFrame
        for(int i = 0; i < 3;i++){
            for(int j = 0; j < 3;j++){
                if(j == 0 && i == 0)
                    pan.add(jPanelTab[i][j]).setBackground(Color.green);
                else
                    pan.add(jPanelTab[i][j]);

            }
        }
        this.setContentPane(pan);
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类数独扩展JFrame实现ActionListener{
公共数独{
//初始化窗口并显示它
这个.setTitle(“数独解算器”);
此.setSize(1000500);
此.setLocationRelativeTo(空);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pan=新的JPanel();
pan.setLayout(新网格布局(3,3));
//初始化JPanel的数组
JPanel[][]jPanelTab=新的JPanel[3][3];
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
jPanelTab[i][j]=新的JPanel();
[j].setLayout(新的GridLayout(3,3));
}
}
//初始化JButtons
JButton[]boutonTab=新JButton[9][9];
对于(int i=0;i<9;i++){
对于(int j=0;j<9;j++){
boutonTab[i][j]=新的JButton();
boutonTab[i][j].setText(“+i+”/“+j”);
}
}
对于(int h=0;h<3;h++){
int n=0;
对于(int i=0;i<3;i++){
int列=0;
对于(int j=0;j<9;j++){
jPanelTab[h][column]。添加(boutonTab[n][j]);
如果(j==2 | | j==5){
列++;
}
}
n++;
}
}
//将9个JPanel添加到JFrame
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
如果(j==0&&i==0)
pan.add(jPanelTab[i][j]).setBackground(颜色.绿色);
其他的
泛加(jPanelTab[i][j]);;
}
}
这个.setContentPane(pan);
此.setVisible(true);
}
@凌驾
已执行的公共无效操作(操作事件e){
}
}

谢谢你的回答

处理二维数组时,第一个索引是行,第二个索引是列

因此,使用更好的变量名可能有助于理解循环逻辑。在下面的代码中,我使用了包含“r”(行)和“c”(列)的变量

当在循环代码中使用上述变量名时,您现在可以在列之前的行中循环,因此外部循环将在变量名中包含“r”,而内部循环将在变量名中包含“c”

希望当您阅读代码时,它会变得更容易理解

对代码所做的主要更改是用于向9个面板中的每个面板添加单个按钮的逻辑

注意按钮的起始索引是面板索引的3倍。每添加到面板的第三个按钮,必须重置列和行索引

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sudoku extends JFrame {

    public Sudoku(){

        //Initialise the window and show it
        this.setTitle("Sudoku Solver");
        this.setSize(1000, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel board = new JPanel();
        board.setLayout(new GridLayout(3,3));

        //Initialise an array of JPanel
        JPanel[][] panels = new JPanel[3][3];
        for (int r = 0; r < 3; r++)
        {
            for (int c = 0; c < 3; c++)
            {
                JPanel panel = new JPanel( new GridLayout(3, 3) );
                panels[r][c] = panel;
                board.add( panel );
            }
        }

        //Initialise the JButtons
        JButton[][] buttons = new JButton[9][9];
        for(int r = 0; r < 9; r++)
        {
            for(int c = 0; c < 9; c++)
            {
                buttons[r][c] = new JButton(r + "/" + c);
            }
        }

        for (int panelR = 0; panelR < 3; panelR++)
        {
            for (int panelC = 0; panelC < 3; panelC++)
            {
                int buttonR = panelR * 3;
                int buttonC = panelC * 3;

                for (int i = 0; i < 9; i++)
                {
                    panels[panelR][panelC].add(buttons[buttonR][buttonC]);
                    buttonC++;

                    if ((i + 1) % 3 == 0)
                    {
                        buttonC = panelC * 3;
                        buttonR++;
                    }
                }
            }
        }

        this.setContentPane(board);
        this.setVisible(true);

    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> new Sudoku() );
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类数独扩展JFrame{
公共数独{
//初始化窗口并显示它
这个.setTitle(“数独解算器”);
此.setSize(1000500);
此.setLocationRelativeTo(空);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel board=新的JPanel();
board.setLayout(新网格布局(3,3));
//初始化JPanel的数组
JPanel[][]面板=新JPanel[3][3];
对于(int r=0;r<3;r++)
{
对于(int c=0;c<3;c++)
{
JPanel面板=新JPanel(新网格布局(3,3));
面板[r][c]=面板;
板。添加(面板);
}
}
//初始化JButtons
JButton[][]按钮=新JButton[9][9];
for(int r=0;r<9;r++)
{
对于(int c=0;c<9;c++)
{
按钮[r][c]=新的JButton(r+“/”+c);
}
}
用于(int panelR=0;panelR<3;panelR++)
{
对于(int-panelC=0;panelC<3;panelC++)
{
int buttonR=面板*3;
int buttonC=面板C*3;
对于(int i=0;i<9;i++)
{
面板[panelR][panelC]。添加(按钮[buttonR][buttonC]);
按钮nc++;
如果((i+1)%3==0)
{
按钮C=面板C*3;
按钮++;
}
}
}
}
此.setContentPane(板);
此.setVisible(true);
}
公共静态void main(字符串[]args)引发异常
{
invokeLater(()->new Sudoku());
}
}