如何从其他类更改HashMap中的值

时间:2018-10-27 05:23:49

标签: java hashmap

我写了大约1680行代码,主要创建了一个GUI。我终于试图使GUI“做一些有趣的事情”。但是,我的代码无法产生预期的结果。我下面有一些相关的代码。

首先,我有一个CharLocStorage类,用于创建哈希图并为哈希图提供一些初始值。

public static class CharLocStorage 
{
    private HashMap<String, Integer> charLocStorage = new HashMap<String, Integer>();
    public HashMap<String, Integer> getCharLocStorageMap() 
    {
        boolean bContainsKey = charLocStorage.containsKey("Han Solo");
        if (bContainsKey == false)
        {
            charLocStorage.put("Han Solo",4);
        }
        boolean bContainsKey2 = charLocStorage.containsKey("Luke Skywalker");
        if (bContainsKey2 == false)
        {
            charLocStorage.put("Luke Skywalker",4);
        }
        return charLocStorage;
    }
}

第二,我有一个类CreatePlanetFrame为行星创建一个JFrame。 (这里有第二个哈希图,但是我永远不需要更改此哈希图中的值。此第二个哈希图可以正常工作,并且与问题无关。)

public static class CreatePlanetFrame
{
    CharLocStorage a = new CharLocStorage();
    public void createPlanetFrame (String sPlanetName)
    {

....

String sLocName = sPlanetName;

                    HashMap<String,Integer> hmap = new HashMap<String,Integer>();

                    /*Adding elements to HashMap*/
                    hmap.put("Bastion",1);
                    hmap.put("Bescane",2);
                    hmap.put("Borosk",3);
                    hmap.put("Dantooine",4);
                    hmap.put("Dubrillion",5);
                    hmap.put("Entralla",6);
                    hmap.put("Garqi",7);
                    hmap.put("Gree",8);
                    hmap.put("Jaemus",9);
                    hmap.put("Kalee",10);
                    hmap.put("Moltok",11);
                    hmap.put("Muunilinst",12);
                    hmap.put("Mygeeto",13);
                    hmap.put("Ord Biniir",14);
                    hmap.put("Ord Trasi",15);
                    hmap.put("Valc",16);
                    hmap.put("Yaga Minor",17);

                int nlocPlanet = hmap.get(sLocName);

                    HashMap<String, Integer> charLocStorage = a.getCharLocStorageMap();

                    /*Adding elements to HashMap*/


                int a;
                String sPersonnel = "";
                for (a=1;a<=2;a++)
                {
                    if (a==1) {sPersonnel = "Han Solo";}
                    if (a==2) {sPersonnel = "Luke Skywalker";}
                    int nlocPersonnel = charLocStorage.get(sPersonnel);
                    if (nlocPersonnel == nlocPlanet)
                    {
                        JPanel panelDA1 = new JPanel();

                        JLabel labelDA1 = new JLabel(sPersonnel);

                        JPanel panelDAA1 = new JPanel();
                        panelDAA1.add(labelDA1);
                        panelDA1.add(panelDAA1);

                        JButton buttonDA1 = new JButton("Move");
                        panelDA1.add(buttonDA1);
                        buttonDA1.addActionListener(new ActionMove());
                        buttonDA1.setName(sPersonnel);

....

第三,我有ActionMove类。

public static class ActionMove implements ActionListener
{
    public void actionPerformed (ActionEvent e) 
    {
        String sCharName = ((JButton)e.getSource()).getName();
        CreateMoveFrame CMF = new CreateMoveFrame();
        CMF.createMoveFrame(sCharName);
    }
}

第四,最后是类CreateMoveFrame。

    public static class CreateMoveFrame
{
    CharLocStorage a = new CharLocStorage();
    public void createMoveFrame (String sCharName)
    {

....

JPanel panel1 = new JPanel(new GridLayout(3,1));

        JLabel label1 = new JLabel("Move "+sCharName+" to ");

        JPanel panel2 = new JPanel();
        panel2.add(label1);
        panel1.add(panel2);

        String[] choices = { "Bastion","Bescane", "Borosk","Dantooine","Dubrillion","Entralla","Garqi","Gree", "Jaemus","Kalee","Moltok","Muunilinst","Mygeeto","Ord Biniir", "Ord Trasi","Valc","Yaga Minor"};

        final JComboBox<String> cb = new JComboBox<String>(choices);

        cb.setVisible(true);
        panel1.add(cb);

        JPanel panel3 = new JPanel();

            JButton button1 = new JButton("MOVE");
            panel3.add(button1);
            //button1.addActionListener(new ActionMoveClick());
            button1.setName(sCharName);

                HashMap<String,Integer> hmap = new HashMap<String,Integer>();

                /*Adding elements to HashMap*/
                hmap.put("Bastion",1);
                hmap.put("Bescane",2);
                hmap.put("Borosk",3);
                hmap.put("Dantooine",4);
                hmap.put("Dubrillion",5);
                hmap.put("Entralla",6);
                hmap.put("Garqi",7);
                hmap.put("Gree",8);
                hmap.put("Jaemus",9);
                hmap.put("Kalee",10);
                hmap.put("Moltok",11);
                hmap.put("Muunilinst",12);
                hmap.put("Mygeeto",13);
                hmap.put("Ord Biniir",14);
                hmap.put("Ord Trasi",15);
                hmap.put("Valc",16);
                hmap.put("Yaga Minor",17);

                String sLocName = cb.getSelectedItem().toString();
                int nLocPlanet = hmap.get(sLocName);

            button1.addActionListener
            (new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                    {
                        a.charLocStorage.put(sCharName,nLocPlanet);
                        HashMap<String,Integer> charLocStorage = a.getCharLocStorageMap();
                        charLocStorage.put(sCharName,nLocPlanet);
                        frame9.dispose();
                    }          
                }
            );

....

除哈希映射中未更新值“ nLocPlanet”外,其他所有内容似乎都可以正常工作。 (Han Solo和Luke Skywalker都出现在Dantooine上,但是我无法让它们移到其他位置,并且可以使用第二个移动按钮同时打印sCharName和nLocPlanet。)

我为帖子的长度表示歉意,我想确保所有相关代码都可见。

(我正在用BlueJ编写代码。我没有任何语法错误。)

1 个答案:

答案 0 :(得分:0)

我发现了问题。我忘了在button1动作侦听器中引用原始哈希图的类。另外,哈希图必须是静态变量。

button1.addActionListener
            (new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                    {
                        HashMap<String,Integer> charLocStorage = a.getCharLocStorageMap();
                        CharLocStorage.charLocStorage.put(sCharName,nLocPlanet);
                        frame9.dispose();
                    }          
                }
            );

public static class CharLocStorage 
{
    private static HashMap<String, Integer> charLocStorage = new HashMap<String, Integer>();
    public HashMap<String, Integer> getCharLocStorageMap() 
    {
        boolean bContainsKey = charLocStorage.containsKey("Han Solo");
        if (bContainsKey == false)
        {
            charLocStorage.put("Han Solo",4);
        }
        boolean bContainsKey2 = charLocStorage.containsKey("Luke Skywalker");
        if (bContainsKey2 == false)
        {
            charLocStorage.put("Luke Skywalker",4);
        }
        return charLocStorage;
    }
}