有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用ArrayList和鼠标单击的java困难

因此,我对Java是新手,在完成AP课程后,目前正在进行独立学习。为了创建文本冒险,我决定使用ArrayList存储玩家选择并参与的每个“故事事件”。当您看到代码时,它会更有意义,但本质上每当我将StoryEvent对象添加到ArrayList时,列表的大小都会相应地增加。但是,每当从MouseClicked()方法引用ArrayList时,ArrayList的大小都会变为0。我使用一个驱动程序来调用所有这些,并使用一个单独的类来创建StoryEvents,但相关的代码将随后出现。您需要在这里查看并关注的问题是事件列表

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(),将my StoryEvents添加到ArrayList事件列表中,这是此处的主要问题。initialize()和play()方法中的eventList的大小都是2。play()方法将在稍后使用适当的游戏故事线选项来更改JTextAreas。然而,只要我单击一个按钮,在本例中,我只留下了如果我单击buttonOne并检查我得到的事件列表的大小0的代码。对此,我几乎无能为力,我曾试图询问同学、以前的学生和我的老师。如果有人能解释为什么事件列表变为0大小,以及如何解决这个问题,我将不胜感激。您可以忽略有关成功布尔值是否为true的部分。这将是游戏的一部分,但目前并不重要。每当我试图从MouseClicked或play()中的事件列表检索信息时,我得到的错误都是IndexOutofBoundsException。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.

共 (3) 个答案

  1. # 1 楼答案

    请遵循事件列表的路径

    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();
    
            }
        }
    
    }
    
  2. # 2 楼答案

    在我看来,这里有两个问题。一个是缺少一个void静态主入口点,在这里你“打包”UI并启动AWT事件线程。另一个问题是,如果没有“主”系统,这些系统如何运行

    需要将面板添加到JFrame对象中。 转到此处并构建一个JFrame对象。然后将面板添加到“原生”容器中,结果应该会有所改善

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

  3. # 3 楼答案

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

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

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

    因此,代码

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

    引发空指针异常