从另一个方法获取字符串

Get String from another method

我有 2 种方法,第一种方法显示所选值,来自我的 JList(list) 的索引。我想要做的是将 selectedValue - s - 发送到 CreateMap 方法。我试过这段代码,但 s 变量为空。为什么?

public void actionPerformed(ActionEvent e)
    {
        int index = 0;

        if(e.getActionCommand().equals("Check")){ //if button is pressed

            index = list.getSelectedIndex();
            System.out.println("Index selected" + index);
            String s = (String) list.getSelectedValue();
            System.out.println("Value Selected " +s);

            createMap();

    }

}


     private Map<String, Integer>createMap()
     {
         Map<String, Integer> graphicsMap = new HashMap<>();

         for(LaneInformation l:graphics.laneInfos )
         {
             if (l.getEllipse().contains(graphics.startX, graphics.startY)) {
                 graphicsMap.put(this.s, graphics.startX);


             }

         }

         return graphicsMap;     
     }

我假设您有 2 个名为 s 的字符串变量,一个全局变量和一个局部变量 actionPerformed

换行

String s = (String) list.getSelectedValue();

this.s = (String) list.getSelectedValue();

它应该可以工作。确保你有一个名为 String s.

的全局变量