0

我的 GUI 中有一个组合框和一个按钮。当用户单击按钮时,我试图返回组合框中的值。但是,该行setOnAction一直以错误突出显示local variables referenced from a lambda expression must be final or effectively final

public class ComboBoxGui
{
    public String show(String msg)
    {
        ...
        
        ObservableList<String> opts = 
            FXCollections.observableArrayList("A", "B", "C");
        ComboBox myComboBox = new ComboBox(opts);
        Button okBtn = new Button("OK");

        ...

        String result;
        okBtn.setOnAction(evt -> {
            result = typeComboBox.getValue().toString(); // this line keeps getting highlighted
        });
    }
}

我尝试设置String result为 final,将我的变量myComboBox作为私有实例,或者创建一个新方法来派生文本值等,但它似乎无法解决手头的错误。

感谢提供的任何帮助。

4

0 回答 0