关于javafx中的PauseTransition [英] About PauseTransition in javafx

查看:217
本文介绍了关于javafx中的PauseTransition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个VBox(根),并在其中添加了一些Button。当我单击带有文本点击的按钮(button_to_click)时,其他十个按钮(包含十个元素的按钮阵列)会将背景颜色更改为红色。我想每个按钮每秒更改其背景颜色。我使用PauseTransition来执行此操作,但是没有用。这是我的代码

I created a VBox (root) and added some Button in it. When I click the button with text "Click" (button_to_click), ten other button (an button array with ten elements) will change background color into 'red'. I want per button change its backgr color per second. I used PauseTransition to do this but it didn't work. Here are my code

package sample;

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.util.Duration;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{

        VBox root = new VBox();

        Button button_to_click = new Button("Click");

        Button[] buttons = new Button[10];

        root.getChildren().add(button_to_click);

        for(int i = 0; i <= 9; i++){
            buttons[i] = new Button(""+i);
            root.getChildren().add(buttons[i]);
        }

        button_to_click.setOnAction(e->{
            for(int i = 0; i <= 9; i++){
                buttons[i].setStyle("-fx-background-color:red");
                PauseTransition pause = new PauseTransition(Duration.seconds(1));
                pause.play();
            }
        });


        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

所有按钮同时更改其背景颜色,那不是我想要的

All button change its background color at the same time, that isn't what I want.

推荐答案

您将PauseTransition视为Thread.sleep调用。但是PauseTransition不能那样工作。

You are treating PauseTransition like it’s a Thread.sleep call. But PauseTransition does not work like that.

即使相同,睡眠调用也无法满足您的需求。像大多数用户界面工具包一样,JavaFX是单线程的,因此进行睡眠调用会使应用程序挂起。

And even if it were the same, a sleep call would not do what you want. JavaFX, like most user interface toolkits, is single threaded, so a sleep call would hang the application.

暂停转换发生在后台线程中。您想要的是在暂停时更改按钮的颜色完成

The pause transition occurs in a background thread. What you want is to change a button’s color when the pause finishes:

button_to_click.setOnAction(e -> {
    for (int i = 0; i <= 9; i++) {
        Button button = buttons[i];

        PauseTransition pause = new PauseTransition(Duration.seconds(i));
        pause.setOnFinished(
            f -> button.setStyle("-fx-background-color:red"));
        pause.play();
    }
});

请注意,我已将PauseTransition的持续时间从 seconds(1)秒更改 seconds(i)。这不是最有效的方法,但是它需要对现有代码进行最少的更改。会导致在 i 秒后调用每个按钮的 setStyle 方法。

Notice that I have changed the PauseTransition’s duration from seconds(1) to seconds(i). This isn’t the most efficient approach, but it requires the fewest changes to your existing code. It will cause each button’s setStyle method to be invoked after i seconds have passed.

这篇关于关于javafx中的PauseTransition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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