java - 需要帮助更改按钮背景

标签 java swing loops drjava

我正在编写一个很像连接四的程序,并且需要在拼写某个单词(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);
          }
        }
      }
    }
  }

最佳答案

不要将String==进行比较。使用 equalsisEmpty 代替:

if(gameBoard[i][c].getText().isEmpty() && playMade == false)
...
if(player.equals("T"))
...
if(wordCheck.equals("OTTO") || wordCheck.equals("TOOT"))

仅当两个 String 引用指向完全相同的 String 对象时,== 运算符才会返回 true。

如果两个 String 对象具有相同的内容,

equals 返回 true。它们可能是也可能不是同一个对象。

关于java - 需要帮助更改按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15995277/

相关文章:

java - 从文本字段输入中过滤 JList

Java:仅当选择右侧菜单项时才继续迭代数组列表

java - 如何以编程方式更改 ActionBar 中的单个操作项文本颜色?

java - 我无法在 Android Studio 中 toast 消息

java - 如何使用不同包中的方法?

Java-执行流程

java - 使用 JSObject 从 Java 调用 Javascript 函数

java - 如何在Java中设置GridBagLayout的列宽

javascript - 动态打开的下拉菜单

php - 如何将多维数组中的大量内容存储到mysql数据库中?