有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在javafx中基于时间更改场景

java和JavaFX新手,请耐心听我说

我需要做一个5个3d水果模型的演示,以连续循环的方式显示,间隔15秒:水果1 15秒,然后水果2 15秒,广告等等。。持续15秒直到结果5,然后返回到结果1并继续,直到我按下ESC键,该键应关闭窗口

我也明白,最好是更改组成场景的根组对象,而不是更改场景,所以我在代码中更改了这一点

我知道需要一个时间线,以便在场景播放时改变场景中的某些内容,但我尝试了类似于此answer所述的内容,但我不了解如何每15秒切换场景根的逻辑

更新:

我放弃了时间轴选项,找到了平台。运行选项,如article上所示,当我看到窗口从场景数组中的第一个水果迭代到第二个水果时,它似乎起作用,但我不确定为什么它只在我需要每15秒运行一次时运行一次,这意味着我的场景切换器:nextSceneIndex()应该在1和0之间来回运行

更新2:

我回到时间表的建议,我实施了塞德里克的解决方案,它奏效了。。。我再高兴不过了:)

这是我的工作代码


  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();

}



共 (1) 个答案

  1. # 1 楼答案

    也许你可以从这里得到一些想法。这使用了我在上面发布的链接中的代码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;
        }
    }