我已编写代码来在JavaFX应用程序中呈现我的html页面(html页面来自我的本地机器),现在我想在html页面(指定)被修改时重新加载webview。 并且html修改由其他应用程序完成。
任何人都可以告诉我如何重新加载webview。 这是我的代码:
package view;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class ProgressView extends Application {
private Scene scene;
Browser br = new Browser("E:\\Developer-Job\\test.html");
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Web View");
scene = new Scene(br,750,500, Color.web("#666970"));
stage.setScene(scene);
//scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
stage.show();
}
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser(String url) {
//apply the styles
getStyleClass().add("browser");
// load the web page
String strXml = "";
String strBuilt ="";
try {
File f = new File(url);
webEngine.load(f.toURI().toURL().toString());
//add the web view to the scene
getChildren().add(browser);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
@Override protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
}
@Override protected double computePrefWidth(double height) {
return 750;
}
@Override protected double computePrefHeight(double width) {
return 500;
}
}
}
提前致谢。
答案 0 :(得分:1)
如何定期检查更改?
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
import java.io.IOException;
public class ProgressView extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Web View");
Browser br = new Browser("E:\\Developer-Job\\test.html");
Scene scene = new Scene(br, 750, 500, Color.web("#666970"));
stage.setScene(scene);
//scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
stage.show();
}
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser(String url) {
getChildren().add(browser);
//apply the styles
getStyleClass().add("browser");
// load the web page
String strXml = "";
String strBuilt = "";
load(url);
Timeline timeline = new Timeline(new KeyFrame(
Duration.millis(2500),
ae -> load(url)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
private void load(String url) {
try {
File f = new File(url);
webEngine.load(f.toURI().toURL().toString());
//add the web view to the scene
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
@Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
}
@Override
protected double computePrefWidth(double height) {
return 750;
}
@Override
protected double computePrefHeight(double width) {
return 500;
}
}
}