JavaFX WebView:如何更改默认光标? [英] JavaFX WebView: how do I change the default cursor?

查看:138
本文介绍了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:

  • 自动
  • 默认
  • 无人
  • 上下文菜单
  • 帮助
  • 指针
  • 进步
  • 等待
  • 单元格
  • 十字准线
  • 文本
  • 垂直文本
  • 别名
  • 复制
  • 移动
  • 不掉线
  • 不允许
  • 电子调整大小
  • n调整大小
  • ne-resize
  • nw-resize
  • s调整大小
  • 调整大小
  • sw-resize
  • 调整大小
  • ew-resize
  • ns调整大小
  • nesw-resize
  • nwse调整大小
  • col-resize
  • 行调整大小
  • 全滚动
  • 放大
  • 缩小
  • auto
  • default
  • none
  • context-menu
  • help
  • pointer
  • progress
  • wait
  • cell
  • crosshair
  • text
  • vertical-text
  • alias
  • copy
  • move
  • no-drop
  • not-allowed
  • grab
  • grabbing
  • e-resize
  • n-resize
  • ne-resize
  • nw-resize
  • s-resize
  • se-resize
  • sw-resize
  • w-resize
  • ew-resize
  • ns-resize
  • nesw-resize
  • nwse-resize
  • col-resize
  • row-resize
  • all-scroll
  • zoom-in
  • zoom-out

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆