gpt4 book ai didi

java - 如何在javafx中重新加载WebView

转载 作者:行者123 更新时间:2023-11-30 06:16:43 26 4
gpt4 key购买 nike

我已经编写了代码来在JavaFX应用程序中渲染我的html页面(html页面来 self 的本地计算机),现在我想在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;
}

}
}

提前致谢。

最佳答案

定期检查更改怎么样?

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;
}

}
}

关于java - 如何在javafx中重新加载WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49043144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com