我想將所見即所得的HTML編輯器添加到我的Java程序中。
我的想法是做一些像this 但不是用python - 用Java。帶Java和And webkit的HTML編輯器 - SWT瀏覽器
我知道的一些選擇,然後他們的問題:
- 一個HTMLEditorKit - 不夠成熟。
- JWebpane - 童話故事。
- QtWebKit - 不是開源的。
- JWebEngine - 不是開源的。
- 中期選舉人 - 簡單。
- Lobo項目 - 不支持contenteditable屬性。
- JavaXPCOM - 我不成功在我的Mac OS X 10.6上操作它。
無論如何,我只是不喜歡使用它,因爲Gecko比webkit慢。
這是這樣,我選擇了從 org.eclipse.swt.browser包帶 Browser Object工作。
現在,我的代碼如下:
代碼中,首先創建一個瀏覽器對象的實例。
之後,它會在body標籤上加載帶有contenteditable='true'
屬性的HTML頁面。
如果它加載一個頁面,它應該將contenteditable='true'
屬性添加到body標籤,並且當它保存頁面時,它應該刪除它。
我的問題是:
- 如何搶編輯HTML代碼?
- 我怎麼知道光標在踩?
- 如何知道某些文字是否突出顯示?
或一般:
- 如何建立與瀏覽器對象和ontenteditable = '真正的' 屬性詞養着?
- 有可能嗎?
- 這是在Java中所見即所得的HTML編輯器的正確方法?
- 任何例子呢?
我已經檢查了SWT Snippets - 沒有多大幫助。
非常感謝。
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class editor {
public static void main(String [] args) {
String html = "<html><title>Editor</title>"
+ "<body contenteditable='true'>"
+ " <h2>All the Page is ditable!!!!!</h2>"
+ "<p>Heres a typical paragraph element</p>"
+ "<ol><li>and now a list</li>"
+ "<li>with only</li>"
+ "<li>three items</li>"
+ "</ol></body></html>";
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser;
try {
browser = new Browser(shell, SWT.BORDER);
}
catch (SWTError e) {
System.out.println(e.getMessage());
display.dispose();
return;
}
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new FillLayout(SWT.ALL));
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
謝謝誇獎 ,更感謝您的答覆! 這似乎是學習c/C++的合適時機... 有了這些語言,它應該更適合這種事情...... – Arnon 2010-11-09 08:27:41