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

时间:2017-04-03 20:41:21

标签: java user-interface arraylist static actionlistener

我试图修改静态ArrayList," selectList,"在班级" AnimalGUI。"

我必须通过Java GUI获取用户输入,因此我在另一个类中实现了JFrame和所有这些。

当我将ArrayList传递给另一个类中存在的ActionListener时,我遇到了问题," HandleGUI。"由于某种原因,ActionListener无法修改静态ArrayList。

我知道它不起作用,因为我试图打印ArrayList的内容,据说修改它并且系统什么都没打印。

以下是动物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];
}

以下是子类&#34; HandleGUI&#34;。

您可以忽略大多数Java GUI组件。这里的重要部分是ActionListener&#34; selectListener。&#34;

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);
    }
}
}

2 个答案:

答案 0 :(得分:0)

看起来selectListener中的条件不符合要求。因此,问题不在于静态列表中不可修改,但该程序不会输入任何if语句:

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

答案 1 :(得分:0)

您正在从对象调用静态字段。

而不是

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

尝试 AnimalGUI.selectList.add(0);