原文地址http://download.oracle.com/javafx/2.0/ui_controls/editor.htm
HTMLEditor控件是一個全功能的富文本編輯器。除了基本編輯功能外,它還支持以下特性:
-
文本格式,包括粗體、斜體、下劃線等等
-
段落設置,比如格式、字體、字號
-
前景和背景顏色
-
文本縮進
-
圓點和數字列表
-
文本對齊
-
添加水平標尺
-
複製和粘貼文本塊
Figure 19-1 是一個JavaFX應用中的富文本編輯器。
Figure 19-1 HTML Editor
HTMLEditor
類呈現編輯內容使用的是HTML字符串形式,比如說,Figure 19-1 中的內容呈現以下字符串:"<html><head></head><body contenteditable="true"><h1>Heading</h1><div><u>Text</u>, some text</div></body></html>
."
由於HTMLEditor
類繼承了Node
類,所以可以爲它的實例應用視效和轉換。
添加HTML Editor
和其他UI控件一樣, HTMLEditor組件必須加入場景才能在應用中顯示。可以像
Example 19-1 這樣直接添加,或者通過佈局容器。
Example 19-1 Adding an HTML Editor to a JavaFX Application
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; public class HTMLEditorSample extends Application { @Override public void start(Stage stage) { stage.setTitle("HTMLEditor Sample"); stage.setWidth(400); stage.setHeight(300); final HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); Scene scene = new Scene(htmlEditor); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
編譯運行上面的代碼效果是 Figure 19-2 .
Figure 19-2 Initial View of the HTMLEditor Component
Description of "Figure 19-2 Initial View of the HTMLEditor Component"
實現該組件後就有格式欄,不能關閉它們。不過也可以使用CSS來改變其外觀。見Example 19-2 .
Example 19-2 Styling the HTMLEditor
htmlEditor.setStyle( "-fx-font: 12 cambria;" + "-fx-border-color: brown; " + "-fx-border-style: dotted;" + "-fx-border-width: 2;" );
把它們加入到 Example 19-1 ,效果是Figure 19-3 .
Figure 19-3 Alternative View of the HTMLEditor Component
Description of "Figure 19-3 Alternative View of the HTMLEditor Component"
現在組件的邊框和格式欄的字體改變了。
HTMLEditor
類提供了一個方法來定義應用啓動後編輯區顯示的內容。使用setHtmlText
方法來設置編輯器的初始文本,見Example 19-3。
Example 19-3 Setting the Text Content
private final String INITIAL_TEXT = "<html><body>Lorem ipsum dolor sit " + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar " + "in scelerisque cursus, pulvinar at ante. Nulla consequat" + "congue lectus in sodales. Nullam eu est a felis ornare " + "bibendum et nec tellus. Vivamus non metus tempus augue auctor " + "ornare. Duis pulvinar justo ac purus adipiscing pulvinar. " + "Integer congue faucibus dapibus. Integer id nisl ut elit " + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum " + "sem.</body></html>"; htmlEditor.setHtmlText(INITIAL_TEXT);
Figure 19-4 是使用setHTMLText
方法後的編輯器。
Figure 19-4 HTMLEditor with the Predefined Text Content
Description of "Figure 19-4 HTMLEditor with the Predefined Text Content"
可以在字符串中使用HTML標籤來指定初始顯示的文本格式。
用HTML Editor構建用戶接口
能夠使用
HTMLEditor
控件來實現典型的用戶接口,比如說可以實現消息服務、email客戶端、甚至內容管理系統。
下面實現一個消息排版窗口,在很多email客戶端應用中都可以找到它。
Example 19-4 HTMLEditor Added to the Email Client UI
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; public class HTMLEditorSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Message Composing"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); final VBox root = new VBox(); root.setPadding(new Insets(8, 8, 8, 8)); root.setSpacing(5); root.setAlignment(Pos.BOTTOM_LEFT); final GridPane grid = new GridPane(); grid.setVgap(5); grid.setHgap(10); final ChoiceBox sendTo = new ChoiceBox(FXCollections.observableArrayList( "To:", "Cc:", "Bcc:") ); sendTo.setPrefWidth(100); GridPane.setConstraints(sendTo, 0, 0); grid.getChildren().add(sendTo); final TextField tbTo = new TextField(); tbTo.setPrefWidth(400); GridPane.setConstraints(tbTo, 1, 0); grid.getChildren().add(tbTo); final Label subjectLabel = new Label("Subject:"); GridPane.setConstraints(subjectLabel, 0, 1); grid.getChildren().add(subjectLabel); final TextField tbSubject = new TextField(); tbTo.setPrefWidth(400); GridPane.setConstraints(tbSubject, 1, 1); grid.getChildren().add(tbSubject); root.getChildren().add(grid); final HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(370); root.getChildren().addAll(htmlEditor, new Button("Send")); final Label htmlLabel = new Label(); htmlLabel.setWrapText(true); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
該接口包括一個選項框來選擇接受類型,2個文本框來輸入email地址和主題,一個標籤來顯示主題字段,一個編輯器,還有發送按鈕。
使用Grid
和VBox佈局容器把這些UI控件加入到應用的場景中。編譯運行的效果見
Figure 19-5 ,這是一個用戶正在排版週報。
調用setPrefWidth
或 setPrefHeight方法爲
HTMLEditor對象設置寬或高
,當然根本不指定也行。Example 19-4 中爲組件高度指定了值,而寬度由VBox悲劇容器控制了。當內容文本超出了編輯區的寬度和高度時,垂直滾動條就顯示出來。
獲取HTML內容
用JavaFX HTMLEditor控件,你可以編輯和設置初始內容。此外,你還可以以HTML格式獲得輸入的和編輯的內容。具體實現見
Example 19-5
Example 19-5 Retrieving HTML Code
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class HTMLEditorSample extends Application {
private final String INITIAL_TEXT = "Lorem ipsum dolor sit "
+ "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar "
+ "in scelerisque cursus, pulvinar at ante. Nulla consequat"
+ "congue lectus in sodales. Nullam eu est a felis ornare "
+ "bibendum et nec tellus. Vivamus non metus tempus augue auctor "
+ "ornare. Duis pulvinar justo ac purus adipiscing pulvinar. "
+ "Integer congue faucibus dapibus. Integer id nisl ut elit "
+ "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum "
+ "sem.";
@Override
public void start(Stage stage) {
stage.setTitle("HTMLEditor Sample");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
root.setPadding(new Insets(8, 8, 8, 8));
root.setSpacing(5);
root.setAlignment(Pos.BOTTOM_LEFT);
final HTMLEditor htmlEditor = new HTMLEditor();
htmlEditor.setPrefHeight(245);
htmlEditor.setHtmlText(INITIAL_TEXT);
final TextArea htmlCode = new TextArea();
htmlCode.setWrapText(true);
ScrollPane scrollPane = new ScrollPane();
scrollPane.getStyleClass().add("noborder-scroll-pane");
scrollPane.setContent(htmlCode);
scrollPane.setFitToWidth(true);
scrollPane.setPrefHeight(180);
Button showHTMLButton = new Button("Produce HTML Code");
root.setAlignment(Pos.CENTER);
showHTMLButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent arg0) {
htmlCode.setText(htmlEditor.getHtmlText());
}
});
root.getChildren().addAll(htmlEditor, showHTMLButton, scrollPane);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
getHTMLText方法獲得了編輯內容並以HTML字符串形式呈現。該內容傳遞給文本區,這樣就能查看、複製、粘貼這些HTML代碼。
Figure 19-6 就是樣例。
類似地,也可以獲得HTML代碼比in個保存爲文件或者發送到WebView對象,以同步編輯器和綁定的瀏覽器中的內容。
下面實現了這個任務 Example 19-6 .
Example 19-6 Rendering Edited HTML Content in a Browser
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.scene.web.HTMLEditor; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class HTMLEditorSample extends Application { private final String INITIAL_TEXT = "Lorem ipsum dolor sit " + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar " + "in scelerisque cursus, pulvinar at ante. Nulla consequat" + "congue lectus in sodales. Nullam eu est a felis ornare " + "bibendum et nec tellus. Vivamus non metus tempus augue auctor " + "ornare. Duis pulvinar justo ac purus adipiscing pulvinar. " + "Integer congue faucibus dapibus. Integer id nisl ut elit " + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum " + "sem."; @Override public void start(Stage stage) { stage.setTitle("HTMLEditor Sample"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); root.setPadding(new Insets(8, 8, 8, 8)); root.setSpacing(5); root.setAlignment(Pos.BOTTOM_LEFT); final HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); htmlEditor.setHtmlText(INITIAL_TEXT); final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); ScrollPane scrollPane = new ScrollPane(); scrollPane.getStyleClass().add("noborder-scroll-pane"); scrollPane.setStyle("-fx-background-color: white"); scrollPane.setContent(browser); scrollPane.setFitToWidth(true); scrollPane.setPrefHeight(180); Button showHTMLButton = new Button("Load Content in Browser"); root.setAlignment(Pos.CENTER); showHTMLButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { webEngine.loadContent(htmlEditor.getHtmlText()); } }); root.getChildren().addAll(htmlEditor, showHTMLButton, scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
從htmlEditor
組件獲得HTML代碼加載到WebEngine
對象來指定綁定瀏覽器的內容。每次用戶點擊Load Content in Browser按鈕,編輯的文本就更新到瀏覽器中。Figure 19-7 是Example 19-6 運行的效果。
使用Text
組件來添加非編輯文本內容。到Using Text and Text Effects in JavaFX 瞭解更多Text組件。