gridlayout用法 java,需要使用Java中的JPanel GridLayout移动按钮的帮助吗?

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.

13nEg.png

What I want it to look like.

GZSuW.png

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

}

解决方案

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

for(int col=0; col

// ...

}

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值