Joanmi:
私はJButtonがが、(0,0)であること、の順序でのJPanelに追加されていないことを見たとき、それはにSystem.outを使用されるように、すべてが同じ位置にない大丈夫だった、Javaを使用してチェスのゲームを作成しています.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));
}
}
}
}
マルコ・R:
どうやらあなたはあなたのdouble配列反復処理されているJButton
列の上に、その後の行の上にSを(その後、左から右へ上から下へ、)に追加するときchessBoard
:
chessBoard.add(t.getFitxaGrafic(i, j));
あなただけ交換する必要があるので、それは、反対(列その後、行)であるべきときi
とj
:
chessBoard.add(t.getFitxaGrafic(j, i));
お役に立てれば。