在类之间传递时,ArrayList添加不起作用

时间:2019-06-25 16:03:48

标签: java swing arraylist collections

到目前为止,调用ArrayList add(E e)方法对我而言仍然无效,我想知道如何修复它以及为什么首先遇到错误。

GUITest.java

//import statements
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUITest implements ActionListener, Runnable {

    private JFrame frmInventorysystem;
    private JPanel frameTop, frameBottom;
    private JComboBox equipList;
    private String category = "";
    private JButton confirmBtn, cancelBtn;
    private JButton uploadBtn;
    private Weapon weapon;
    private ArrayList<Weapon> weapons = new ArrayList<Weapon>();
    private Armor armor;
    private ArrayList<Armor> armors = new ArrayList<Armor>();
    private GUIWeaponCategory weaponGUI = null;
    private GUIArmorCategory armorGUI = null;

    public GUITest() {
        // insert formatting code here
                frmInventorysystem = new JFrame();
        frmInventorysystem.setTitle("InventorySystem");
        frmInventorysystem.setBounds(100, 100, 450, 300);
        frmInventorysystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmInventorysystem.getContentPane().setLayout(new BorderLayout(0, 0));

        frameTop = new JPanel();
        frameTop.setLayout(new FlowLayout());

        /*
         * JFrame inside another JFrame is not possible. JPanels are used instead
         * Creating a flow layout for the bottom frame
         */
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        // creates comboBox to find out which of the three items player is looking to
        // insert
        // equipList.setModel(new DefaultComboBoxModel(new String[] {"Weapon", "Armor",
        // "Mod"}));
        String[] weaponCategories = { "Weapon", "Armor", "Mod" };
        equipList = new JComboBox(weaponCategories);
        frameTop.add(equipList);
        frmInventorysystem.getContentPane().add(frameTop, BorderLayout.NORTH);

        // Converting BorderLayout.south into a flow layout
        frmInventorysystem.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);

        // creates new windows to sort equipment when confirmBtn is clicked
        frameBottom.add(confirmBtn);

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);

        frameBottom.add(cancelBtn);

        uploadBtn = new JButton("Upload");

        uploadBtn.addActionListener(this);

        frameBottom.add(uploadBtn);

        frmInventorysystem.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        // creates new windows to sort equipment when confirmBtn is clicked
        if (e.getSource() == confirmBtn) {
            if (equipList.getSelectedItem().equals("Weapon")) {
                weaponGUI = new GUIWeaponCategory();
                SwingUtilities.invokeLater(weaponGUI);
            }
        }
        // Exits when cancelBtn is clicked
        // exit program

        if (e.getSource() == uploadBtn) {
            // uploads data to excel sheet
            if (weaponGUI != null) {
                weapons = weaponGUI.getWeapons();
                System.out.println(weaponGUI.toString());
            }
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        GUITest window = new GUITest();
        SwingUtilities.invokeLater(window);
    }
}

GUIWeaponCategory.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;

public class GUIWeaponCategory implements ActionListener, Runnable {
    private JButton confirmBtn, cancelBtn;
    private JFrame frmGuiweaponcategory;
    private JPanel frameBottom, frameCenter;
    private String weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill;
    private String[] weaponTypes, weaponVariants, activeSkills, handlingSkills, holsteredSkills;
    private JComboBox boxWeaponType, boxActiveSkill, boxHandlingSkill, boxHolsteredSkill;
    private JTextPane paneWeaponVariant;
    // creates weapon object
    // thoughts on creating a Weapon data structure?
    private Weapon weapon;
    private ArrayList<Weapon> weapons = new ArrayList<Weapon>();

    public GUIWeaponCategory() {
        // insert code for GUI here
        frmGuiweaponcategory = new JFrame();
        frmGuiweaponcategory.setTitle("GUIWeaponCategory");
        frmGuiweaponcategory.setBounds(100, 100, 450, 300);
        frmGuiweaponcategory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // new JPanel because JFrames can't be put in a JFrame
        // sets frame to be a FlowLayout as well
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        // comboBox for easy access to weapon category types
        boxWeaponType = new JComboBox();
        weaponTypes = new String[] { "Assault Rifle", "Light Machine Gun", "Marksman Rifle", "Pistol", "Rifle",
                "Shotgun", "Submachine Gun" };
        Arrays.sort(weaponTypes);
        boxWeaponType.setModel(new DefaultComboBoxModel(weaponTypes));
        frmGuiweaponcategory.getContentPane().add(boxWeaponType, BorderLayout.NORTH);

        // BorderLayout.SOUTH is now a FlowLayout
        frmGuiweaponcategory.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);
        frameBottom.add(confirmBtn);

        // adds cancelBtn and exits when buttonClicked
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);
        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frmGuiweaponcategory.dispose();
            }
        });
        frameBottom.add(cancelBtn);

        frameCenter = new JPanel();
        frmGuiweaponcategory.getContentPane().add(frameCenter, BorderLayout.CENTER);
        GridBagLayout gbl_frameCenter = new GridBagLayout();
        gbl_frameCenter.columnWidths = new int[] { 50, 50, 50 };
        gbl_frameCenter.rowHeights = new int[] { 50, 50, 50, 50 };
        gbl_frameCenter.columnWeights = new double[] { 0.0, 1.0 };
        gbl_frameCenter.rowWeights = new double[] { 1.0, Double.MIN_VALUE, 0.0 };
        frameCenter.setLayout(gbl_frameCenter);

        JLabel weaponVariant = new JLabel("Weapon Variant");
        GridBagConstraints gbc_weaponVariant = new GridBagConstraints();
        gbc_weaponVariant.insets = new Insets(0, 0, 5, 5);
        gbc_weaponVariant.gridx = 0;
        gbc_weaponVariant.gridy = 0;
        frameCenter.add(weaponVariant, gbc_weaponVariant);

        paneWeaponVariant = new JTextPane();
        GridBagConstraints gbc_paneWeaponVariant = new GridBagConstraints();
        gbc_paneWeaponVariant.fill = GridBagConstraints.HORIZONTAL;
        gbc_paneWeaponVariant.insets = new Insets(0, 0, 5, 5);
        gbc_paneWeaponVariant.gridx = 1;
        gbc_paneWeaponVariant.gridy = 0;
        frameCenter.add(paneWeaponVariant, gbc_paneWeaponVariant);

        // Add label to mark combo box
        JLabel activeLabel = new JLabel("Active Skill");
        GridBagConstraints gbc_activeLabel = new GridBagConstraints();
        gbc_activeLabel.insets = new Insets(0, 0, 5, 5);
        gbc_activeLabel.gridx = 0;
        gbc_activeLabel.gridy = 1;
        frameCenter.add(activeLabel, gbc_activeLabel);

        boxActiveSkill = new JComboBox();
        activeSkills = new String[] { "Boomerang", "BreadBasket", "Close & Personal", "Eyeless", "Fast Hands",
                "Finisher", "First Blood", "Frenzy", "Ignited", "Killer", "Lucky Shot", "Measured", "Naked",
                "Near Sighted", "On Empty", "Optimist", "Outsider", "Perpetuation", "Premeditated", "Preservation",
                "Pummel", "Ranger", "Reformation", "Rifleman", "Sadist", "Salvage", "Spike", "Steady Handed",
                "Strained", "Unhinged", "Unwavering", "Vindictive" };
        Arrays.sort(activeSkills);
        boxActiveSkill.setModel(new DefaultComboBoxModel(activeSkills));
        GridBagConstraints gbc_boxActiveSkill = new GridBagConstraints();
        gbc_boxActiveSkill.insets = new Insets(0, 0, 5, 5);
        gbc_boxActiveSkill.gridx = 1;
        gbc_boxActiveSkill.gridy = 1;
        frameCenter.add(boxActiveSkill, gbc_boxActiveSkill);

        JLabel lblHandlingSkill = new JLabel("Handling/Equipped Skill");
        GridBagConstraints gbc_lblHandlingSkill = new GridBagConstraints();
        gbc_lblHandlingSkill.insets = new Insets(0, 0, 5, 5);
        gbc_lblHandlingSkill.gridx = 0;
        gbc_lblHandlingSkill.gridy = 2;
        frameCenter.add(lblHandlingSkill, gbc_lblHandlingSkill);

        boxHandlingSkill = new JComboBox();
        handlingSkills = new String[] { "Cannon", "In Rhythm", "Protected Deploy", "Protected Reload", "Recharged",
                "Rooted", "Stop, Drop, and Roll", "Zen", "Accurate", "Allegro", "Distance", "Extra", "Jazz Hands",
                "Optimized", "Stable" };
        Arrays.sort(handlingSkills);
        boxHandlingSkill.setModel(new DefaultComboBoxModel(handlingSkills));
        GridBagConstraints gbc_boxHandlingSkill = new GridBagConstraints();
        gbc_boxHandlingSkill.insets = new Insets(0, 0, 5, 5);
        gbc_boxHandlingSkill.gridx = 1;
        gbc_boxHandlingSkill.gridy = 2;
        frameCenter.add(boxHandlingSkill, gbc_boxHandlingSkill);

        JLabel lblHolsteredSkill = new JLabel("Holstered Skill:");
        GridBagConstraints gbc_lblHolsteredSkill = new GridBagConstraints();
        gbc_lblHolsteredSkill.insets = new Insets(0, 0, 0, 5);
        gbc_lblHolsteredSkill.gridx = 0;
        gbc_lblHolsteredSkill.gridy = 3;
        frameCenter.add(lblHolsteredSkill, gbc_lblHolsteredSkill);

        boxHolsteredSkill = new JComboBox();
        holsteredSkills = new String[] { "Double Duty", "Everlasting", "Greased", "Overlap", "Transmission",
                "Wascally" };
        Arrays.sort(holsteredSkills);
        boxHolsteredSkill.setModel(new DefaultComboBoxModel(holsteredSkills));
        GridBagConstraints gbc_boxHolsteredSkill = new GridBagConstraints();
        gbc_boxHolsteredSkill.insets = new Insets(0, 0, 0, 5);
        gbc_boxHolsteredSkill.gridx = 1;
        gbc_boxHolsteredSkill.gridy = 3;
        frameCenter.add(boxHolsteredSkill, gbc_boxHolsteredSkill);

        // sets the JFrame to be visible
        frmGuiweaponcategory.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == confirmBtn) {
            // puts selected items into String variables
            // replace with a method to create a weapon later
            // consider the possibility for adding to a data structure
            createWeapon();
            frmGuiweaponcategory.dispose();
        }

        else if (e.getSource() == cancelBtn) {
            frmGuiweaponcategory.dispose();
        }
    }

    public ArrayList<Weapon> createWeapon() {
        weaponType = boxWeaponType.getSelectedItem().toString();
        weaponVariant = paneWeaponVariant.getText();
        activeSkill = boxWeaponType.getSelectedItem().toString();
        handlingSkill = boxHandlingSkill.getSelectedItem().toString();
        holsteredSkill = boxHolsteredSkill.getSelectedItem().toString();
        weapons.add(new Weapon(weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill));
        return weapons;
    }

    public ArrayList<Weapon> getWeapons() {
        return weapons;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}
public class Weapon {
    String weaponType, weaponVariant, activeSkill, handlingSkill, holsteredSkill;

    public Weapon(String weaponType, String weaponVariant, String activeSkill, String handlingSkill, String holsteredSkill )
    {
        this.weaponType = weaponType;
        this.weaponVariant = weaponVariant;
        this.activeSkill = activeSkill;
        this.handlingSkill = handlingSkill;
        this.holsteredSkill = holsteredSkill;
        }
    public String toString()
    {
        return weaponType + " " + weaponVariant + " " + activeSkill + " " + handlingSkill + " " + holsteredSkill;
    }
}

到目前为止,代码将一个武器对象从一类传递到另一类都没有问题。

但是,通过更改代码使其包含Arraylists,它使得程序仅在Arraylist中存储1个武器对象。

单击按钮后,我希望能够将整个数组列表从GUIWeaponCategory传递给GUITest。

1 个答案:

答案 0 :(得分:0)

尝试将数组列表的实例移动到一个类中的构造函数中,然后引用该类对该武器列表的特定获取器/设置器。 您的代码看起来像在多个类中创建了一个新的武器列表,这将解释您为什么包含一个值的问题(因为它们都是单独的实例)。

private ArrayList<Weapon> weapons;

        public GUIWeaponCategory() {
        //insert code for GUI here
           weapons = new ArrayList<Weapon>()
        }