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

时间:2019-06-14 02:49:18

标签: javafx fxml

我正在尝试做一个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

1 个答案:

答案 0 :(得分:2)

您可以从controller进入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();

}

示例:

var things = require("./someThings");
let result = getResult();
async function getResult() {
    return await things.getSomeThingsAsync();
}