如何使用setOnAction里面定义的变量呢?

时间:2017-03-30 08:23:43

标签: java button javafx

我已经在互联网上搜索了解决方案,但似乎没有任何效果。

我想要做的是使用变量" poeni"单击按钮时显示的结果,并显示导致OnActionEvent之外的标签。

以下是我的代码中给我带来麻烦的部分。

dpkg -x

4 个答案:

答案 0 :(得分:1)

您可以将它作为类的一个字段,以便在方法和类的范围内使用它。您可以看到这篇文章Scope of Variables In Java

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

答案 1 :(得分:0)

顾名思义,本地变量是 local 到它们所定义的范围。

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

像:

class Example {
  private int counter = 0;

  void someMethod() {
    Button rezultat = new Button("Pogledajte Vas rezultat");
      rezultat.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            if(odgovor1_1.isSelected()) this.Example.poeni++;
...

但是,当然,这需要仔细考虑事情应该如何在最顶层聚集在一起 - 因为它可能不一定是你想要你的字段的 Example 类。

长话短说 - 局部变量是其范围的本地变量;您需要存在于封闭范围内的某些,以便将此类信息传播到本地范围之外。

答案 2 :(得分:0)

只需将该变量设为类字段。

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);
    }
}

更新

这应该是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()));
    }
}

答案 3 :(得分:0)

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

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

//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);
        }
    });