簡體   English   中英

需要幫助更改按鈕背景

[英]Need help changing a button background

我正在編寫一個程序,就像連接四個,並且在拼寫某個單詞(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"))

== -運算符將只返回true,如果兩個String -references在完全相同的點String -object。

如果兩個String對象具有相同的內容,則equals返回true。 它們可能是同一對象,也可能不是同一對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM