请问有人可以帮助我如何在JavaFX中捕获HTML按钮的onclick事件? 点击那个按钮javascript警告应该显示,我可以在applet窗口上显示HTML页面,但onclick事件不起作用
HTML:
<html lang="en">
<head>
<title>WebView</title>
<link rel="stylesheet" type="text/css" href="help.css">
<script>
function buttonClick() {
alert("Button Clicked");
}
</script>
</head>
<body>
<button onclick="buttonClick()">Submit</button>
</body>
</html>
Bellow是我的JavaFX代码:
public class WebViewSample extends Application {
private Scene scene;
@Override
public void start(Stage stage) {
// create scene
stage.setTitle("WebView");
scene = new Scene(new Browser(), 750, 500, Color.web("#666970"));
stage.setScene(scene);
// apply CSS style
//scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
// show stage
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Browser extends Region {
private HBox toolBar;
private static String[] imageFiles = new String[]{
"help.png"
};
private static String[] captions = new String[]{
"Help"
};
private static String[] urls = new String[]{
WebViewSample.class.getResource("help.html").toExternalForm()
};
final Hyperlink[] hpls = new Hyperlink[captions.length];
final Image[] images = new Image[imageFiles.length];
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
final ComboBox comboBox = new ComboBox();
public Browser() {
//apply the styles
getStyleClass().add("browser");
for (int i = 0; i < captions.length; i++) {
// create hyperlinks
Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]);
Image image = images[i] =
new Image(getClass().getResourceAsStream(imageFiles[i]));
hpl.setGraphic(new ImageView(image));
final String url = urls[i];
hpl.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
webEngine.executeScript("url");
}
});
}
comboBox.setPrefWidth(60);
// create the toolbar
toolBar = new HBox();
toolBar.setAlignment(Pos.CENTER);
toolBar.getStyleClass().add("browser-toolbar");
toolBar.getChildren().add(comboBox);
toolBar.getChildren().addAll(hpls);
toolBar.getChildren().add(createSpacer());
//add components
getChildren().add(toolBar);
getChildren().add(browser);
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
@Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
double tbHeight = toolBar.prefHeight(w);
layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
}
@Override
protected double computePrefWidth(double height) {
return 750;
}
@Override
protected double computePrefHeight(double width) {
return 600;
}
}
答案 0 :(得分:3)
您没有看到任何内容的原因是您尚未为webEngine
定义 // in the constructor of `Browser`
webEngine.setOnAlert((WebEvent<String> event) -> {
System.out.println("ALERT!!!! " + event.getData());
});
处理程序。尝试最简单的事情:
public class Bridge {
public void doSomething() {
...
}
}
这将在标准输出中打印警报。这也意味着事件实际上已被捕获。
如果你想从这个活动中调用Java代码,你必须阅读this (编辑2017/08/06:链接已经消失; this是最接近的我可以找到)。简而言之:
创建一个包含您要调用的代码的对象,例如:
JSObject jsobj = (JSObject) webEngine.executeScript("window");
jsobj.setMember("bridge", new Bridge());
将其放在页面上下文中:
function buttonClick() {
bridge.doSomething();
}
从JS事件处理程序中调用它:
JSObject.removeMember()
一旦您不再需要桥梁将其删除,请使用$( document ).ready(function() {
$(".adicionar").click(function(){
linha = $(".linha:first").clone();
$(".tabela").append("<tr class='linha'>"+$(linha).html()+"</tr>");
remover();
});
});
function remover() {
$(".remover").click(function(){
$(this).parent().parent().remove();
});
}
注意安全!