Java ActionListener:禁用按钮

Java ActionListener:禁用按钮,java,jframe,actionlistener,Java,Jframe,Actionlistener,我目前正在开发一个Java类,该类生成一个简单的Tic-Tac-Toe的JFrame/JButton布局。在实现ActionListener时,我打算让选定的JButton将其标题设置为“X”或“O”(基于是否轮到X选择JButton的布尔语句),并将其禁用(因此无法在接下来的回合中播放)。我创建的当前应用程序可以做到这一点,但在我单击另一个按钮之前,它有时不会更改JButton文本或禁用按钮。当我单击其中一个jbutton时,似乎没有任何内聚顺序。我花了几个小时试图解决这个问题,但没有结果。我

我目前正在开发一个Java类,该类生成一个简单的Tic-Tac-Toe的JFrame/JButton布局。在实现ActionListener时,我打算让选定的JButton将其标题设置为“X”或“O”(基于是否轮到X选择JButton的布尔语句),并将其禁用(因此无法在接下来的回合中播放)。我创建的当前应用程序可以做到这一点,但在我单击另一个按钮之前,它有时不会更改JButton文本或禁用按钮。当我单击其中一个jbutton时,似乎没有任何内聚顺序。我花了几个小时试图解决这个问题,但没有结果。我如何编写actionPerformed方法或如何将其添加到JButtons中有问题吗

以下是我的课程代码:

import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;

public class TTT extends JFrame implements ActionListener{

  // private fields
  private JButton[] buttonArray;
  private JLabel prompt;
  private boolean turnX;
  private String letter;



 public TTT() {
    // Instantiates JFrame window and adds lines to board
    super.setSize(235, 280);
    super.setTitle("Tic-Tac-Toe");
    // Instantiates JButton array
    buttonArray = new JButton[9];
    // Loop that creates the JButton squares
    for(int y = 30; y <= 140; y += 55) {
      for(int x = 30; x <= 140; x += 55) {
        for(int index = 0; index < buttonArray.length; index++) {
        buttonArray[index] = new JButton();
        buttonArray[index].setSize(50, 50);
        buttonArray[index].setLocation(x, y);
        buttonArray[index].addActionListener(this);
        super.add(buttonArray[index]);
      }  
    }
    }
    prompt = new javax.swing.JLabel("X's TURN");
    prompt.setVerticalAlignment(JLabel.BOTTOM);
    super.add(prompt);

    turnX = true;

    super.setVisible(true);
  } 

 public void actionPerformed(java.awt.event.ActionEvent a) {

        // Calculate whose turn it is
        if(turnX){
            letter = "X";
            prompt.setText("O's TURN");
            turnX = false;    
        } else if(!turnX){
            letter = "O";
            prompt.setText("X's TURN");
            turnX = true;
        }
        JButton pressedButton = (JButton)a.getSource(); 
        pressedButton.setText(letter);
        pressedButton.setEnabled(false);
        super.repaint();
 }

  public static void main(String[] args) {
    new TTT();
  } 
}  
import javax.swing.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类TTT扩展JFrame实现ActionListener{
//私人领域
私有JButton[]按钮数组;
专用JLabel提示符;
私有布尔turnX;
私人字符串字母;
公共TTT(){
//实例化JFrame窗口并向线路板添加线路
超级设置大小(235280);
超级标题(“Tic Tac Toe”);
//实例化JButton数组
buttonArray=新的JButton[9];
//创建JButton正方形的循环
对于(int y=30;y,使用此代码:

for(int y = 30; y <= 140; y += 55) {
  for(int x = 30; x <= 140; x += 55) {
    for(int index = 0; index < buttonArray.length; index++) {

非常感谢!当我读到我在JFrame中添加了82个按钮时,我笑了。。。
...
int index = 0;   // <-- Add this line
for(int y = 30; y <= 140; y += 55) {
    for(int x = 30; x <= 140; x += 55) {
           // <-- remove the third for-loop
        buttonArray[index] = new JButton();
        buttonArray[index].setSize(50, 50);
        buttonArray[index].setLocation(x, y);
        buttonArray[index].addActionListener(this);
        super.add(buttonArray[index]);
        index++;   // <-- increment 'index'
    }  
}
...
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        new TTT();
    }
});