Java从ActionListener调用方法

时间:2014-01-21 18:50:40

标签: java awt actionlistener

我有一小段代码,用于在单击按钮时绘制卡片。截至目前,当我激活actionPerformed()时出现以下错误(等等):线程中的异常" AWT-EventQueue-0 java.lang.IndexOutOfBoundsException ..."。如果这是一个简单的错误,请原谅我。

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类

1 个答案:

答案 0 :(得分:1)

您尝试从PlayerOneDeck的第一行ActionPerformed获取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 } } 调用类变量,该变量保持为空。例如:

{{1}}

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