当我在应用程序中将JavaFX应用程序制作为三分之三WebView
时,必须在按+和-键的同时执行缩放功能。代码如下:
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);
}
});
通过此代码,我无法收听按键,但放大和缩小均无法正常工作。请帮忙,因为我必须将鼠标指针更改为双向箭头,然后再听箭头和+和-键进行放大和缩小。
您似乎要WebView
根据事件更改的缩放状态,因此您应该更新自身zoom
持有的属性WebView
。从本示例开始,我EventFilter
根据您的示例添加了以下内容。它似乎按预期工作。
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);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句