单击另一个JButton时更改JButton的颜色

时间:2018-02-20 12:38:17

标签: java swing awt jbutton background-color

我的项目有问题。我正在研究一个非常简单的“棋盘游戏”。当我单击其中一个按钮时,我需要使用相邻按钮来更改颜色。我不知道如何引用其他按钮。这是Button对象:

package city;

import java.awt.Color;
import javax.swing.JButton;

public class Button extends JButton {

public String color;
public Color lightBlue = Color.CYAN;
public Color lightRed = Color.PINK;
public Color darkBlue = Color.BLUE;
public Color darkRed = Color.RED;
public Color gray = Color.GRAY;
public Color defaultColor = Color.WHITE;

public int x;
public int y;

public void canMoveLightRed() {

  }
}

以下是主要代码:

package city;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JFrame;

public class GameBoard extends JFrame implements ActionListener {

// The overall box count in chess board
public static final int squareCount = 64;



public GameBoard(String title) {

    Color defaultColor = Color.WHITE;
    Color darkRedColor = Color.RED;
    Color darkBlueColor = Color.BLUE;
    Color lightBlueColor = Color.CYAN;
    Color lightRedColor = Color.PINK;

    Button squareButton = null;

    for (int i = 1; i <= squareCount; i++) {
        squareButton = new Button();
        squareButton.setBackground(defaultColor);
        add(squareButton);
        squareButton.addActionListener(this);

        //Sets the tooltip text of the coordinates of the square
        if (i == 1) {
            squareButton.setToolTipText("A1");
            squareButton.setActionCommand("A1");
        }

        if (i == 2) {
            squareButton.setToolTipText("A2");
        }

        if (i == 3) {
            squareButton.setToolTipText("A3");
        }

        if (i == 4) {
            squareButton.setToolTipText("A4");
        }

        if (i == 5) {
            squareButton.setToolTipText("A5");
        }

        if (i == 6) {
            squareButton.setToolTipText("A6");
        }

        if (i == 7) {
            squareButton.setToolTipText("A7");
        }

        if (i == 8) {
            squareButton.setToolTipText("A8");
        }

        //Sets the text for the Gate squares
        if (i == 4) {
            squareButton.setText("Red's");
        }

        if (i == 60) {
            squareButton.setText("Blue's");
        }

        if (i == 5 || i == 61) {
            squareButton.setText("Gate");
        }

        //Sets the color for Red Soldiers
        if (i == 1 || i == 2 || i == 3 || i == 5 || i == 6 || i == 7 || i == 8) {
            squareButton.setBackground(lightRedColor);
            squareButton.setActionCommand("Red Soldier");
        }

        //Sets the color for the Red Ram
        if (i == 4 || i == 12) {
            squareButton.setBackground(darkRedColor);
            squareButton.setActionCommand("Red Ram");
        }

        //Sets the color for Blue Soldiers
        if (i == 57 || i == 58 || i == 59 || i == 60 || i == 62 || i == 63 || i == 64) {
            squareButton.setBackground(lightBlueColor);
            squareButton.setActionCommand("Blue Soldier");
        }

        //Sets the color for the Blue Ram
        if (i == 53 || i == 61) {
            squareButton.setBackground(darkBlueColor);
            squareButton.setActionCommand("Blue Ram");




        }
    }

    this.setTitle(title); // Setting the title of board
    this.setLayout(new GridLayout(8, 18)); // GridLayout will arrange elements in Grid Manager 8 X 8
    this.setSize(650, 650); // Size of the chess board
    this.setVisible(true); // Sets the board visible
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If you close the window, the program will terminate
    this.setResizable(false); //The window is not resizable anymore ;)
}

public void actionPerformed(ActionEvent ae) {
    String action = ae.getActionCommand();
    if(action.equals("A1")){
        System.out.println("lul");
    }

    if (action.equals("Red Soldier")) {
        System.out.println("Red Soldier");
    }
    if (action.equals("Red Ram")) {
        System.out.println("Red Ram");
    }
    if (action.equals("Blue Soldier")) {
        System.out.println("Blue Soldier");
    }
    if (action.equals("Blue Ram")) {
        System.out.println("Blue Ram");
    }
}



public static void main(String[] args) {

    String title = "City - A Two-Player Strategic Game";

    GameBoard gameBoard = new GameBoard(title); // Creating the instance of gameBoard
  }
}

感谢所有有类似问题的帮助和好运!

0 个答案:

没有答案