import javafx.application.Application;
import javafx.event.ActionEvent;
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 HtmlEditorTest extends Application {

    private final String INITIAL_TEXT = "测试文本内容测试文本内容测试文本内容测试文本内容测试文本内容测试文本内容测试文本内容测试文本内容";

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTMLEditor例子");
        stage.setWidth(650);//舞台宽度设置
        stage.setHeight(500);//舞台高度设置
        Scene scene = new Scene(new Group());//创建一个场景挂载节点组容器对象

        VBox root = new VBox();//创建一个垂直盒子布局对象
        root.setPadding(new Insets(8, 8, 8, 8));//设置VBox布局对象中各个元素之间的内边距
        root.setSpacing(5);//设置垂直盒子对象元素之间的空隙距离
        root.setAlignment(Pos.BOTTOM_LEFT);//设置垂直盒子对象的对齐方式

        final HTMLEditor htmlEditor = new HTMLEditor();//创建html富文本编辑器
        htmlEditor.setPrefHeight(245);//设置富文本编辑器的高度
        htmlEditor.setHtmlText(INITIAL_TEXT);//设置富文本编辑器中的初始化内容

        final TextArea htmlCode = new TextArea();//创建TextArea类型对象
        htmlCode.setWrapText(true);//设置TextArea中的文本可以环绕包围

        ScrollPane scrollPane = new ScrollPane();//创建滚动面板对象
        scrollPane.getStyleClass().add("noborder-scroll-pane");//滚动面板对象上面设定无边框样式
        scrollPane.setContent(htmlCode);//给scrollPane对象上添加内容
        scrollPane.setFitToWidth(true);//给scrollPane设置固定宽度
        scrollPane.setPrefHeight(180);//给scrollPane对象上添加高度

        Button showHTMLButton = new Button("生成html代码");
        root.setAlignment(Pos.CENTER);//设置垂直盒子模型对象的对齐模式为居中对齐
        showHTMLButton.setOnAction((ActionEvent arg0) -> {//设置button组件的鼠标点击事件处理回调函数内容
            htmlCode.setText(htmlEditor.getHtmlText());//设置显示文本内容为从htmlEditor.getHtmlText()获取的文本内容
        });

        root.getChildren().addAll(htmlEditor, showHTMLButton, scrollPane);//向垂直盒子布局器中添加htmlEditor,showHTMLButton,scrollPane这三个组件对象
        scene.setRoot(root);//场景上挂载根节点对象为VBox root
        stage.setScene(scene);//舞台上挂载场景
        stage.show();//舞台效果展现播放
    }

    public static void main(String[] args) {
        launch(args);
    }
}


java识别富文本 javafx 富文本编辑器_测试文本