如何在 JavaFX 应用程序中处理 HTML 表单

How to handle HTML form in JavaFX application

有人可以帮助我如何捕获 JavaFX 中 HTML 按钮的 onclick 事件吗? 该按钮的 onClick javascript 警报应该显示,我可以在小程序上显示 HTML 页面 window 但 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>

下面是我的 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;
    }
}

您没有看到任何内容的原因是您没有为 webEngine 定义 onAlert 处理程序。尝试最简单的事情:

    // in the constructor of `Browser`
    webEngine.setOnAlert((WebEvent<String> event) -> {
        System.out.println("ALERT!!!! " + event.getData());
    });

这将在标准输出中打印警报。这也意味着事件实际上被捕获了。

如果您想从此事件调用 Java 代码,您必须阅读 this (EDIT 2017/08/06: the link is gone now; this 是我能找到的最接近的)。简而言之:

  1. 创建一个包含你要调用的代码的对象,例如:

    public class Bridge {
        public void doSomething() {
            ...
        }
    }
    
  2. 放在页面上下文中:

    JSObject jsobj = (JSObject) webEngine.executeScript("window");
    jsobj.setMember("bridge", new Bridge());
    
  3. 从 JS 事件处理程序中调用它:

    function buttonClick() {
        bridge.doSomething();
    }
    
  4. 使用JSObject.removeMember()一旦你不再需要桥将其移除

  5. 注意安全!