2017-06-02 56 views
1

我對javafx非常陌生。我已經在javafx上構建了一個應用程序,它將通過WebView加載index.html頁面。當我點擊index.html中的註冊按鈕時,新的註冊表單頁面將被加載。alert框在JavaScript中不起作用,同時在JAVAFX中驗證html表單

JavaFX代碼:

public void start(final Stage primaryStage) throws Exception { 
    primaryStage.setResizable(false); 
    FrontEndFinal f=new FrontEndFinal(); 

    final WebView browser = new WebView(); 
    final WebEngine webEngine = browser.getEngine(); 
    webEngine.setJavaScriptEnabled(true); 
    Worker<Void> worker = webEngine.getLoadWorker(); 
    worker.stateProperty().addListener(new ChangeListener<State>() { 

     public void changed(ObservableValue<? extends State> observable, // 
       State oldValue, State newValue) { 

      // When load successed. 
      if (newValue == Worker.State.SUCCEEDED) { 

       JSObject jsobj = (JSObject) webEngine.executeScript("window"); 

       jsobj.setMember("myJavaMember", new FrontEndFinal()); 
      } 
     } 
    }); 
    VBox root = new VBox(); 

    root.setMinHeight(500); 
    root.setMinWidth(1250); 


    System.out.println(System.getProperty("user.dir")); 
    String p=System.getProperty("user.dir"); 
    String [] t=p.split("\\\\"); 
    System.out.println(t.length); 
    String final1=t[0]+"/"; 
    for(int i=1;i<t.length;i++) 
    { 
     final1=final1+t[i]+"/"; 
    } 
    System.out.println(final1); 


    browser.getEngine().load("file:///"+final1+"/Html"+"/SMS.html"); 
    root.getChildren().add(browser); 
} 

當我運行這段代碼加載index.html

index.html中

<button class="dropbtn" onclick="location.href = 'Registration.html'"><b><Register></b></button> 

點擊按鈕後,它會打開Registration.html

在Registration.html

<form name="myForm" method="post"> 
Name: <input type="text" name="fname"> 
<button type="button" name="Submit" onclick="abc()">Submit</button> 
<script> 
function abc(){ 
    window.alert("Invalid name"); 
} 

現在我的問題是,當我提交Registration.html表格名稱字段爲空,它不顯示警告框 我GOOGLE了很多網站,如:https://community.oracle.com/thread/2540279] 但沒有工作 任何幫助或建議?

由於事先

回答

1

您需要設置Web引擎的onAlert處理程序。這使您能夠配置您的應用程序如何響應Javascript alert()回調。要顯示一個簡單的對話框消息:

webEngine.setOnAlert(event -> { 
    Alert alert = new Alert(Alert.AlertType.INFORMATION); 
    alert.setContentText(event.getData()); 
    alert.showAndWait(); 
}); 

這裏是一個SSCCE:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Alert; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

public class WebViewAlertTest extends Application { 

    private final String HTML = "" 
      + "<html>" 
      + "<body>" 
      + "<button onclick='alert(\"Javascript Alert\")'>Submit</button>" 
      + "</body>" 
      + "</html>"; 

    @Override 
    public void start(Stage primaryStage) { 
     WebView webView = new WebView(); 
     WebEngine webEngine = webView.getEngine(); 
     webEngine.setOnAlert(event -> { 
      Alert alert = new Alert(Alert.AlertType.INFORMATION); 
      alert.setHeaderText("Message from the web page"); 
      alert.setContentText(event.getData()); 
      alert.showAndWait(); 
     }); 
     webEngine.loadContent(HTML); 

     Scene scene = new Scene(webView, 400, 400); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

嗨@James_D感謝您的答覆...但是,當我上面的代碼運行它顯示錯誤提示事件不能得到解決,以一個變量。 –

+0

@sagnemsagnem代碼工作正常。我添加了一個可以運行的完整示例。 –