当前位置: 首页 > article >正文

第三百七十二节 JavaFX教程 - JavaFX HTMLEditor

JavaFX教程 - JavaFX HTMLEditor

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

  • 粗体
  • 斜体
  • 下划线
  • 删除线
  • 字体系列
  • 字体大小
  • 前景色
  • 背景颜色
  • 缩进
  • 项目符号列表
  • 编号列表
  • 对齐
  • 水平线
  • 复制文本片段
  • 粘贴文本片段

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

创建HTML编辑器

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        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);
    }
}

上面的代码生成以下结果。

null

HTML内容

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

htmlEditor.setHtmlText(INITIAL_TEXT);
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar "
            + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        Scene scene = new Scene(htmlEditor);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

格式

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

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
            + "<UL><li>a</li><li>a</li><li>a</li></UL>"
            + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        Scene scene = new Scene(htmlEditor);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null

获取HTML内容

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Main extends Application {
 
    @Override
    public void start(Stage stage) {
        HTMLEditor htmlEditor = new HTMLEditor();
        htmlEditor.setPrefHeight(245);
        String INITIAL_TEXT = "Lorem ipsum dolor sit "
            + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar "
            + "<UL><li>a</li><li>a</li><li>a</li></UL>"
            + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum "
            + "sem.";
        htmlEditor.setHtmlText(INITIAL_TEXT);
        
        Button showHTMLButton = new Button("Produce HTML Code");
        
        showHTMLButton.setOnAction((ActionEvent arg0) -> {
          System.out.println(htmlEditor.getHtmlText());
        });
        
        VBox vbox = new VBox();
        vbox.getChildren().addAll(htmlEditor,showHTMLButton);
        Scene scene = new Scene(vbox);       
        stage.setScene(scene);
        stage.show();
    } 
    public static void main(String[] args) {
        launch(args);
    }
}

上面的代码生成以下结果。

null


http://www.kler.cn/a/570023.html

相关文章:

  • 【HDLbits--FSM续(二)】
  • 搭建iOS逆向开发环境 (下) - 越狱设备与高级工具配置
  • 蓝桥杯4T平台(串口控制LD状态)
  • 【Java项目】基于vue的地方美食分享系统
  • Spring配置文件
  • iPhone 镜像 连接错误
  • 2024 ChatGPT大模型技术场景与商业应用视频精讲合集(45课).zip
  • 51单片机课综合项目
  • 京准电钟:NTP校时服务器于安防监控系统应用方案
  • K8S学习之基础五:k8s中node节点亲和性
  • 创建者——建造者模式
  • 直流减速电机控制实验:实验介绍
  • 机器学习数学通关指南
  • AI赋能企业协作4-NL2Sql技术路线
  • 【uniapp】离线打包uniapp为apk详细步骤
  • 碰一碰矩阵发视频源码:从 NFC 触发到 AI 矩阵剪辑
  • Excel文件中物件PPT文档如何保存到本地
  • MindIE 长文本推理
  • pat甲级1002 A+B for Polynomials
  • netty 与 websocket