gpt4 book ai didi

Java 从 ActionListener 调用方法

转载 作者:行者123 更新时间:2023-12-01 13:36:02 26 4
gpt4 key购买 nike

我有一小段代码,旨在在单击按钮时绘制卡片。截至目前,当我激活 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 类(class)结束

最佳答案

您尝试从 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
}
}

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

关于Java 从 ActionListener 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21266664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com