2014-01-21 33 views
0

我有一小段代码设计用来在单击按钮时绘制卡片。截至目前,当我激活actionPerformed()时,出现以下错误(其中包括):线程“AWT-EventQueue-0 java.lang.IndexOutOfBoundsException ...”中的异常。我完全陌生,所以原谅我,如果这是一个简单的错误。来自ActionListener的Java调用方法

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; /*Used for Random and ArrayList*/ 
public class AL extends Frame implements WindowListener,ActionListener { 
    TextField text = new TextField(20); 
    Button b; 
    private int numClicks = 0; 
    private static ArrayList<Card> deck=new ArrayList<Card>(); 
    private static ArrayList<Card> playerOneDeck=new ArrayList<Card>(); 
    private static ArrayList<Card> playerTwoDeck=new ArrayList<Card>(); 



    public static void main(String[] args) { 
      AL myWindow = new AL("Well of course you know, this means WAR!"); 
      myWindow.setSize(350,100); 
      myWindow.setVisible(true); 
      ArrayList<Card> playerTwoDeck=new ArrayList<Card>(); 
    }//end main() 

    public AL(String title) { 

      super(title); 
      setLayout(new FlowLayout()); 
      addWindowListener(this); 
      b = new Button("Draw"); 
      add(b); 
      add(text); 
      b.addActionListener(this); 
      ArrayList<Card> deck=new ArrayList<Card>(); 
      ArrayList<Card> playerOneDeck=new ArrayList<Card>(); 
      deck=initializeDeck(); 
      Collections.shuffle(deck); 
    } 

    public void actionPerformed(ActionEvent e) { 
      numClicks++; 
      Card theCard=playerOneDeck.get(0); 
      text.setText(theCard.name); 
      text.setText(theCard.name); 
    } 

    public void windowClosing(WindowEvent e) { 
      dispose(); 
      System.exit(0); 
    } 
    /*Assign names, values, and suits to all of the Card objects.*/ 
    public static ArrayList<Card> initializeDeck() { 
     Card[] tempDeck=new Card[52]; 
     ArrayList<Card> completeDeck=new ArrayList<Card>(); 
     for (int i=0; i<tempDeck.length; i++) { 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Two"; 
      tempDeck[i].value=2; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Three"; 
      tempDeck[i].value=3; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Four"; 
      tempDeck[i].value=4; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Five"; 
      tempDeck[i].value=5; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Six"; 
      tempDeck[i].value=6; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Seven"; 
      tempDeck[i].value=7; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Eight"; 
      tempDeck[i].value=8; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Nine"; 
      tempDeck[i].value=9; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Ten"; 
      tempDeck[i].value=10; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Jack"; 
      tempDeck[i].value=11; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Queen"; 
      tempDeck[i].value=12; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="King"; 
      tempDeck[i].value=13; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Ace"; 
      tempDeck[i].value=14; 
     }//end FOR 
     /*Assign suits*/ 
     /*keep in mind that the array is now [2], [3],..[k],[a],[2],[3],[k],[a],etc */ 
     for (int j=0; j<tempDeck.length; j++) { 
      tempDeck[j].suit="Hearts"; 
      j++; 
      tempDeck[j].suit="Diamonds"; 
      j++; 
      tempDeck[j].suit="Spades"; 
      j++; 
      tempDeck[j].suit="Clubs"; 
     }//end FOR 

     for (int k=0; k<tempDeck.length;k++) { 
      completeDeck.add(tempDeck[k]); 
     } 
     return completeDeck; 
    }//end initializeDeck() 

    public void windowOpened(WindowEvent e) {} 
    public void windowActivated(WindowEvent e) {} 
    public void windowIconified(WindowEvent e) {} 
    public void windowDeiconified(WindowEvent e) {} 
    public void windowDeactivated(WindowEvent e) {} 
    public void windowClosed(WindowEvent e) {} 

} //结束AL类

+2

1)为了更好地提供帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)。 2)对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。 3)通常不需要使用'static',并且代码味道不好。 –

+0

堆栈跟踪说哪一行发生异常? – Kayaman

+0

顺便说一句 - 为什么是AWT而不是Swing?看到我对[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放弃使用AWT组件。 –

回答

1

您尝试在ActionPerformed第一行从PlayerOneDeck获得的第一个元素:Card theCard=playerOneDeck.get(0);然而,PlayerOneDeck是空ArrayList。它没有元素,所以试图从中得到一个元素返回IndexOutOfBoundsException

您初始化一个类变量PlayerOneDeck并且还有一个变量在您的同名构造函数中。您可以使用元素填充构造函数变量,但actionPerformed将调用保留为空的类变量。例如:

public class Main { 
    ArrayList<Integer> array = new ArrayList<Integer>(); // class variable 

    public static void main(String[] args) { 
     ArrayList<Integer> array = new ArrayList<Integer>(); // method variable, overrides class variable within the main method 
     array.add(1); // adds 1 to the method variable 
     (new Main()).printArray(); 
    } 

    public void printArray() { 
     System.out.println(array.get(0)); // tries to print the first element of class variable, throws IndexOutOfBoundsException 
    } 
} 

删除您的本地变量并仅使用全局类,并且您的问题可能会消失。

+0

我的歉意是,我复制并粘贴了一个旧版本。 PlayOneDeck现在有内容。 – eggHunter

+0

我有一个无GUI的版本,工作正常,所以我知道逻辑是正确的。 – eggHunter

+1

我注意到你有一个类变量'PlayerOneDeck',并且还在'main'方法中实例化了一个名字相同的变量。你可能试图用元素填充该方法变量,但是'actionPerformed'使用类实例变量,它将保持为空。 – asaini007