调用ActionListener时,Hashmap为空

时间:2016-08-15 14:33:37

标签: java hashmap actionlistener

我有一些代码如下:

public class Application {

    public JTextField some_textfield;

    public HashMap<String, Integer> map;

    /*
     * public static main(String[] args)
     *  calls Application()
     */

    public Application() {
        initialize();
        initializeMap();
        System.out.println(map);
        //prints the map {"some_textfield" : 123}
    }

    private void initializeMap() {

        map = new HashMap<String,Integer>();
        map.put(some_textfield.getName(), 123);
        //...
    }

    private void initialize() {
        //...
        some_textfield = new JTextField();
        some_textfield.setName("some_textfield");
        some_textfield.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println(map);
                //map is empty
            }
        });
        //...
    }
}

当我在文本字段中输入内容并触发actionListener时,它会在控制台中打印一个空地图{}

如何解决此问题?

0 个答案:

没有答案