如何更改默认光标

如何更改默认光标

本文介绍了JavaFX WebView:如何更改默认光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改WebView的默认光标?我所做的每一个更改都会被忽略,该图标始终会恢复为默认指针.

How do I change WebView's default cursor? Every change I make is ignored, the icon always reverts back to the default pointer.

示例:

import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        webView.getEngine().loadContent("http://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        scene.setCursor(Cursor.CLOSED_HAND); // Doesn't work, reverted to pointer
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我还试图更改webView光标本身,但无济于事.

I also tried to change the webView cursor itself, but to no avail.

推荐答案

文档的HTML内容定义了光标,因此您可以在文档加载后修改其正文样式:

The document’s HTML content defines the cursor, so you can modify the document’s body style after it loads:

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewCursorOverride extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        webView.getEngine().getLoadWorker().stateProperty().addListener(
            (o, old, state) -> {
                if (state != Worker.State.SUCCEEDED) {
                    return;
                }

                Document doc = webView.getEngine().getDocument();
                Element body = (Element)
                    doc.getElementsByTagName("body").item(0);
                String style = body.getAttribute("style");
                body.setAttribute("style", "cursor: grab;" + style);
            });
        webView.getEngine().load("https://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

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

您还可以根据图像创建自己的光标:

You can also create your own cursor from an image:

body.setAttribute("style",
    "cursor: url('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Pixel_51_icon_cursor_click_top_right.svg/36px-Pixel_51_icon_cursor_click_top_right.svg.png') 27 9, default;" + style);

cursor CSS属性的完整定义是在此处.这是当前的预定义游标列表;请注意,并非每个系统都支持所有这些功能:

The full definition of the cursor CSS property is here. Here is the current list of predefined cursors; note that not all of them are supported on every system:

这篇关于JavaFX WebView:如何更改默认光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!