在WebView中,按下+和 - 键时执行放大和缩小功能 [英] In WebView, perform zoom in and out function while I press + and - key

查看:203
本文介绍了在WebView中,按下+和 - 键时执行放大和缩小功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的应用程序中将三个三个 WebView 中的JavaFX应用程序制作成三个时,我必须执行放大和缩小功能,同时按+和 - 键。代码如下:

  StackPane root = new StackPane(); 

HBox hbox = new HBox(30); //创建一个HBox来容纳2个vbox

//创建一个带有垂直增长的textarea的vbox
VBox vbox = new VBox(10);
//标签label1 =新标签();

final WebView img = new WebView();
final WebEngine Img = img.getEngine();
final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);
img.addEventFilter(KeyEvent.KEY_RELEASED,(KeyEvent e) - > {
if(e.getCode()== KeyCode.ADD || e.getCode()== KeyCode.EQUALS || e.getCode()== KeyCode.PLUS){
System.out.println(YES);
zoomProperty.set(zoomProperty.get()* 1.1);
}
else if(e.getCode()== KeyCode.SUBTRACT || e.getCode()== KeyCode.MINUS){
System.out.println(YES);
zoomProperty .set(zoomProperty.get()/ 1.1);
}
});

通过此代码,我可以收听密钥,但放大和缩小功能无效。请帮忙,因为我必须将鼠标指针改为双头箭头然后听箭头和+和 - 键放大和缩小。

解决方案

看起来你想要改变


As I'm making JavaFX application three in three WebView in my application, I have to perform zoom in and out function while I press + and - key. Code is given below:

  StackPane root = new StackPane();

        HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes        

        // create a vbox with a textarea that grows vertically
        VBox vbox = new VBox(10);  
        //Label label1 = new Label("");

        final WebView img = new WebView();
         final WebEngine Img = img.getEngine();
         final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);
 img.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e) -> {
            if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS || e.getCode() == KeyCode.PLUS) {
                System.out.println("YES");
                    zoomProperty.set(zoomProperty.get() * 1.1);
            }
            else if(e.getCode()== KeyCode.SUBTRACT||e.getCode() == KeyCode.MINUS ){
                System.out.println("YES");
                zoomProperty.set(zoomProperty.get() / 1.1);
            }
        });

As through this code im able to listen to the key but zoom in and zoom out is not working. Please help as I have to change mouse pointer to double headed arrow then listen to arrow and + and - key for zoom in and out.

解决方案

It looks you want to alter the zoom state of the WebView based on an event, so you should update the zoom property held by the WebView itself. Starting form this example, I added the following EventFilter based on your example. It appears to work as expected.

webView.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e) -> {
    if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS
            || e.getCode() == KeyCode.PLUS) {
        System.out.println("+");
        webView.setZoom(webView.getZoom() * 1.1);
    }
    else if(e.getCode() == KeyCode.SUBTRACT || e.getCode() == KeyCode.MINUS ){
        System.out.println("-");
        webView.setZoom(webView.getZoom() / 1.1);
    }
}); 

这篇关于在WebView中,按下+和 - 键时执行放大和缩小功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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