不活动时如何加载到默认网站

How do I load to default website when inactive

我正在尝试做一个 WebView 浏览网站。当程序处于非活动状态几秒钟时,WebView将加载回网站的开头。

JavaFXApplication13.java

@Override
public void start(Stage stage) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
    stage.setScene(new Scene(root));
    stage.show();        
    Duration duration = Duration.seconds(10);
    PauseTransition transition = new PauseTransition(duration);
    transition.setOnFinished(evt -> inactive());
    stage.addEventFilter(InputEvent.ANY, evt -> transition.playFromStart());
    transition.play();
}

private void inactive(){
    //to investigate if it inactive
    System.out.println("Inactive once");   
    //load other website when inactive ?

}

MainController.java

@FXML
WebView webview;
private WebEngine webEngine;

@Override
public void initialize(URL url, ResourceBundle rb) {
    webEngine = webview.getEngine();
    webview.setContextMenuEnabled(false);
    webEngine.load("http://www.google.com");}

这是我的 main.fxml https://drive.google.com/open?id=1_WnWhkbjaX1s2Y2jI0ojIvc2aQC1njJh

您可以从 controller

访问 webview
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = loader.load();

MainController controller = loader.getController();
controller.loadDefaultSite();

并在 MainController 中创建一个函数

public void loadDefaultSite() {
    webEngine.load("http://www.google.com");
}

示例:

private MainController controller;

@Override
public void start(Stage stage) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
    Parent root = loader.load();
    controller = loader.getController();
    stage.setScene(new Scene(root));
    stage.show();        
    Duration duration = Duration.seconds(10);
    PauseTransition transition = new PauseTransition(duration);
    transition.setOnFinished(evt -> inactive());
    stage.addEventFilter(InputEvent.ANY, evt -> transition.playFromStart());
    transition.play();
}

private void inactive(){
   //check if site is already default... 
   controller.loadDefaultSite();

}