gpt4 book ai didi

Java 井字游戏 GUI

转载 作者:行者123 更新时间:2023-12-01 07:30:26 24 4
gpt4 key购买 nike

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

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

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

}

}

最佳答案

i want 3 columns and three rows.. But output shows 6 columns

您需要为 GridLayout 指定正确的参数:

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

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

关于Java 井字游戏 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18319436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com