Need help moving the buttons using JPanel GridLayout in Java? [duplicate]

时间秒杀一切 提交于 2021-01-29 09:52:47

问题


I want to move my buttons for my GUI game which is a 9x9 grid so the buttons appear as the image below. I have tried hard but can not figure it out.

The current issue I am having.

What I want it to look like.

public FutoshikiGUI()
{        
    grid  = new JPanel(new GridLayout(0,9));
    cells = new PanelCell[5+1][5+1];     
    
    for (int row=1; row<=5; row++) 
    {
        for (int col=1; col<=5; col++)
        {
            // number input
            cells[row][col] = new PanelCell(this,row,col);
            grid.add(cells[row][col]); 
            if(col < 5)
            {
               // relation input 
              JButton button = new JButton("");
              button.setBackground(Color.white);
              button.setOpaque(true);
              button.setBorder(new LineBorder(Color.BLACK));
              grid.add(button);
            }      
        }
    }
   
    for (int row=1; row<=4; row++) 
    {
        for (int col=1; col<=5; col++)
        {
            // relation input
            JButton btn = new JButton("");
            btn.setBackground(Color.white);
            btn.setOpaque(true);
            btn.setBorder(new LineBorder(Color.BLACK));
            grid.add(btn);
            if(col < 5)
            { 
              // blank input 
              JButton button = new JButton("");
              button.setBackground(Color.darkGray);
              button.setOpaque(true);
              button.setBorder(new LineBorder(Color.BLACK));
              grid.add(button);
            }      
        }
    }
    setLayout(new BorderLayout());
    add(grid, BorderLayout.NORTH);
}

回答1:


I guess your PanelCell objects are the "blue" cells, while the "white" cells are buttons with the white background and the gray cells are buttons with the darkGray background color.

First of all (to make your code easier to maintain) I suggest you create a simple "factory method" to create your buttons:

public static JButton createButton(Color background) {
    JButton button = new JButton("");
    button.setBackground(background);
    button.setOpaque(true);
    button.setBorder(new LineBorder(Color.BLACK));
    return button;
}

This method contains redundant code that you where repeating three times (but you only change the background color). So it is easier to put it in a method and pass the background color as a parameter.

Now your code would look like this (I shortened the formatting):

for (int row=1; row<=5; row++){
    for (int col=1; col<=5; col++){
        cells[row][col] = new PanelCell(this,row,col);
        grid.add(cells[row][col]); 
        if(col < 5){
          grid.add(createButton(Color.white));  // I just replaced your code with the method call to create the button instance
        }      
    }
}

for (int row=1; row<=4; row++) {
    for (int col=1; col<=5; col++){
        grid.add(createButton(Color.white));
        if(col < 5){ 
          grid.add(createButton(Color.darkGray));
        }      
    }
}

Now you have an array of PanelCells that can contain 6 rows and 6 columns. Your for loops however start at index 1, which means that actually your array will have no cells in the first row.
Always start for loops with index 0 and let them finish at the "length" of the array - then those errors don't happen anymore.

cells = new PanelCell[5][5];  // in your image I only see 5 "blue" cells in a row and 5 columns in each row

for(int row=0; row<cells.length; row++){   // you already initialised the array to have 5 rows
   for(int col=0; col<cells[row].length; col++){  // now you can be sure that all cells are iterated over once (each row and each column)
     // ...
   }
 }

So you iterate over 5 rows and 5 columns each time. You add a new PanelCell to your grid and (for the first 4 columns) you add a white button.
And here is the problem
After creating your first 25 cells, you iterate again over 4 rows and 5 columns and add white and gray buttons. So in the end your grid contains 5 rows with blue and white buttons and 4 rows with gray and white buttons.

So you have to think of a way to fill your grid in the right order. Here is an example on how you can do that without maintaining a cell array :

for(row=0; row<9; row++){     // your grid should contain 9 rows 
   for(col=0; col<9; col++){  // and 9 columns in each row
      if(row%2==0){           // your blue buttons are only in "even" rows (0,2,4,6,8)
         if(col%2==0){        // in even rows your blue buttons are only in even columns
            grid.add(new PanelCell(...));  // create your PanelCell
         }else{
            grid.add(createButton(Color.white)); // in "uneven" columns you add a white button)
         }
      }else{                  // in uneven rows you have white buttons in even columns and gray buttons in uneven columns
          if(col%2==0){        
            grid.add(createButton(Color.white));  // create a white button
         }else{
            grid.add(createButton(Color.darkGray)); // create a gray button
         }
      }
    }
 }

And now this should work. The code above is only an example and should fill your "grid" correctly, however it does not take into account how to handle your "cell" array or create the PanelCell instances.



来源:https://stackoverflow.com/questions/65563415/need-help-moving-the-buttons-using-jpanel-gridlayout-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!