我对 WebView 中的滚动条有问题。我想在加载 web 视图时,滚动条自动滚动到 web 视图的末尾。我该怎么做?
这是我的样本
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;
public class Controller implements Initializable {
@FXML
WebView webView;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
loadWebView();
}
private void loadWebView() {
webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");
StringBuilder html= new StringBuilder().append("<html><body>");
for (int i=0; i<100; i++) {
html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");
}
html.append("</body></html>");
Set<Node> nodes = webView.lookupAll(".scroll-bar");
for (Node node : nodes) {
if (ScrollBar.class.isInstance(node)) {
System.out.println("Scrollbar here!!!");
ScrollBar scroll = (ScrollBar) node;
scroll.setValue(scroll.getMax());
}
}
WebEngine webEngine = webView.getEngine();
webEngine.loadContent(html.toString());
}
}
谢谢 4 你的阅读