【发布时间】:2017-05-17 01:50:25
【问题描述】:
我使用 JavaFX 开发了一个应用程序,其中一部分使用了 WebView。但是,我注意到,当用户或通过 JS 以编程方式将 WebView 滚动到底部或接近底部时,似乎会发生奇怪的事情。我特别希望有某种方法来阻止这种行为,并且仍然需要允许两种意义上的滚动。它类似于this question,但它们不一样。我的帖子的其余部分解释了正在发生的事情。
如果您滚动到底部并将窗口调整为比以前更大,并将鼠标悬停在窗口中的其他 JavaFX 控件上,它们会变成白框。
为了说明,假设您有以下窗口:
滚动到底部并将窗口大小调整为比以前更大,然后将鼠标悬停在 JavaFX 按钮和 TextBox 上,这样做时它们都会消失:
这至少发生在带有 Java8u111 和 Java8u51 的 Windows 10 上。我做了一个例子来证明这一点:
example.Main.java
package example;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Example.fxml"));
BorderPane rootWindow = (BorderPane) loader.load();
Scene scene = new Scene(rootWindow);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
example.ExampleController.java
package example;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class ExampleController {
@FXML private WebView webView;
@FXML
private void initialize() {
WebEngine engine = webView.getEngine();
engine.load("https://stackoverflow.com");
engine.getLoadWorker().stateProperty().addListener( (o, oldVal, newVal) -> {
if (newVal == Worker.State.SUCCEEDED) {
engine.executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
});
}
}
example.Example.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.web.WebView?>
<BorderPane prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="example.ExampleController">
<center>
<WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</WebView>
</center>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<TextField HBox.hgrow="ALWAYS" />
<Button alignment="TOP_LEFT" mnemonicParsing="false" text="Button" />
</children>
</HBox>
</bottom>
</BorderPane>
有没有办法阻止这种情况发生或至少减轻它?
【问题讨论】: