【问题标题】:Difficulty with ArrayLists and mouseClickedArrayLists 和 mouseClicked 的困难
【发布时间】:2013-11-18 18:37:39
【问题描述】:

所以我是 Java 新手,目前正在完成 AP 课程后的独立学习。为了创建一个文本冒险,我决定使用 ArrayLists 来存储玩家选择和参与的每个“StoryEvent”。当你看到代码时它会更有意义,但基本上每当我将 StoryEvent 对象添加到我的 ArrayList 列表时规模增长,因为他们应该。然而,每当从 MouseClicked() 方法引用 ArrayList 时,ArrayList 就会以某种方式变为 0。我使用驱动程序来调用所有这些,并使用单独的类来创建 StoryEvents,但相关代码将随之而来。您需要在此处查看并关注的问题是 eventList。

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;


public class Adventure_Chapter1 implements MouseListener
{
// storyline based strings/buttons

static String storyLineStart = ("StoryLineStart");

static String storyOptionOne = ("Assassin Option");
static String storyOptionTwo = ("Ranger Option");
static String storyOptionThree = ("Druid Option");
static String storyOptionFour = ("Wizard Option");
static String storyOptionFive = ("Paladin Option");
static String storyOptionSix = ("Berserker Option");

boolean success = true;
JTextArea storyLineDisplay = new JTextArea(storyLineStart);
JTextArea optionOne = new JTextArea(storyOptionOne);
JTextArea optionTwo = new JTextArea(storyOptionTwo);
JTextArea optionThree = new JTextArea(storyOptionThree);
JTextArea optionFour = new JTextArea(storyOptionFour);
JTextArea optionFive = new JTextArea(storyOptionFive);
JTextArea optionSix = new JTextArea(storyOptionSix);
JPanel totalGUI;
JLabel title, chapter;
JButton buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix;
static int choice = 1;
ArrayList<StoryEvent> currentCharacter = new ArrayList<StoryEvent>();
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();


public JPanel createContentPane()   {

    JLabel chapter = new JLabel ("ESCAPE FROM AGROK KEEP");
                   //creates JLabel to be the chapter title
    chapter.setSize(350, 30);
    chapter.setLocation(465, 40);
    totalGUI.add(chapter);

    //storyLineDisplay JTextArea
    storyLineDisplay.setLocation(50, 100);
    storyLineDisplay.setSize(1170, 450);
    storyLineDisplay.setVisible(true);
    storyLineDisplay.setEditable(false);
    storyLineDisplay.setLineWrap(true);
    storyLineDisplay.setWrapStyleWord(true);
    totalGUI.add(storyLineDisplay);

    JButton buttonOne = new JButton("Option One");                          //creates the buttons for a player to use and 
    buttonOne.setLocation(50, 600);                                         // JTextAreas to display options
    buttonOne.setSize(110, 50);
    buttonOne.addMouseListener(this);
    totalGUI.add(buttonOne);

    optionOne.setLocation(210, 600);
    optionOne.setSize(600,50);
    optionOne.setVisible(true);
    optionOne.setEditable(false);
    optionOne.setLineWrap(true);
    optionOne.setWrapStyleWord(true);
    totalGUI.add(optionOne);

    JButton buttonTwo = new JButton("Option Two");
    buttonTwo.setLocation(50, 660);
    buttonTwo.setSize(110, 50);
    buttonTwo.addMouseListener(this);
    totalGUI.add(buttonTwo);

    optionTwo.setLocation(210, 660);
    optionTwo.setSize(600, 50);
    optionTwo.setVisible(true);
    optionTwo.setEditable(false);
    optionTwo.setLineWrap(true);
    optionTwo.setWrapStyleWord(true);
    totalGUI.add(optionTwo);

    JButton buttonThree = new JButton("Option Three");
    buttonThree.setLocation(50, 720);
    buttonThree.setSize(110, 50);
    buttonThree.addMouseListener(this);
    totalGUI.add(buttonThree);

    optionThree.setLocation(210, 720);
    optionThree.setSize(600, 50);
    optionThree.setVisible(true);
    optionThree.setEditable(false);
    optionThree.setLineWrap(true);
    optionThree.setWrapStyleWord(true);
    totalGUI.add(optionThree);

    JButton buttonFour = new JButton("Option Four");
    buttonFour.setLocation(50, 780);
    buttonFour.setSize(110, 50);
    buttonFour.addMouseListener(this);
    totalGUI.add(buttonFour);

    optionFour.setLocation(210, 780);
    optionFour.setSize(600, 50);
    optionFour.setVisible(true);
    optionFour.setLineWrap(true);
    optionFour.setWrapStyleWord(true);
    optionFour.setEditable(false);
    totalGUI.add(optionFour);

    JButton buttonFive = new JButton("Option Five");
    buttonFive.setLocation(50, 840);
    buttonFive.setSize(110, 50);
    buttonFive.addMouseListener(this);
    totalGUI.add(buttonFive);

    optionFive.setLocation(210, 840);
    optionFive.setSize(600, 50);
    optionFive.setVisible(true);
    optionFive.setEditable(false);
    optionFive.setLineWrap(true);
    optionFive.setWrapStyleWord(true);
    totalGUI.add(optionFive);

    JButton buttonSix = new JButton("Option Six");
    buttonSix.setLocation(50, 900);
    buttonSix.setSize(110, 50);
    buttonSix.addMouseListener(this);
    totalGUI.add(buttonSix);

    optionSix.setLocation(210, 900);
    optionSix.setSize(600, 50);
    optionSix.setVisible(true);
    optionSix.setEditable(false);
    optionSix.setLineWrap(true);
    optionSix.setWrapStyleWord(true);
    totalGUI.add(optionSix);

    // add stat JLabels for characters here

    return totalGUI;
}



public void initiliaze() throws FontFormatException, IOException
{
    JFrame adventureFrame = new JFrame("The Fall of Agronak");              //creates JFrame for text adventure

    Adventure_Chapter1 demo = new Adventure_Chapter1();
    adventureFrame.setContentPane(demo.createContentPane());

    adventureFrame.setSize(1280, 1024);
    adventureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    adventureFrame.setVisible(true);

    load();     // loads StoryEvents
    play(0);
    System.out.println("Init() eventList size: " + eventList.size());
}

private void load()
{
    //ArrayList<StoryEvent> loadList = new ArrayList<StoryEvent>();
    int x = 0;
    switch(x)
    {
    case 0:
        StoryEvent txt0 = new StoryEvent(storyLineStart, storyOptionOne, storyOptionTwo, storyOptionThree, storyOptionFour,
                storyOptionFive, storyOptionSix, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6);
        eventList.add(txt0); 
    case 1:
        StoryEvent assassinStart = new StoryEvent("Assassin Breaks out of Jail.", "option 1", "option2", "option3",
                "option4", "option5", "option6", 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7);
        eventList.add(assassinStart);
    }
}

public void updatePlayer()                          // to be implemented - reprint stats and check if dead
{
    System.out.println("Player Updated ");
}

public void play(int c)                                 // to be implemented
{
    storyLineDisplay.setText("testing");
    System.out.println("Play() eventList size:" + eventList.size());
    optionOne.setText(eventList.get(c).getOption1());
    optionTwo.setText(eventList.get(c).getOption2());
    optionThree.setText(eventList.get(c).getOption3());
    optionFour.setText(eventList.get(c).getOption4());
    optionFive.setText(eventList.get(c).getOption5());
    optionSix.setText(eventList.get(c).getOption6());
}

public void mouseClicked(MouseEvent e)
{
    if (e.getSource().equals(buttonOne));
    {
        if (success == true)
        {
            updatePlayer();
            System.out.println("MouseClicked eventList size: " + eventList.size());
            //currentCharacter.add(eventList.get(choice));
            //choice = eventList.get(choice).getNext1();
            play(choice);
        }
        else
        {
            updatePlayer();
            currentCharacter.add(eventList.get(choice));
            choice = currentCharacter.get(currentCharacter.size() -1).getFail1();

        }
    }

}

所以,基本上我调用initialize(),然后创建所有的GUI 元素。然后它调用 load() 将我的 StoryEvents 添加到 ArrayList eventList 中,这是这里的主要问题。在 initialize() 中 eventList 的大小是 2,在 play() 方法中也是如此。 play() 方法稍后将用于使用游戏的适当故事情节选项来更改 JTextAreas。然而,一旦我单击一个按钮,在这种情况下,我只留下了如果我单击 buttonOne 的代码,并检查 eventList 的大小,我得到 0。对此我无能为力,并试图询问 co -学生、以前的学生和我的导师。如果有人能解释为什么 eventList 变成 0 大小,以及如何解决这个问题,我将不胜感激。如果成功 boolean == true,您可以忽略部分。这将是稍后游戏的一部分,但目前无关紧要。每当我尝试从 MouseClicked 或 play() 中的 eventList 检索信息时,我得到的错误是 IndexOutOfBoundsExceptions。 Eclipse 告诉我,在 play() 中,ArrayList 的大小为 2,但它在两行之后给了我一个错误。这是输出:

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.

【问题讨论】:

  • 我在这段代码中看不到任何会重置该列表的内容。也许您正在一遍又一遍地创建包含列表的代码的新实例。我也没有看到这方面的证据。无论哪种情况,如果您需要更多帮助,请提供显示问题的代码子集。这段代码太多了。
  • 是的,请只发布代码的相关部分。
  • 在您的private void load() 方法中还有一件事您正在使用switch,在完成特定情况后您需要使用break,否则它将用于下一个可用的case。
  • 这是一个完整的猜测,但请尝试创建一个默认构造函数并在其中添加一个 println:public Adventure_Chapter1(){System.out.println("In constructor");}。我实际上没有在代码中看到您实例化任何东西的任何地方,但是如果在您不知情的情况下以某种方式创建了一个新对象,那将解释您的问题。
  • 如果你遵循 eventList 的路径 - 给我带来问题的 arrayList - 它从类顶部的创建到 load() 方法,然后是 mouseClicked()。当我明天再次获得所有代码时,我可以只重新发布这些部分作为对这个问题的回应。

标签: java arraylist mouseclick-event


【解决方案1】:

在你的代码中我看到了代码

//currentCharacter.add(eventList.get(choice));

被评论。这会导致在 arrayList 中没有任何内容。

因此代码

currentCharacter.get(currentCharacter.size() -1)

抛出 NUllPointer 异常

【讨论】:

  • 目前有意将其作为评论留下。问题是在 load() 方法中,arrayList 中存储了两个 StoryEvent 对象。这些稍后会在 MouseClicked() 方法中消失。
【解决方案2】:

在我看来这里有两个问题。一个是您缺少一个无效的静态主入口点,您可以在其中“打包” UI 并启动 AWT 事件线程。另一个是没有'main'怎么可能运行?

您需要将面板添加到 JFrame 对象中。 到这里构建一个 JFrame 对象。然后将您的面板添加到该“本机”容器中,您的结果应该会有所改善。

http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

【讨论】:

  • 有一个单独的类被用作驱动程序,并且在代码中创建了一个JFrame。 GUI 不是这里的问题,但我感谢您的意见。
【解决方案3】:

请按照eventList的路径走。

public class Adventure_Chapter1 implements MouseListener
{
//GUI based JObjects here

boolean success = true;
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();


public JPanel createContentPane()   
{
   //creates GUI
}



public void initiliaze() throws FontFormatException, IOException
{
    JFrame adventureFrame = new JFrame("The Fall of Agronak");

Adventure_Chapter1 demo = new Adventure_Chapter1();
adventureFrame.setContentPane(demo.createContentPane());

adventureFrame.setSize(1280, 1024);
adventureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adventureFrame.setVisible(true);

load();     // loads StoryEvents
play(0);
System.out.println("Init() eventList size: " + eventList.size());
}

private void load()
{
    //ArrayList<StoryEvent> loadList = new ArrayList<StoryEvent>();
    int x = 0;
    switch(x)
    {
    case 0:
    StoryEvent txt0 = new StoryEvent(storyLineStart, storyOptionOne, storyOptionTwo, storyOptionThree, storyOptionFour,
            storyOptionFive, storyOptionSix, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6);
    eventList.add(txt0); 
    case 1:
    StoryEvent assassinStart = new StoryEvent("Assassin Breaks out of Jail.", "option 1", "option2", "option3",
            "option4", "option5", "option6", 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7);
    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());
    optionOne.setText(eventList.get(c).getOption1());
    optionTwo.setText(eventList.get(c).getOption2());
    optionThree.setText(eventList.get(c).getOption3());
    optionFour.setText(eventList.get(c).getOption4());
    optionFive.setText(eventList.get(c).getOption5());
    optionSix.setText(eventList.get(c).getOption6());
 }

public void mouseClicked(MouseEvent e)
{
    if (e.getSource().equals(buttonOne));
    {
        if (success == true)
        {
            updatePlayer();
            System.out.println("MouseClicked eventList size: " + eventList.size());
            play(choice);
        }
        else
        {
            updatePlayer();
            currentCharacter.add(eventList.get(choice));
            choice = currentCharacter.get(currentCharacter.size() -1).getFail1();

        }
    }

}

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多