我的某个项目有问题。下面的代码显示了现在的游戏。根据按钮的颜色,当你点击JButtons时我需要设置不同的功能。例如,如果单击Cyan按钮,它将播放CyanBlock()函数。玩游戏时颜色会发生变化。
package city;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
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;
JButton squareButton = null;
for (int i = 1; i <= squareCount; i++) {
squareButton = new JButton();
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
}
}
感谢您的帮助。祝大家好运和我一样有问题!