HTMLEditor控件是一个富文本编辑器,具有以下功能。

  • 粗体
  • 斜体
  • 下划线
  • 删除线
  • 字体系列

  • 字体大小

  • 前景色

  • 背景颜色
  • 缩进
  • 项目符号列表
  • 编号列表
  • 对齐
  • 水平线
  • 复制文本片段
  • 粘贴文本片段

HTMLEditor类返回HTML字符串中的编辑内容。

创建HTML编辑器

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.web.HTMLEditor;
  4. import javafx.stage.Stage;
  5. public class Main extends Application {
  6. @Override
  7. public void start(Stage stage) {
  8. HTMLEditor htmlEditor = new HTMLEditor();
  9. htmlEditor.setPrefHeight(245);
  10. Scene scene = new Scene(htmlEditor);
  11. stage.setScene(scene);
  12. stage.show();
  13. }
  14. public static void main(String[] args) {
  15. launch(args);
  16. }
  17. }

image.png

HTML内容

要将内容设置为HTMLEditor类,请使用setHtmlText方法。

  1. htmlEditor.setHtmlText(INITIAL_TEXT);
  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.web.HTMLEditor;
  4. import javafx.stage.Stage;
  5. public class Main extends Application {
  6. @Override
  7. public void start(Stage stage) {
  8. HTMLEditor htmlEditor = new HTMLEditor();
  9. htmlEditor.setPrefHeight(245);
  10. String INITIAL_TEXT = "Lorem ipsum dolor sit "
  11. + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar "
  12. + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum "
  13. + "sem.";
  14. htmlEditor.setHtmlText(INITIAL_TEXT);
  15. Scene scene = new Scene(htmlEditor);
  16. stage.setScene(scene);
  17. stage.show();
  18. }
  19. public static void main(String[] args) {
  20. launch(args);
  21. }
  22. }

image.png

格式

我们可以使用此字符串中的HTML标记为最初渲染的内容应用特定的格式。

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.web.HTMLEditor;
  4. import javafx.stage.Stage;
  5. public class Main extends Application {
  6. @Override
  7. public void start(Stage stage) {
  8. HTMLEditor htmlEditor = new HTMLEditor();
  9. htmlEditor.setPrefHeight(245);
  10. String INITIAL_TEXT = "Lorem ipsum dolor sit "
  11. + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
  12. + "<UL><li>a</li><li>a</li><li>a</li></UL>"
  13. + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
  14. + "sem.";
  15. htmlEditor.setHtmlText(INITIAL_TEXT);
  16. Scene scene = new Scene(htmlEditor);
  17. stage.setScene(scene);
  18. stage.show();
  19. }
  20. public static void main(String[] args) {
  21. launch(args);
  22. }
  23. }

image.png

获取HTML内容

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.layout.VBox;
  6. import javafx.scene.web.HTMLEditor;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. @Override
  10. public void start(Stage stage) {
  11. HTMLEditor htmlEditor = new HTMLEditor();
  12. htmlEditor.setPrefHeight(245);
  13. String INITIAL_TEXT = "Lorem ipsum dolor sit "
  14. + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
  15. + "<UL><li>a</li><li>a</li><li>a</li></UL>"
  16. + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
  17. + "sem.";
  18. htmlEditor.setHtmlText(INITIAL_TEXT);
  19. Button showHTMLButton = new Button("Produce HTML Code");
  20. showHTMLButton.setOnAction((ActionEvent arg0) -> {
  21. System.out.println(htmlEditor.getHtmlText());
  22. });
  23. VBox vbox = new VBox();
  24. vbox.getChildren().addAll(htmlEditor,showHTMLButton);
  25. Scene scene = new Scene(vbox);
  26. stage.setScene(scene);
  27. stage.show();
  28. }
  29. public static void main(String[] args) {
  30. launch(args);
  31. }
  32. }

image.png