JavaFX动态代码

时间:2018-04-30 19:15:52

标签: java javafx dynamic

我希望在增加i的值后立即出现在标签中 例如:

-in i = 0显示0

-in i = 1显示01

-in i = 2显示012

你能帮助我吗

<span>

4 个答案:

答案 0 :(得分:1)

问题是您在用户界面的线程上更新了标签的值。 JavaFX与一个模型一起工作,在每个&#39; tick&#39; (60帧/秒)。完成所有更新只有在您的事件管理员代码完成后才能看到。

此外,鉴于这是一项长期运行的任务,它将导致无响应的用户界面。

您应该使用Worker来执行长时间运行的任务。有关异步处理,请参阅tutorial。请注意,它不能保证您将看到所有值,因为工作人员可以比用户界面更新更快,系统将合并这些更新。

答案 1 :(得分:0)

您可以使用Timeline来完成此任务。

import java.util.concurrent.atomic.AtomicLong;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication177 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        AtomicLong i = new AtomicLong();

        Label label = new Label();
        Button btn = new Button();

        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.5), (ActionEvent event) -> {//This controls how fast this should run. This example happens every half of a second
            label.setText(label.getText() + Long.toString(i.getAndIncrement()));
        }));
        timeline.setCycleCount(10000000);//This controls the amount of time this should run
        timeline.setOnFinished(event -> {//This tells what to do once cycle count is reached
            btn.setDisable(false);
        });

        btn.setText("Start");
        btn.setOnAction((ActionEvent event) -> {
            btn.setDisable(true);
            timeline.play();
        });

        StackPane stackPane = new StackPane(label);
        VBox root = new VBox(stackPane, new StackPane(btn));
        VBox.setVgrow(stackPane, Priority.ALWAYS);

        Scene scene = new Scene(root, 300, 250);

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

答案 2 :(得分:0)

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test5 extends Application {
    private String text = "";
    private int i;
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        StackPane pane = new StackPane();
        Label lblText = new Label("");
        pane.getChildren().add(lblText);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (i=0;i<10000;i++) {
                        Platform.runLater(new Runnable() { // Run from JavaFX GUI
                            @Override
                            public void run() {
                                lblText.setText(lblText.getText()+i);
                            }
                        });
                        Thread.sleep(200);
                    }
                }
                catch (InterruptedException ex) {

                }
            }
        }).start();
        Scene scene = new Scene(pane, 200, 50);
        primaryStage.setTitle("FlashText"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
}

答案 3 :(得分:0)

Service<Void> service = new Service<Void>() {
        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    //Your First Task #1
                    //Here UI won't be interrupted
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            //Your Second Task After Completion Of First One #2
                        }
                    });
                    return null;
                }
            };
        }
    };
    service.start();
}

#1。您要在后台ex中执行的任务。加载数据必须放在这里。对我来说很好。

#2。一旦后台任务完成,该线程将被执行,因此Ui和后台线程将分别且平稳地运行。

我知道这个答案为时已晚,但我只想分享我所做的这可能有所帮助!