Java Tic Tac Toe GUI

时间:2013-08-19 17:17:18

标签: java swing tic-tac-toe

代码工作正常,但问题是我需要3列和3行..但输出显示6列而不是3行和3列

这是一个出现问题的代码。 这是一个主要类:

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

}

}

这是一个董事会类:

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

}

}

3 个答案:

答案 0 :(得分:5)

  

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

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

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

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

答案 1 :(得分: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);
   }
 }

答案 2 :(得分:1)

  

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

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

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