Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

java - In WebView, perform zoom in and out function while I press + and - key

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}); 

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...