java按钮纵向排列,java-在JPanel中对JButton进行排序

我正在使用Java创建国际象棋游戏,当我看到未按顺序将JButton添加到JPanel时,一切都很好,即(0,0)与使用System.out的位置不同.Println谁知道我该怎么解决?

private void configurarCaselles() {

Insets marge = new Insets(0, 0, 0, 0);

for (int i = 0; i < t.getTaulerCaselles().length; i++) {

for (int j = 0; j < t.getTaulerCaselles()[0].length; j++) {

Casella_Grafic c = t.getFitxaGrafic(i, j);

c.setMargin(marge);

ImageIcon icon = new ImageIcon(new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));

c.setIcon(icon);

if ((i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0)) {

c.setBackground(Color.WHITE);

} else {

c.setBackground(Color.BLACK);

}

}

}

chessBoard.add(new JLabel(""));

for (int i = 0; i < 8; i++) {

chessBoard.add(new JLabel(COLS.substring(i, i + 1), SwingConstants.CENTER));

}

for (int j = 0; j < 8; j++) {

for (int i = 0; i < 8; i++) {

switch (i) {

case 0:

chessBoard.add(new JLabel("" + (9 - (j + 1)), SwingConstants.CENTER));

default:

chessBoard.add(t.getFitxaGrafic(i, j));

}

}

}

}

UOuMR.png

jLNEL.png

解决方法:

显然,当您将JButtons的双重数组遍历行,然后遍历列(从上到下,然后从左到右)时,将其迭代:

chessBoard.add(t.getFitxaGrafic(i, j));

当它应该相反时(列然后是行),因此您只需要交换i和j:

chessBoard.add(t.getFitxaGrafic(j, i));

希望这可以帮助.

标签:grid-layout,swing,java

来源: https://codeday.me/bug/20191024/1922357.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值