更新JavaFX阶段的大小和位置不起作用

时间:2019-07-01 14:32:43

标签: java swing javafx

我是JavaFX的初学者,我对swing / JFrames有所了解,可以使用validate方法更新屏幕。 JavaFX库中是否存在等效的阶段方法?当我在以下代码中执行Thread.sleep()并更新舞台的大小和位置时,舞台会在时间过去后加载,然后立即移动。这不是我的意图。我希望加载舞台,等待时间,然后更新位置和大小。这到底是怎么了?谢谢!

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Driver extends Application
{    
    public static void main(String [] args) throws Exception
    {
        launch(args);      
    }

    @Override
    public void start(Stage stage) throws Exception 
    {             
        // Create the WebView
        WebView webView = new WebView();

        // Create the WebEngine
        final WebEngine webEngine = webView.getEngine();

        // LOad the Start-Page
        webEngine.load("https://www.reddit.com");

        // Update the stage title when a new web page title is available
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() 
        {
            public void changed(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) 
            {
                if (newState == Worker.State.SUCCEEDED) 
                {
                    //stage.setTitle(webEngine.getLocation());
                    stage.setTitle(webEngine.getTitle());
                }
            }
        });

        // Create the VBox
        VBox root = new VBox();

        // Add the WebView to the VBox
        root.getChildren().add(webView);

        // Create the Scene
        Scene scene = new Scene(root);

        // Add  the Scene to the Stage
        stage.setWidth(200);
        stage.setHeight(200);
        stage.setScene(scene);
        stage.setX(500);
        stage.setY(500);
        stage.show();
        Thread.sleep(5000);
        stage.setX(800);
        stage.setY(200);
        stage.setWidth(60);
        stage.setHeight(60);
   }
}

1 个答案:

答案 0 :(得分:0)

PauseTransition应该可以工作。

  

替换:

Thread.sleep(5000);
stage.setX(800);
stage.setY(200);
stage.setWidth(60);
stage.setHeight(60);
  

使用:

PauseTransition wait = new PauseTransition(Duration.seconds(5));
wait.setOnFinished((e) -> {
    /*YOUR METHOD*/
    stage.setX(800);
    stage.setY(200);
    stage.setWidth(60);
    stage.setHeight(60);
});
wait.play();