简体   繁体   English

Java-如何修改一个类中的静态ArrayList和另一个类中的ActionListener?

[英]Java - How do I modify static ArrayList in one class with an ActionListener in another class?

I'm trying to modify a static ArrayList, "selectList," in the class "AnimalGUI." 我正在尝试在“ AnimalGUI”类中修改静态ArrayList“ selectList”。

I have to take user input through Java GUI so I implemented JFrame and all that in another class. 我必须通过Java GUI接受用户输入,因此我在另一个类中实现了JFrame以及所有其他功能。

I encounter the problem when I pass the ArrayList to an ActionListener that exists in a different class, "HandleGUI." 当我将ArrayList传递给另一个类“ HandleGUI”中的ActionListener时遇到了问题。 The ActionListener cannot modify the static ArrayList for some reason. 由于某种原因,ActionListener无法修改静态ArrayList。

I know it's not working because I tried to print the ArrayList's content after, supposedly, modifying it and the system printed nothing. 我知道它不起作用,因为我想在修改ArrayList的内容后尝试打印它,而系统什么也不打印。

Below is the class Animal GUI, nothing special to focus on besides the ArrayList "selectList" at the bottom of the code. 下面是动物GUI类,除了代码底部的ArrayList“ selectList”外,没有什么特别需要关注的。

public class AnimalGUI {

public static void main(String[] args) {
    AnimalGUI a = new AnimalGUI();
    HandleGUI h = new HandleGUI();

    h.initialisation();
    a.printSelectList();
}

//Print selectList
void printSelectList(){
    for(int x: selectList){
        System.out.println(x);
    }
}

static ArrayList<Integer> selectList = new ArrayList<Integer>();
static ArrayList<Animal> aniList = new ArrayList<Animal>();
static String[][] Forest = new String[15][15];
}

Below is the subclass "HandleGUI". 下面是子类“ HandleGUI”。

You can ignore most of the Java GUI components. 您可以忽略大多数Java GUI组件。 The important part here is the ActionListener "selectListener." 这里的重要部分是ActionListener“ selectListener”。

public class HandleGUI{

JFrame menu = new JFrame("Startup Menu");
JPanel panelA = new JPanel();
JCheckBox cCB, dCB, fCB, hCB, lCB, tCB, uCB, wCB;
JButton cBT, dBT, fBT, hBT, lBT, tBT, uBT, wBT, start;

//Constructor
public HandleGUI(){

    //Declare all the JCheckBoxes.
    cCB = new JCheckBox("Cat");
    dCB = new JCheckBox("Dog");
    fCB = new JCheckBox("Fox");
    hCB = new JCheckBox("Hippo");
    lCB = new JCheckBox("Lion");
    tCB = new JCheckBox("Tiger");
    uCB = new JCheckBox("Turtle");
    wCB = new JCheckBox("Wolf");

    //Declare all the JButtons.
    cBT = new JButton("Pick an alternative icon.");
    dBT = new JButton("Pick an alternative icon.");
    fBT = new JButton("Pick an alternative icon.");
    hBT = new JButton("Pick an alternative icon.");
    lBT = new JButton("Pick an alternative icon.");
    tBT = new JButton("Pick an alternative icon.");
    uBT = new JButton("Pick an alternative icon.");
    wBT = new JButton("Pick an alternative icon.");

    start = new JButton("Start");

    //Declare JFrame and set its size.
    //Make the startup menu display in centre of monitor.
    menu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    menu.setSize(300, 300);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    menu.setLocation(dim.width/2-menu.getSize().width/2, dim.height/2-menu.getSize().height/2);


}

//Generates a setup menu that allows user to select animals to be included in the simulation. 
public void initialisation(){

    //Make the startup menu display in centre of monitor.
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    menu.setLocation(dim.width/2-menu.getSize().width/2, dim.height/2-menu.getSize().height/2);

    //Set the JPanel to grid layout so elements on both columns align.
    panelA.setLayout(new GridLayout(8, 2, 5, 5));

    //Add the elements into JPanel.
    panelA.add(cCB);
    panelA.add(cBT);
    panelA.add(dCB);
    panelA.add(dBT);
    panelA.add(fCB); 
    panelA.add(fBT);
    panelA.add(hCB);
    panelA.add(hBT);
    panelA.add(lCB);
    panelA.add(lBT);
    panelA.add(tCB);
    panelA.add(tBT);
    panelA.add(uCB);
    panelA.add(uBT);
    panelA.add(wCB);
    panelA.add(wBT);

    //Add the Panel to the Frame.
    menu.getContentPane().add(panelA, BorderLayout.CENTER);
    menu.getContentPane().add(start, BorderLayout.SOUTH);

    //Make Frame visible.
    menu.setVisible(true);

    start.addActionListener(new selectListener());
}

class selectListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        AnimalGUI a = new AnimalGUI();


        if(cCB.isSelected())
            a.selectList.add(0);

        if(dCB.isSelected())
            a.selectList.add(1);

        if(fCB.isSelected())
            a.selectList.add(2);

        if(hCB.isSelected())
            a.selectList.add(3);

        if(lCB.isSelected())
            a.selectList.add(4);

        if(tCB.isSelected())
            a.selectList.add(5);

        if(uCB.isSelected())
            a.selectList.add(6);

        if(wCB.isSelected())
            a.selectList.add(7);

        menu.setVisible(false);
    }
}
}

Looks like conditions in your selectListener are not met. 看起来您的selectListener中的条件未满足。 So, the issue is not in static list not modifyable, but that program doesn't enter any if statement: 因此,问题不在静态列表中不可修改,但该程序未输入任何if语句:

if(cCB.isSelected())
    a.selectList.add(0);
.....

You are calling static fields from objects. 您正在从对象调用静态字段。

Instead of 代替

AnimalGUI a = new AnimalGUI(); <...> a.selectList.add(0);

Try AnimalGUI.selectList.add(0); 试试AnimalGUI.selectList.add(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM