简体   繁体   English

Javafx Gui更新

[英]Javafx Gui update

My problem is basically from Java language which I cannot figure out. 我的问题基本上是来自Java语言,我无法弄清楚。 This is just a basic code to help me understand the concept of updating GUI using Platform.runlater method. 这只是一个基本代码,可以帮助我了解使用Platform.runlater方法更新GUI的概念。

package practise;

import java.util.Random;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Practise extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        final Line l1 = new Line(200,80,200,0);
        final int c = 0;        

        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");

                Platform.runLater(new Runnable() {
                    Random rn = new Random();
                    @Override

                    public void run() {
                        if(c==0)
                        {
                            l1.setStartX(rn.nextInt(400));
                            l1.setStartY(rn.nextInt(400));
                            c = 1;
                        }
                        else
                        {
                            l1.setEndX(rn.nextInt(400));
                            l1.setEndY(rn.nextInt(400));
                            c = 0;
                        }
                    }
                });
            }
        });


        l1.setStroke(Color.YELLOW);
        l1.setStrokeWidth(2);

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        root.getChildren().add(l1);

        Scene scene = new Scene(root, 400, 400, Color.WHITE);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

This is my code. 这是我的代码。 I wish to alternatingly change either the start point or the end points of the lines. 我希望交替更改线条的起点或终点。 For that I created a variable 'c' which alternates it's values every time I press the button. 为此,我创建了一个变量“ c”,每次按下按钮时都会交替使用它的值。

But the issue is that the compiler forces me to change int c into final. 但是问题是编译器强迫我将int c更改为final。 Which means it becomes a constant and no longer serves my purpose. 这意味着它变成一个常数,不再符合我的目的。

Why is this happening and how can I get around it? 为什么会发生这种情况,我该如何解决?

You're using a variable with local scope in an anonymous inner class. 您正在匿名内部类中使用具有局部范围的变量。 You can only do this if the variable is final . 您只能在变量为final执行此操作。 If you need to change the value of the variable, you could define it as an instance variable. 如果需要更改变量的值,则可以将其定义为实例变量。

public class MainApp extends Application {

    private int c;

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        final Line l1 = new Line(200,80,200,0);

        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");

                Platform.runLater(new Runnable() {
                    Random rn = new Random();
                    @Override

                    public void run() {
                        if(c==0)
                        {
                            System.out.println("c=" + c);
                            l1.setStartX(rn.nextInt(400));
                            l1.setStartY(rn.nextInt(400));
                            c = 1;
                        }
                        else
                        {
                            System.out.println("c=" + c);
                            l1.setEndX(rn.nextInt(400));
                            l1.setEndY(rn.nextInt(400));
                            c = 0;
                        }
                    }
                });
            }
        });


        l1.setStroke(Color.YELLOW);
        l1.setStrokeWidth(2);

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        root.getChildren().add(l1);

        Scene scene = new Scene(root, 400, 400, Color.WHITE);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Output after a couple of button clicks: 几次按钮单击后的输出:

Hello World! 你好,世界!
c=0 c = 0
Hello World! 你好,世界!
c=1 c = 1
Hello World! 你好,世界!
c=0 c = 0

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

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