是否需要使用Java中的JPanel GridLayout移动按钮的帮助? [英] Need help moving the buttons using JPanel GridLayout in Java?

查看:82
本文介绍了是否需要使用Java中的JPanel GridLayout移动按钮的帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动我的9x9网格GUI游戏的按钮,因此按钮如下图所示.我已经尽力了,但无法弄清楚.

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.

我当前遇到的问题.

我希望它看起来像什么.

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

推荐答案

我猜您的PanelCell对象是蓝色"对象,单元格,而白色"单元格单元格是具有white背景的按钮,灰色单元格是具有darkGray背景颜色的按钮.

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

此方法包含多余的代码,您可以在其中重复3次(但您只能更改背景颜色).因此,将其放入方法中并传递背景色作为参数会更容易.

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

现在您有一个PanelCell数组,可以包含6行和6列.但是,您的for循环从索引1开始,这意味着实际上您的数组在第一行中将没有任何单元格.
总是从索引0开始for循环,并让它们以"length"结束.阵列-那么这些错误就不会再发生了.

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)
     // ...
   }
 }

因此,您每次迭代5行和5列.您将一个新的PanelCell添加到网格中,并且(对于前4列)添加了一个白色按钮.
这是问题所在
创建前25个单元格后,您将在4行和5列中再次进行迭代,并添加白色和灰色按钮.因此最后,您的网格包含5行带有蓝色和白色按钮的行和4行带有灰色和白色按钮的行.

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
         }
      }
    }
 }

现在应该可以了.上面的代码仅是示例,应该填充您的网格".正确地,但是它没有考虑如何处理您的单元".数组或创建PanelCell实例.

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.

这篇关于是否需要使用Java中的JPanel GridLayout移动按钮的帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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