我正在编写一个很像连接四个的程序,并且需要在拼写某个单词(TOOT 或 OTTO)时更改 4 个按钮的背景。目前我的代码没有改变背景。任何帮助都会很棒。
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Integer;
import javax.swing.AbstractButton;
import java.awt.Color;
import javax.swing.UIManager;
public class TootAndOtto extends JFrame implements ActionListener {
private int rows;
private int columns;
private JButton[][] gameBoard;
private String player = "T";
public TootAndOtto() {
rows = 6;
columns = 6;
runGame();
this.setVisible(true);
}
/**
* Creates the board, as well as the grid and buttons that are placed on the board
*/
public void runGame() {
this.setSize(600,600);
this.setLocation(300,100);
JPanel board = new JPanel(new GridLayout(rows, columns));
board.setVisible(true);
this.getContentPane().add(board);
gameBoard = new JButton[rows][columns];
for (int i = 0; i < rows; i++) {
for (int i2 = 0; i2 < columns; i2++) {
gameBoard[i][i2] = new JButton();
gameBoard[i][i2].addActionListener(this);
board.add(gameBoard[i][i2]);
}
}
}
/**
* Runs the program anytime a button is clicked. This takes care of finding what button was clicked, placing a letter on a button,
* and determining if a winner exists
* @param e - the information of a button once is is clicked
*/
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
int r = findRow(b);
int c = findColumn(r, b);
placeLabel(c);
checkVertical(r, c);
}
/**
* Finds the row of the button that is clicked
* @param b - The address of the button that is clicked
* @return An int that is the number of the row that but clicked button is in.
*/
public int findRow(JButton b) {
int r = -1;
for (int i = 0; i < rows; i++) {
for (int i2 = 0; i2 < columns; i2++) {
if (b.equals(gameBoard[i][i2])){
r = i;
}
}
}
return r;
}
/**
* Finds the column of the button that is clicked
* @param b - The address of the button that is clicked
* @return An int that is the number of the column that but clicked button is in.
*/
public int findColumn(int r, JButton b) {
int c = -1;
for (int z = 0; z < columns; z++) {
if (b.equals(gameBoard[r][z])) {
c = z;
}
}
return c;
}
/**
* places alternation player letters of T and O into the board if there is an empty space.
* @param c - The column that the clicked button is in
*/
public void placeLabel(int c) {
boolean playMade = false;
for(int i = rows-1; i >= 0; i--) {
if(gameBoard[i][c].getText() == "" && playMade == false) {
gameBoard[i][c].setText(player);
playMade = true;
if (player == "T")
player = "O";
else
player = "T";
}
}
}
/**
* Checks for a vertical win in the column that the letter is placed in
* @param r - the row that the clicked button is in
* @param c - The column that the clicked button is in
*/
public void checkVertical(int r, int c) {
if(r < (rows - 3)) {
for (int i = r; i < rows; i++) {
String wordCheck = "";
if ((i + 3) < rows) {
wordCheck = gameBoard[i][c].getText() + gameBoard[i+1][c].getText() + gameBoard[i+2][c].getText() + gameBoard[i+3][c].getText();
if (wordCheck == "OTTO" || wordCheck == "TOOT") {
gameBoard[i][c].setBackground(Color.yellow);
gameBoard[i+1][c].setBackground(Color.yellow);
gameBoard[i+2][c].setBackground(Color.yellow);
gameBoard[i+3][c].setBackground(Color.yellow);
}
}
}
}
}