簡體   English   中英

如何在 javafx 中根據時間更改場景

[英]how to change scene based on time in javafx

java 和 JavaFX 的新手,請多多包涵

我需要演示 5 個 3d 水果模型,它們以連續循環顯示,間隔 15 秒:fruit1 15 秒,fruit2 15 秒廣告,依此類推.. 直到fruit5 15 秒,然后返回fruit1 和繼續,直到我按下應該關閉 window 的 ESC 鍵。

我也知道更改構成場景的根組 object 而不是更改場景是理想的,所以我在代碼中進行了更改

我知道需要一個時間線來改變場景中的某些內容,但我已經嘗試過類似於這個答案所說的內容,但我不明白如何切換場景的根每 15 秒

更新:

我放棄了時間線選項,我發現本文中看到的 platform.run 選項似乎有效,因為我看到 window 更新從場景數組中的第一個水果迭代到第二個,但我不知道為什么僅在我需要它每 15 秒運行一次時運行一次,這意味着我的場景切換器:nextSceneIndex() 應該 go 在 1 和 0 之間來回切換。

更新2:

我回到了時間線建議,並實施了 Sedrick 的解決方案,它奏效了……我高興極了:)

這是我的工作代碼!


  public void start(Stage stage) throws Exception {

        BorderPane[] scenes = new BorderPane[]{createSceneApple(),createSceneCarrot(),createSceneTomato()};


        Timeline tl = new Timeline();
        tl.setCycleCount(Animation.INDEFINITE);
        KeyFrame kf_fruit = new KeyFrame(Duration.seconds(10),
                new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent event) {

                       if (index==0){
                           root.getChildren().setAll(scenes[0]);
                           index = 1;
                       }else if(index==1){
                           root.getChildren().setAll(scenes[1]);
                           index = 2;
                       }else if(index==2){
                           root.getChildren().setAll(scenes[2]);
                           index = 0;
                       }
                    }  
        });

        tl.getKeyFrames().add(kf_fruit);
        tl.play();

        Scene scene = new Scene(root, windowWidth, windowHeight);
        stage.setScene(scene);
        stage.show();

}


也許你可以從這里得到一些想法。 這使用了我上面發布的鏈接中的代碼。 Timeline用於循環遍歷Shape列表和有關該形狀的信息。

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        List<MyShape> shapes = new ArrayList();
        shapes.add(new MyShape("Circle", "Shape.Circle", "More Circle Info", new Circle(25, Color.BLUE)));
        shapes.add(new MyShape("Rectangle", "Shape.Rectangle", "More Rectangle Info", new Rectangle(100, 50, Color.RED)));
        shapes.add(new MyShape("Line", "Shape.Line", "More Line Info", new Line(0, 0, 100, 100)));


        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        TextArea ta1 = new TextArea();        
        VBox leftWindow = new VBox(tf1, tf2, ta1);

        StackPane rightWindow = new StackPane(shapes.get(1).getShape());

        AtomicInteger counter = new AtomicInteger();
        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println(counter.get() % shapes.size());
                MyShape currentShape = shapes.get(counter.getAndIncrement() % shapes.size());
                tf1.setText(currentShape.getName());
                tf2.setText(currentShape.getType());
                ta1.setText(currentShape.getMoreInfo());
                rightWindow.getChildren().set(0, currentShape.getShape());
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

        BorderPane root = new BorderPane();
        root.setLeft(new StackPane(leftWindow));
        root.setRight(rightWindow);

        var scene = new Scene(root, 640, 480);
        stage.setScene(scene);
        stage.show();
    }

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

}

更新:如果你只有兩個場景,那會簡化一些事情。 您基本上需要設置初始視圖。 然后,您需要每兩秒切換一次當前顯示的視圖。 (我用了兩秒鍾,這樣你就可以在它們被關閉之前看到視圖)。 我創建了自己的createSceneCarrotcreateSceneApple版本,因為我不知道您的實現。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        BorderPane[] scenes = new BorderPane[]{createSceneApple(),createSceneCarrot()};


        StackPane root = new StackPane(scenes[0]);//Set initial view;      

        Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), (ActionEvent event) -> {
            if(root.getChildren().get(0).equals(scenes[0]))//If the first scene is loaded, load the second scene.
            {
                root.getChildren().set(0, scenes[1]);
            }
            else
            {
                root.getChildren().set(0, scenes[0]);
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

        var scene = new Scene(root, 640, 640);
        stage.setScene(scene);
        stage.show();
    }

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

    public BorderPane createSceneApple()
    {
        BorderPane borderPane = new BorderPane();

        TextField tf1 = new TextField("Rectangle 1");
        TextField tf2 = new TextField("Rectangle Color: Blue");
        TextArea ta1 = new TextArea("20x40");        
        VBox leftWindow = new VBox(tf1, tf2, ta1);
        borderPane.setLeft(leftWindow);

        StackPane rightWindow = new StackPane(new Rectangle(20, 40, Color.BLUE));
        borderPane.setRight(rightWindow);

        return  borderPane;
    }

    public BorderPane createSceneCarrot()
    {
        BorderPane borderPane = new BorderPane();

        TextField tf1 = new TextField("Circle 1");
        TextField tf2 = new TextField("Circle Color: Blue");
        TextArea ta1 = new TextArea("Radius: 50");        
        VBox leftWindow = new VBox(tf1, tf2, ta1);
        borderPane.setLeft(leftWindow);

        StackPane rightWindow = new StackPane(new Circle(50, Color.RED));
        borderPane.setRight(rightWindow);

        return  borderPane;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM