gpt4 book ai didi

java - 检测 WebView 中的超链接悬停并打印链接

转载 作者:行者123 更新时间:2023-11-29 08:34:19 24 4
gpt4 key购买 nike

我想从 WebView 添加悬停的超链接并将其显示在角落中。

我怎样才能实现?它应该像 Chrome 的功能:

Chrome 屏幕截图示例

enter image description here

包含许多链接的示例 WebView 代码:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class core extends Application {
@Override
public void start(final Stage stage) {
Scene scene = new Scene(new Group());

final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);

webEngine.load("http://java2s.com");

scene.setRoot(scrollPane);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

最佳答案

第一件事 - 您的 Scene 应该包含多个元素,而不仅仅是 ScrollPane。您需要一个 TextField 来显示超链接的内容,并且这两个 Control 应该放置在某种 Pane 上。为了处理当鼠标滑过超链接时显示内容,您需要获取所有超链接类型的节点,然后向它们添加适当的监听器。

看看下面的代码,它实现了您想要实现的目标(它部分基于 'JavaFX WebView addHyperlinkListener' article from the 'Inspiration and Expression' blog ):

public class Main extends Application {

@Override
public void start(final Stage stage) {
Scene scene = new Scene(new Group());

final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);

TextField textField = new TextField();
textField.setVisible(false);
textField.setEditable(false);

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.SUCCEEDED) {

EventListener mouseOverEventListener = new EventListener() {
@Override
public void handleEvent(Event ev) {
String href = ((Element) ev.getTarget()).getAttribute("href");
textField.setText(href);
textField.setVisible(true);
textField.setPrefWidth(textField.getText().length() * 6); //
System.out.println(href);
}
};

EventListener mouseOutEventListener = new EventListener() {
@Override
public void handleEvent(Event ev) {
textField.setVisible(false);
}
};

Document document = webEngine.getDocument();
NodeList nodeList = document.getElementsByTagName("a");
for (int i = 0 ; i < nodeList.getLength() ; i++) {
((EventTarget) nodeList.item(i)).addEventListener("mouseover",mouseOverEventListener,false);
((EventTarget) nodeList.item(i)).addEventListener("mouseout",mouseOutEventListener,false);
}
}
});

String content = "http://java2s.com";
webEngine.load(content);

AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().add(scrollPane);
anchorPane.getChildren().add(textField);
AnchorPane.setBottomAnchor(textField, 0.0);
AnchorPane.setLeftAnchor(textField, 0.0);

scene.setRoot(anchorPane);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

关于java - 检测 WebView 中的超链接悬停并打印链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495758/

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