Javafx 不活动时如何加载到默认网站
我正在尝试使用Javafx 不活动时如何加载到默认网站,javafx,fxml,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));
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
您可以从
控制器
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = loader.load();
MainController controller = loader.getController();
controller.loadDefaultSite();
并在main控制器中
生成一个函数
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();
}
它确实有效。非常感谢。