简体   繁体   English

如何从主类访问侦听器中定义的变量?

[英]How to access a variable defined in a Listener from the main class?

I have a problem with a variable in MyFrame class. 我的MyFrame类中的变量有问题。 I want to have in MyFrame class the value of a variable that is defined in a combobox listener. 我想在MyFrame类中具有在组合框侦听器中定义的变量的值。

This is my situation: I have a combobox with some friends' name. 这是我的情况:我有一个带有一些朋友名字的组合框。 I have put a listener to the combobox which has to return the surname of the selected friend. 我已将一个侦听器放入组合框,该组合框必须返回所选朋友的姓氏。 I want to insert the value of surname in a command in MyFrame class, but there are some problems: once setted surname as final (because it has to be used in the Listener), I have an error that say: 我要插入的价值surname在命令MyFrame类,但也存在一些问题:一旦设置好的surname作为final (因为它在监听中使用),我有说,一个错误:

The final local variable surname cannot be assigned, since it is defined in an enclosing type . The final local variable surname cannot be assigned, since it is defined in an enclosing type

What is (or are) the matter(s)? 到底是什么? Here I post my code: 我在这里发布我的代码:

public class MyFrame extends {
public static void main (String[] args)
    {
        //other 
        String [] names = {"john","al","jack"};
        final String surname=null;
        JLabel nameLbl = new JLabel("surname: " + surname);         
        JComboBox box = new JComboBox(names);    
        JPanel centralPnl = new JPanel();
        centralPnl.add(nameLbl);
        centralPnl.add(box);            
        box.addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e) {                 
                if (e.getStateChange() == ItemEvent.SELECTED)
                { 
            // Here operations from database 
            //that return friends' surname under the variable name of "result"
                    surname = result;                       
                }
            }
        });
    }
}

You are trying to reassign a final variable, and thats the problem. 您正在尝试重新分配final变量,这就是问题所在。 Also your final variable needs to be initialised in the first place. 同样,您的final变量也需要首先进行初始化。

There are two things first one is that final variable must be initialized when it is declared and that final variable cannot be reassigned a value. 有两件事情第一个是final变量必须当它宣布被初始化和final变量不能被重新分配的值。

Unfortunately you are doing both of the mistakes. 不幸的是,您同时犯了两个错误。

Another problem is that you should post a Valid code; 另一个问题是您应该发布一个有效的代码。 it will make others finding problems easily. 它将使其他人容易发现问题。

Beyond the issues with the code already pointed out, I guess the question is do you need to store surname or are you just using it to update the label? 除了已经指出的代码问题之外,我想问题是您是否需要存储姓氏还是只是使用它来更新标签?

If you need to store the data, move your surname variable to the class level. 如果需要存储数据,请将您的姓氏变量移到类级别。

If you are simply updating the label, then do something like 如果您只是更新标签,请执行以下操作

nameLbl.setText("surname: " + result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM