JavaFX标签不会持续更新 [英] JavaFX label will not continuously update

查看:82
本文介绍了JavaFX标签不会持续更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我已成为不断更新的标签问题的牺牲品.在寻找解决方案时,我发现一个答案很多,建议将我的标签绑定到StringProperty,然后每当更改StringProperty时,标签的文本就会随之更改.但是,我无法为自己的一生而努力.

Unfortunately, I have fallen prey to the continuously updating label problem. While searching for a solution, I found an answer with many upvotes that suggested binding my label to a StringProperty, and then whenever that StringProperty is changed, the label's text would subsequently be changed. However, I cannot for the life of me get it to work.

我知道这是某种线程问题.有没有一种使用DataBinding解决方案等解决问题的方法,或者线程是唯一的选择?如果只有线程选择,您能指出我正确的方向吗?我也没有找到使用线程的好解决方案...

I know that it's a threading issue of some sort. Is there a way to solve the problem using a DataBinding solution, etc, or is threading the only option? If threading is the only option, could you point me in the right direction? I haven't found a nice solution using threading either...

任何帮助将不胜感激!

程序说明:下面的程序的所需功能是使标签不断地更新,因为它在for循环中从0-10开始计数.

Program Description: The desired function of the program below is to have the label continuously update as it counts from 0-10 in a for loop.

public class First extends Application {
Stage mainStage;
Scene mainScene;

Button mainButton;
Label mainLabel;

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

@Override
public void start(Stage stage) throws Exception {

    mainStage = stage;
    mainButton = new Button("Begin!");
    mainLabel = new Label("Ready");

    VBox box = new VBox(50);
    box.getChildren().addAll(mainLabel, mainButton);

    mainScene = new Scene(box, 200, 200);
    mainStage.setScene(mainScene);
    mainStage.setTitle("Test Program");
    mainStage.show();

    //Handles Button Press
    mainButton.setOnAction(e -> {
        Second s = new Second();
        mainLabel.textProperty().bind(s.getProperty());
        s.count();
    });
  }
}

这是第二堂课:

public class Second {

private StringProperty strP = new SimpleStringProperty(this, "strProperty", "");

//Get Property
public StringProperty getProperty() {
    return strP;
}

//Get String
public String getString() {
    return strP.get();
}

//Changes StringProperty every 0.25s
public void count() {

    for (int i = 0; i <= 10; i++) {

        this.strP.set(Integer.toString(i));

        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }
  }
}

推荐答案

Java8和JavaFx具有使线程更轻松的新类.您可以使用AnimationTimer或时间轴.此示例使用时间轴.

Java8 and JavaFx have new Classes that make threads easier. You can use AnimationTimer or Timeline. This example uses Timeline.

import javafx.animation.*;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.util.*;

/**
 *
 * @author Sedrick
 */
public class First  extends Application {

    Stage mainStage;
    Scene mainScene;

    Button mainButton;
    Label mainLabel;

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

    @Override
    public void start(Stage stage) throws Exception
    {

        mainStage = stage;
        mainButton = new Button("Begin!");
        mainLabel = new Label("Ready");

        VBox box = new VBox(50);
        box.getChildren().addAll(mainLabel, mainButton);

        mainScene = new Scene(box, 200, 200);
        mainStage.setScene(mainScene);
        mainStage.setTitle("Test Program");
        mainStage.show();

        //Handles Button Press
        mainButton.setOnAction(e -> {
            Second s = new Second();
            mainLabel.textProperty().bind(s.getProperty());
            Timeline timeline = new Timeline(
                    new KeyFrame(Duration.seconds(0),
                            new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            s.setStrP(Integer.toString(Integer.parseInt(s.getStrP()) + 1));//I think you should have used an Integer here.
                        }
                    }
                    ),
                    new KeyFrame(Duration.seconds(1))//Do something every second. In this case we are going to increment setStrP.
            );
            timeline.setCycleCount(10);//Repeat this 10 times
            timeline.play();
        });
    }
}



import javafx.beans.property.*;

public class Second {

    private StringProperty strP = new SimpleStringProperty();

    Second()
    {
        setStrP("0");//set to zero
    }
//Get Property

    public StringProperty getProperty()
    {
        return strP;
    }

//Get String
    public String getStrP()
    {
        return strP.get();
    }

//Changes StringProperty every 0.25s
    public void setStrP(String i)
    {
        this.strP.set(i);
    }
}

这篇关于JavaFX标签不会持续更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆