Java 如何在setOnAction外部使用setOnAction内部定义的变量?

Java 如何在setOnAction外部使用setOnAction内部定义的变量?,java,button,javafx,Java,Button,Javafx,我在互联网上到处寻找解决办法,但似乎什么都不管用 我想做的是使用变量“poeni”,当单击按钮时会对其进行修改,并显示一个在OnActionEvent之外的标签 这是我代码中给我带来麻烦的部分 //The button Button rezultat = new Button("Pogledajte Vas rezultat"); rezultat.setOnAction(new EventHandler<ActionEvent>() {

我在互联网上到处寻找解决办法,但似乎什么都不管用

我想做的是使用变量“poeni”,当单击按钮时会对其进行修改,并显示一个在OnActionEvent之外的标签

这是我代码中给我带来麻烦的部分

//The button
        Button rezultat = new Button("Pogledajte Vas rezultat");
        rezultat.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                int poeni = 0; //The variable i want to use outside

                if(odgovor1_1.isSelected()) poeni++;
                if(odgovor2_3.isSelected()) poeni++; //Theese if's check if the RadioButton is checked and increment if it is
                if(odgovor3_2.isSelected()) poeni++;
            }
        });


        //The label in which I want to display the results
        Label lbl_rezultat = new Label("Vas rezultat je: " + poeni);
//按钮
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
int poeni=0;//我要在外部使用的变量
if(odgovor1_1.isSelected())poeni++;
if(odgovor2_3.isSelected())poeni++;//使用if检查单选按钮是否选中,如果选中则递增
if(odgovor3_2.isSelected())poeni++;
}
});
//要在其中显示结果的标签
标签lbl_rezultat=新标签(“Vas rezultat je:+poeni”);

顾名思义,local变量在其定义的范围内是局部的

因此,当您希望某个匿名内部类的实例具有与封闭类通信的手段时,必须在该外部类上使用字段

比如:

类示例{
专用整数计数器=0;
void方法(){
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
if(odgovor1_1.isSelected())this.Example.poeni++;
...
但是,当然,这需要仔细考虑在顶层应该如何组合,因为它可能不一定是您希望字段所在的示例类


长话短说-局部变量是其作用域的局部变量;为了将此类信息传播到局部作用域之外,您需要存在于封闭作用域中的某些内容。

只需将该变量设置为类字段即可

public class MyClass
{
    private int poeni;

    public MyClass()
    {
        Button rezultat = new Button("Pogledajte Vas rezultat");
        rezultat.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MyClass.this.poeni = 0;

                if(odgovor1_1.isSelected()) MyClass.this.poeni++;
                if(odgovor2_3.isSelected()) MyClass.this.poeni++;
                if(odgovor3_2.isSelected()) MyClass.this.poeni++;
            }
        });


        //The label in which I want to display the results
        Label lbl_rezultat = new Label("Vas rezultat je: " + this.poeni);
    }
}
公共类MyClass
{
普瓦尼私人酒店;
公共MyClass()
{
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
MyClass.this.poeni=0;
如果(odgovor1_1.isSelected())MyClass.this.poeni++;
if(odgovor2_3.isSelected())MyClass.this.poeni++;
如果(odgovor3_2.isSelected())MyClass.this.poeni++;
}
});
//要在其中显示结果的标签
标签lbl_rezultat=新标签(“Vas rezultat je:+this.poeni”);
}
}
更新: 这应该是JavaFX风格的正确方法:

public class MyClass
{
    private final IntegerProperty poeni = new SimpleIntegerProperty(0);
    public IntegerProperty poeniProperty()
    {
        return this.poeni;
    }
    public int getPoeni()
    {
        this.poeniProperty().get();
    }
    public void setPoeni(int value)
    {
        this.poeniProperty().set(value);
    }

    public MyClass()
    {
        Button rezultat = new Button("Pogledajte Vas rezultat");
        rezultat.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                int count = 0;

                if(odgovor1_1.isSelected()) count++;
                if(odgovor2_3.isSelected()) count++;
                if(odgovor3_2.isSelected()) count++;
                MyClass.this.setPoeni(count);
            }
        });


        //The label in which I want to display the results
        Label lbl_rezultat = new Label();
        lbl_rezultat.textProperty.bind(Bindings.createStringBinding(() ->
            "Vas rezultat je: " + Integer.toString(MyClass.this.getPoeni()),
            MyClass.this.poeniProperty()));
    }
}
公共类MyClass
{
private final IntegerProperty poeni=新的SimpleIntegerProperty(0);
公共集成属性poeniProperty()
{
把这个还给我;
}
public int getPoeni()
{
this.poeniProperty().get();
}
公共void setPoeni(int值)
{
this.poeniProperty().set(值);
}
公共MyClass()
{
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
整数计数=0;
如果(odgovor1_1.isSelected())计数++;
如果(odgovor2_3.isSelected())计数++;
如果(odgovor3_2.isSelected())计数++;
MyClass.this.setPoeni(count);
}
});
//要在其中显示结果的标签
Label lbl_rezultat=新标签();
lbl_rezultat.textProperty.bind(Bindings.createStringBinding(()->
Vas rezultat je:“+Integer.toString(MyClass.this.getPoeni()),
MyClass.this.poeniProperty());
}
}

您可以将它作为类的一个字段,以便在类的方法和范围中使用它

class-Rezultat{
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
int poeni;//poeni的范围从这里开始
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
poeni=0;
if(odgovor1_1.isSelected())poeni++;
if(odgovor2_3.isSelected())poeni++;//使用if检查单选按钮是否选中,如果选中则递增
if(odgovor3_2.isSelected())poeni++;
}
});
标签lbl_rezultat=新标签(“Vas rezultat je:+poeni”);
}//您可以在这里使用变量

您需要创建一个机制,以便使用变量的值更新
标签。您可以创建一个方法并接受
int
参数。更新
poeni
时,您只需使用更新的值调用该方法。在该方法中,您可以更新
标签

另一种方法是简单地在上面定义标签并将其标记为final。您可以始终在标签上设置文本

//The button
    Button rezultat = new Button("Pogledajte Vas rezultat");
    //The label in which I want to display the results
    final  Label lbl_rezultat = new Label();
    rezultat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            int poeni = 0; //The variable i want to use outside

            if(odgovor1_1.isSelected()) poeni++;
            if(odgovor2_3.isSelected()) poeni++; //Theese if's check if the RadioButton is checked and increment if it is
            if(odgovor3_2.isSelected()) poeni++;
            lbl_rezultat.setText("Vas rezultat je: " + poeni);
        }
    });
//按钮
Button rezultat=新按钮(“Pogledajte Vas rezultat”);
//要在其中显示结果的标签
最终标签lbl_rezultat=新标签();
setOnAction(新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent事件){
int poeni=0;//我要在外部使用的变量
if(odgovor1_1.isSelected())poeni++;
if(odgovor2_3.isSelected())poeni++;//使用if检查单选按钮是否选中,如果选中则递增
if(odgovor3_2.isSelected())poeni++;
lbl_rezultat.setText(“Vas rezultat je:+poeni”);
}
});

即使变量在外部可见,这也不起作用。通常,在触发偶数时,操作处理程序会在很久以后被调用。但这不太可能(至少)
class Rezultat{
    Button rezultat = new Button("Pogledajte Vas rezultat");
    int poeni;//The scope of poeni starts form here
    rezultat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            poeni = 0;

            if(odgovor1_1.isSelected()) poeni++;
            if(odgovor2_3.isSelected()) poeni++; //Theese if's check if the RadioButton is checked and increment if it is
            if(odgovor3_2.isSelected()) poeni++;
        }
    });
    Label lbl_rezultat = new Label("Vas rezultat je: " + poeni);
  }//You can use variable to here
//The button
    Button rezultat = new Button("Pogledajte Vas rezultat");
    //The label in which I want to display the results
    final  Label lbl_rezultat = new Label();
    rezultat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            int poeni = 0; //The variable i want to use outside

            if(odgovor1_1.isSelected()) poeni++;
            if(odgovor2_3.isSelected()) poeni++; //Theese if's check if the RadioButton is checked and increment if it is
            if(odgovor3_2.isSelected()) poeni++;
            lbl_rezultat.setText("Vas rezultat je: " + poeni);
        }
    });