0
在窗體中,我嵌入了帶有大量html內容(主要是表格,按鈕)的WebBrowser組件,以實現豐富的UI。點擊html按鈕可以處理事件嗎?處理HTML按鈕代號爲1的事件
在窗體中,我嵌入了帶有大量html內容(主要是表格,按鈕)的WebBrowser組件,以實現豐富的UI。點擊html按鈕可以處理事件嗎?處理HTML按鈕代號爲1的事件
當然,看看廚房水槽演示樣本。
通常只需導航到事件的URL並實現您自己的BrowserNavigationCallback
來處理導航到該特定的URL。
這是從廚房水槽演示通知代碼setBrowserNavigationCallback
塊:
final WebBrowser wb = new WebBrowser();
if(wb.getInternal() instanceof BrowserComponent) {
Button btn = new Button("Add");
final TextArea content = new TextArea();
Container north = new Container(new BorderLayout());
north.addComponent(BorderLayout.CENTER, content);
north.addComponent(BorderLayout.EAST, btn);
cnt.addComponent(BorderLayout.NORTH, north);
content.setHint("Add to web document");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
((BrowserComponent)wb.getInternal()).execute("fnc('" + content.getText() + "');");
}
});
((BrowserComponent)wb.getInternal()).setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(String url) {
if(url.startsWith("http://sayhello")) {
// warning!!! This is not on the EDT and this method MUST return immediately!
Display.getInstance().callSerially(new Runnable() {
public void run() {
((BrowserComponent)wb.getInternal()).execute("fnc('this was written by Java code!')");
}
});
return false;
}
return true;
}
});
}