所以这是运行应用程序的代码:
public class TestJavaFX extends Application {
private Scene scene;
@Override
public void start(Stage stage) throws UnsupportedEncodingException, IOException {
// create the scene
stage.setMinWidth(550);
stage.setMinHeight(400);
stage.setTitle("Dashboard Loading...");
WebView browser = new WebView();
// sets temp title of scene & creates WebView, a JavaFX panel
// Programmatic capabilities (aka DOM/loading pages) are accessed
// through the WebEngine (browser.getEngine())
browser.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) browser.getEngine().executeScript("window");
win.setMember("app", new JavaApp());
HTMLDocument doc = (HTMLDocument) browser.getEngine().getDocument();
stage.setTitle(doc.getTitle());
}
}
});
// adds listener to add app object (from java) to the javascript
// allowing javascript access to java functions
// and also changes the window title to the HTML title
browser.getEngine().load(getClass().getResource("html/index.htm").toString());
// loads the page, resource is configured to run in a jar setting as
// well as in the IDE
StackPane layout = new StackPane();
layout.getChildren().add(browser);
// creates a layout for the scene and adds the webview
scene = new Scene(layout, 900, 650);
new JavaApp().runBatch(5050505);
// initiates the scene (AKA the inner part of the window) and adds a
// layout
// numbers can be changed; they are the size of the window (W,H)
stage.setScene(scene);
stage.show();
// adds scene to stage (window part of window)
// packs stage and makes stage visible
}
public static void main(String[] args) {
launch(args);
}
这是JavaApp类的代码:
public class JavaApp{
public void runBatch(int num) /*throws IOException*/ {
System.out.println(num);
}
}
HTML文件通过一个按钮调用runBatch,该按钮具有onclick属性=“app.runBatch(number)”,其中number可以是0,1,2等。
这就是问题所在:我不知道为什么,但每当runBatch在其定义中有一个参数(例如int num)时,就好像该功能甚至不存在于网页中而且当我点击它时它不会运行HTML按钮。一旦我删除了参数,它运行正常。这是一个错误还是我能解决的问题?