[英]JButtons aren't placed at the same location than their JPanel
我现在正在尝试制作一个数独网格。 所以我使用了 2 个数组(双维),一个包含所有 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) {
}
}
感谢您的回答!
处理二维数组时,第一个索引是行,第二个索引是列。
所以使用更好的变量名可能有助于理解循环逻辑。 在下面的代码中,我使用了包含“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() );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.