我目前正在开发一个Java项目。用户界面使用HTML / JS / CSS编写,并通过JavaFX框架显示。我正在测试Java环境之外的外部浏览器(Firefox和Chrome)以及Java程序本身的特定于接口的功能。
我在JavaFX环境中为元素添加单击侦听器时遇到问题。在写完这篇文章底部的代码之后,我在内部和外部测试了它。它在Chrome和Firefox外部运行良好,但它不能在JavaFX中运行 - >事件在外部正确触发,但不在JavaFX内部。
建立听众的代码:
$(function() {
//set listeners
$(".eventButton").click(function() {
var id = $(this).attr("id");
switch (id) {
case "port_button":
javaOpenWindow('PortWindow','PortWindow');
break;
default:
console.warn("Functionality for button '"+id+"' not implemented");
break;
}
});
console.log("Button listeners set");
}
HTML元素:
<li id="port_button" class='eventButton'><a href='#' >Port Settings</a></li>
<li class='last eventButton' id='advSettings'><a href='#'>Advanced Settings</a></li>
我还没有找到为什么事件不会在JavaFX中触发,据我所知,JavaFX没有抛出任何错误或异常。
作为一个注释,当我在没有JQuery的情况下实现上述内容时,问题仍然存在。
任何人都可以提出解决方案或找到确切问题的方法吗?
这是Java FX代码。它首先使用构造函数初始化某些元素,然后在外部调用initializeWindow方法初始化其他所有元素并打开窗口。
private Stage stage;
private Scene scene;
private WebView webView;
private WebEngine webEngine;
private JavascriptLink js;
private GuiObserver observer;
private String fName;
private int width;
private int height;
private JavascriptLink parentWindow;
private SimpleBooleanProperty webEngineLoadedProperty;
public Window(String fName) {
//create the stage and set attributes
webEngineLoadedProperty = new SimpleBooleanProperty(false);
js = null;
parentWindow = null;
stage = new Stage();
width = 1200;
height = 600;
stage.setWidth(width);
stage.setHeight(height);
observer = null;
this.fName = fName;
}
/**
*
* @param fName filename of html file
* @param wName name for window
* @param width of window
* @param height of window
* @throws IOException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public Window(String fName,int width, int height, GuiObserver o, Stage s) throws IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this(fName);
this.width = width;
this.height = height;
stage = s;
observer = o;
}
public Window(JavascriptLink parent,String fName,int width, int height, GuiObserver o, Stage s) throws IOException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this(fName,width,height,o,s);
parentWindow = parent;
}
/**
* Initialize the window and open it
*/
public void initializeWindow() {
scene = new Scene(new Group());
//load in the appropriate html file to the scene
webView = new WebView();
webEngine = webView.getEngine();
webEngine.setJavaScriptEnabled(true);
webView.setPrefWidth((double)width);
webView.setPrefHeight((double)height);
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
//stage.setTitle(windowName);
webEngineLoadedProperty.set(true);
}
}
});
webEngine.load(this.getClass().getResource(fName).toExternalForm());
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(webView);
scene.setRoot(scrollPane);
/*stage.setWidth((double)width);
stage.setHeight((double)height);*/
stage.setScene(scene);
stage.show();
//add listeners
setListeners();
}
编辑:我已经确定事件监听器实际上是正确的行为,它是返回JavaFX的链接,由于某种原因,它似乎在某个代码之后被破坏了。