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

第三百七十二节 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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.lryc.cn/news/545716.html

相关文章:

  • 蓝桥杯试题:DFS回溯
  • Lua | 每日一练 (4)
  • 每日一题——接雨水
  • java常见面试01
  • 算法-二叉树篇27-把二叉搜索树转换为累加树
  • C语言:51单片机 基础知识
  • olmOCR:使用VLM解析PDF
  • 数据结构(初阶)(七)----树和二叉树(堆,堆排序)
  • 图像分类项目1:基于卷积神经网络的动物图像分类
  • Kali Linux 2024.4版本全局代理(wide Proxy)配置,适用于浏览器、命令行
  • [Windows] 批量为视频或者音频生成字幕 video subtitle master 1.5.2
  • 不要升级,Flutter Debug 在 iOS 18.4 beta 无法运行,提示 mprotect failed: Permission denied
  • 介绍 torch-mlir 从 pytorch 生态到 mlir 生态
  • upload
  • InterHand26M(handposeX-json 格式)数据集-release >> DataBall
  • [Java基础] JVM常量池介绍(BeanUtils.copyProperties(source, target)中的属性值引用的是同一个对象吗)
  • `maturin`是什么:matu rus in python
  • spring boot整合flyway实现数据的动态维护
  • unity中使用spine详解
  • 14. LangChain项目实战1——基于公司制度RAG回答机器人
  • 利用STM32TIM自制延迟函数实验
  • 创建一个MCP服务器,并在Cline中使用,增强自定义功能。
  • Android Activity栈关系解析
  • java使用word模板填充内容,再生成pdf
  • 回归实战详细代码+解析:预测新冠感染人数
  • AI人工智能机器学习之聚类分析
  • (下:补充——五个模型的理论基础)深度学习——图像分类篇章
  • 使用Python自动生成图文并茂的网页分析报告
  • uniapp-原生android插件开发摘要
  • GIT工具学习【1】:基本操作