从另一个方法获取String [英] Get String from another method

查看:103
本文介绍了从另一个方法获取String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 methods, the first one where i display selected value, index from my JList(list). What i want to do is to send selectedValue - s - to CreateMap method. I tried this code, but s variable is null. Why?

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

I assume you have 2 String variables called s, one global and one local in actionPerformed.

更改行

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

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

它应该有效。确保你有一个名为 String s 的全局变量。

And it should work. Be sure you have a global variable called String s.

这篇关于从另一个方法获取String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆