0

The code works fine but problem is that i want 3 columns and three rows.. But output shows 6 columns instead of three rows and three columns

Here is a code in which problem appears.. Here is a main class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import games.board.Board;
import games.board.Cell;
import games.board.Mark;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;



public class TicTacToeGUIGame extends JFrame  {

/**
 * @param args
 */
private Board gb;
private int turn;
private void takeTurn(Cell c) {
    Mark curMark = (turn++ % 2 == 0)? Mark.NOUGHT
    : Mark.CROSS;
    gb.setCell(curMark, c.getRow(), c.getColumn());
    }
private TicTacToeGUIGame() {
    gb = new Board(3, 3, new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    Cell c = (Cell) ae.getSource();
    takeTurn(c);
    }
    });
    this.add(gb);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("TIC-TAC-TOE");
    this.setSize(300, 300);
    this.setVisible(true);
    }


public static void main(String[] args) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater( new Runnable () {
        public void run() { new TicTacToeGUIGame(); }
        });

}

}

Here is a board class:

package games.board;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Board extends JPanel {
private Cell[][] cells;
public Board(int rows, int columns) {
cells = new Cell[rows][columns];
for( int r = 0; r < cells[0].length; r++ ) {
for (int c = 0; c < cells[1].length; c++) {
cells[r][c] = new Cell(r,c);
}
}
}
public Board(int rows, int columns, ActionListener ah) {
    cells = new Cell[rows][columns];
    this.setLayout(new GridLayout());
    for( int r = 0; r < cells.length; r++ ) {
    for (int c = 0; c < cells[r].length; c++) {
    cells[r][c] = new Cell(r,c);
    this.add(cells[r][c]);
    cells[r][c].addActionListener(ah);
    }
    }
    }
public void setCell(Mark mark, int row, int column) throws
IllegalArgumentException {
if (cells[row][column].getContent() == Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new IllegalArgumentException("Player already there!");
}
public Cell getCell(int row, int column) {
return cells[row][column];
}
public String toString() {
StringBuilder str = new StringBuilder();
for( int r = 0; r < cells.length; r++ ) {
str.append("|"); 


for (int c = 0; c < cells[r].length; c++) {
switch(cells[r][c].getContent()) {
case NOUGHT:
    str.append("O");
    break;
    case CROSS:
    str.append("X");
    break;
    case YELLOW:
    str.append("Y");
    break;
    case RED:
    str.append("R");
    break;
    case BLUE:
    str.append("B");
    break;
    case GREEN:
    str.append("G");
    break;
    case MAGENTA:
    str.append("M");
    break;
    case ORANGE:
    str.append("M");
    break;
    default: //Empty
    str.append("");
    }
    str.append("|");
    }
    str.append("\n");
    }
    return str.toString();

}

}
4

3 回答 3

5

我想要 3 列和 3 行。但输出显示 6 列

您需要为 GridLayout 指定适当的参数:

this.setLayout(new GridLayout(0, 3));

这将告诉网格包含 3 列。行数取决于您添加的组件数。

于 2013-08-19T17:21:51.223 回答
1

我想要 3 列和 3 行...但输出显示 6 列

利用,this.setLayout(new GridLayout(nRows, nColumns));

更改nRowsnColumns相应地更改您想要的列数和行数,即,如果您想要一个20x20您将使用的游戏,this.setLayout(new GridLayout(20, 20));

于 2013-08-19T17:23:29.177 回答
1

更改您的董事会课程:

for( int r = 0; r < cells[0].length; r++ ) {
    for (int c = 0; c < cells[1].length; c++) {
      cells[r][c] = new Cell(r,c);
   }
 }

for( int r = 0; r < cells[0].length; r++ ) {
    for (int c = 0; c < cells[r].length; c++) {
      cells[r][c] = new Cell(r,c);
   }
 }
于 2013-08-19T17:21:37.270 回答