【发布时间】:2013-12-07 17:41:38
【问题描述】:
请遵循 eventList 的路径。虽然它最初应该存储正确的对象,但它会在进入 MouseClicked() 时立即清空自己。有一个驱动程序用于在另一个类中运行 initialize()。我似乎无法让 eventList 保存其信息。
public class Adventure_Chapter1 implements MouseListener
{
boolean success = true;
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();
public void initiliaze() throws FontFormatException, IOException
{
load(); // loads StoryEvents
play(0);
System.out.println("Init() eventList size: " + eventList.size());
}
private void load()
{
int x = 0;
switch(x)
{
case 0:
StoryEvent txt0 = new StoryEvent(parameters);
eventList.add(txt0);
case 1:
StoryEvent assassinStart = new StoryEvent(parameters);
eventList.add(assassinStart);
}
}
public void updatePlayer()
{
System.out.println("Player Updated ");
}
public void play(int c) // to be implemented
{
storyLineDisplay.setText("testing");
System.out.println("Play() eventList size:" + eventList.size());
//int c would typically change my buttons' options next to them. but for now it
is irrelevant.
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource().equals(buttonOne));
{
if (success == true)
{
updatePlayer();
System.out.println("MouseClicked eventList size: " + eventList.size());
play(1);
}
else
{
updatePlayer();
currentCharacter.add(eventList.get(choice));
choice = currentCharacter.get(currentCharacter.size() -1).getFail1();
play(1);
}
}
}
输出在这里:
Play() eventList size:2
Init() eventList size: 2
Player Updated
MouseClicked eventList size: 0
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Adventure_Chapter1.mouseClicked(Adventure_Chapter1.java:277)
insert further lines of errors here.
【问题讨论】:
-
请应用正确的格式!
-
在我们回答这个问题之前,您想帮自己一个忙,并删除对您的问题没有影响的代码,直到您拥有仍然显示问题的最小代码库。当你这样做的时候,你几乎肯定会发现问题。
-
不知道是不是和问题有关系,但是
mouseClicked:if (e.getSource().equals(buttonOne));这一行多了一个分号,需要去掉。 -
我建议您在调试模式下运行您的代码并在
eventList上添加一个监视。您提供的代码对我来说看起来不错。可能您在其他地方覆盖了您的列表。 -
编辑删除更多代码。现在的任何内容都会影响 eventList 或需要跟随输出。
标签: java arraylist awt mouselistener