JavaFX UI控制元件教程(二十)之HTML Editor
翻譯自 HTML Editor
在本章中,您將學習如何使用嵌入式HTML編輯器編輯JavaFX應用程式中的文字。
該HTMLEditor
控制元件是一個功能齊全的富文字編輯器。它的實現基於HTML5的文件編輯功能,包括以下編輯功能:
-
文字格式包括粗體,斜體,下劃線和樣式
-
段落設定,例如格式,字體系列和字型大小
-
前景色和背景色
-
文字縮排
-
專案符號和編號列表
-
文字對齊
-
新增水平規則
-
複製和貼上文字片段
圖19-1顯示了新增到JavaFX應用程式的富文字編輯器。
圖19-1 HTML編輯器
該HTML
Editor
班於在一個HTML字串的形式編輯內容。例如,圖19-1中編輯器中鍵入的內容由以下字串表示:“ <html><head></head><body contenteditable="true"><h1>Heading</h1><div><u>Text</u>, some text</div></body></html>
。”
因為HTMLEditor
類是類的擴充套件,所以Node
可以將視覺效果或轉換應用於其例項。
新增HTML編輯器
與任何其他UI控制元件一樣,HTMLEditor
必須將元件新增到場景中,以便它可以顯示在應用程式中。您可以將其直接新增到場景中,如示例19-1所示,也可以通過佈局容器將其新增到其他示例中。
示例19-1將HTML編輯器新增到JavaFX應用程式
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); } }
編譯並執行此程式碼片段會生成如圖19-2所示的視窗。
圖19-2 HTMLEditor元件的初始檢視
格式工具欄在元件的實現中提供。您無法切換其可見性。但是,您仍然可以通過應用CSS樣式來自定義編輯器的外觀,如例19-2所示。
例19-2為HTMLEditor設定樣式
htmlEditor.setStyle(
"-fx-font: 12 cambria;"
+ "-fx-border-color: brown; "
+ "-fx-border-style: dotted;"
+ "-fx-border-width: 2;"
);
將此程式碼行新增到示例19-1後,編輯器將更改,如圖19-3所示。
圖19-3 HTMLEditor元件的備用檢視
應用的樣式更改元件的邊框和格式工具欄的字型。
的HTMLEditor
類提供了定義在應用程式啟動時出現在編輯區域中的內容的方法。使用該setHtmlText
方法,如例19-3所示,設定編輯器的初始文字。
示例19-3設定文字內容
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);
圖19-4演示了使用該setHTMLText
方法設定文字的文字編輯器。
圖19-4帶有預定義文字內容的HTMLEditor
您可以使用此字串中的HTML標記為最初呈現的內容應用特定格式。
使用HTML編輯器構建使用者介面
您可以使用該HTMLEditor
控制元件在JavaFX應用程式中實現典型的使用者介面(UI)。例如,您可以實現即時訊息服務,電子郵件客戶端甚至內容管理系統。
顯示可在許多電子郵件客戶端應用程式中找到的訊息編寫視窗的使用者介面。
示例19-4 HTMLEditor已新增到電子郵件客戶端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);
}
}
使用者介面包括用於選擇收件人型別的選擇框,用於輸入電子郵件地址和郵件主題的兩個文字欄位,用於指示主題欄位的標籤,編輯器和傳送按鈕。
UI控制元件通過使用Grid
和VBox
佈局容器排列在應用程式場景中。編譯並執行此應用程式時,圖19-5中顯示的視窗顯示了當使用者編寫每週報告時此應用程式的輸出。
圖19-5電子郵件客戶端使用者介面
您可以HTMLEditor
通過呼叫setPrefWidth
或setPrefHeight
方法設定物件的特定寬度和高度值,也可以不指定其寬度和高度。例19-4指定了元件的高度。其寬度由VBox
佈局容器定義。當文字內容超出編輯區域的高度時,將出現垂直滾動條。
獲取HTML內容
使用JavaFX HTMLEditor
控制元件,您可以編輯文字並設定初始內容。此外,您還可以獲取HTML格式的輸入和編輯文字。例19-5中顯示的應用程式實現了此任務。
示例19-5檢索HTML程式碼
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
呼叫HTMLEditor
物件的方法派生編輯的內容並將其呈現為HTML字串。此資訊將傳遞到文字區域,以便您可以觀察,複製和貼上生成的HTML程式碼。圖19-6顯示了正在HTMLEditor示例中編輯的文字的HTML程式碼。
圖19-6獲取HTML內容
同樣,您可以獲取HTML程式碼並將其儲存在檔案中,或將其傳送到WebView
物件以在編輯器和嵌入式瀏覽器中同步內容。請參見例19-6中如何實現此任務。
示例19-6在瀏覽器中呈現已編輯的HTML內容
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
到指定嵌入式瀏覽器內容的物件中。每次使用者單擊“在瀏覽器中載入內容”按鈕時,編輯的內容都會在瀏覽器中更新。圖19-7演示了例項19-6的實際應用。
圖19-7在瀏覽器中載入內容
您可以使用該Text
元件將非編輯文字內容新增到UI。有關該元件的更多資訊,請參閱在JavaFX中使用文字和文字效果Text
。
相關的API文件