JavaFX UI控件教程(二十)之HTML Editor

翻译自  HTML Editor

在本章中,您将学习如何使用嵌入式HTML编辑器编辑JavaFX应用程序中的文本。

HTMLEditor控件是一个功能齐全的富文本编辑器。它的实现基于HTML5的文档编辑功能,包括以下编辑功能:

  • 文本格式包括粗体,斜体,下划线和样式

  • 段落设置,例如格式,字体系列和字体大小

  • 前景色和背景色

  • 文字缩进

  • 项目符号和编号列表

  • 文字对齐

  • 添加水平规则

  • 复制和粘贴文本片段

图19-1显示了添加到JavaFX应用程序的富文本编辑器。

图19-1 HTML编辑器

HTMLEditor班于在一个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 {@Overridepublic 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 {@Overridepublic 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控件通过使用GridVBox布局容器排列在应用程序场景中。编译并运行此应用程序时,图19-5中显示的窗口显示了当用户编写每周报告时此应用程序的输出。

图19-5电子邮件客户端用户界面

您可以HTMLEditor通过调用setPrefWidthsetPrefHeight方法设置对象的特定宽度和高度值,也可以不指定其宽度和高度。例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.";@Overridepublic 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.";@Overridepublic 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文档 

  • HTMLEditor

  • WebView

  • WebEngine

  • Label

  • Button

  • TextField

  • ChoiceBox

  • ScrollPane

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/325038.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript ECMA

JavaScript的引入方式 <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><!--[1]js引入的方式一 alert:弹窗--><script type"text/javascript"> alert("js学习"); </s…

OJ4008-糖果【各种dp之3】

题目 就是一个英雄瞎搞之类为世界作出贡献&#xff0c;蓝后某zz公司免费给他糖&#xff0c;然后由于某些原因他只能拿能整除k数量糖果&#xff0c;然后每个盒子里有一定数量的糖果&#xff0c;拿了盒子就必须拿完里面所有的糖果&#xff0c;求他怎么拿的最多。 输入 5 7 1 …

使用layui弹框实现添加时,当添加成功之后如何进行关闭当前窗口刷新父页面的数据

一看标题可能大家都比较模糊&#xff0c;我就去特意做了一个gif的演示图&#xff0c;结果发现太小了&#xff0c;就改成了现在的视频&#xff0c;视频地址&#xff1a; 使用layui实现对数据的增删改查演示案例实现技术是&#xff1a;ssh框架layui表格&#xff0c;即简单的对单表…

IdentityServer4 SigningCredential(RSA 证书加密)

IdentityServer4 默认提供了两种证书加密配置&#xff1a; services.AddIdentityServer().AddDeveloperSigningCredential().AddTemporarySigningCredential(); 这两种证书加密方式&#xff0c;都是临时使用&#xff0c;每次重启项目的时候&#xff0c;都会重新生成一个新的证…

JavaFX UI控件教程(二十一)之Tooltip

翻译自 Tooltip 在本章中&#xff0c;您将了解工具提示&#xff0c;即当鼠标光标悬停该控件时&#xff0c;可以为任何UI控件设置的控件。 的Tooltip类表示通常用于显示关于所述用户接口的控制附加信息的公共UI组件。可以通过调用setTooltip方法在任何控件上设置工具提示。 …

JavaScript对象与事件

JavaScript中常用对象 <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><script type"text/javascript">/********[1]日期对象的学习****************/function demo1(){ var date new D…

OJ7627-鸡蛋的硬度【各种dp之4】

题目&#xff08;直接贴了&#xff09; 最近XX公司举办了一个奇怪的比赛&#xff1a;鸡蛋硬度之王争霸赛。参赛者是来自世 界各地的母鸡&#xff0c;比赛的内容是看谁下的蛋最硬&#xff0c;更奇怪的是XX公司并不使用什么精密仪器来测量蛋的硬度&#xff0c;他们采用了一种最老…

layui如何实现添加数据时关闭页面层,并实时刷新表格数据?

可能看到标题的你没有明白我到底想表达啥&#xff1f;&#xff08;我起名字时删改多遍&#xff0c;这是最终定下来的&#xff09;&#xff0c;不过&#xff0c;为了让大家看的明白&#xff0c;我专门去做了个演示视频&#xff1a;演示案例使用的技术&#xff1a;ssh框架layui表…

属性VS局部变量

类中属性的使用属性&#xff08;成员变量&#xff09; vs 局部变量1.相同点&#xff1a;1.1 定义变量的格式&#xff1a;数据类型 变量名 变量值1.2 先声明&#xff0c;后使用1.3 变量都有其对应的作用域2.不同点&#xff1a;2.1 在类中声明的位置的不同属性&#xff1a;…

.net core 集成 autofac

1. Install Install-Package AutofacInstall-Package Autofac.Extensions.DependencyInjection 2.Startup 2.1 增加成員 public IContainer ApplicationContainer { get; private set; } 2.2 Startup.ConfigureServices 返回值改為&#xff1a;IServiceProvider 末尾中增…

JavaFX UI控件教程(二十三)之Menu

翻译自 Menu 本章介绍如何创建菜单和菜单栏&#xff0c;添加菜单项&#xff0c;将菜单分组&#xff0c;创建子菜单以及设置上下文菜单。 您可以使用以下JavaFX API类在JavaFX应用程序中构建菜单。 菜单栏 菜单项 菜单 CheckMenuItem RadioMenuItem 菜单 CustomMenuItem…

利用bootstraptable展示数据,对数据进行排序分页等操作

今天分享一下bootstraptable的相关技能点&#xff0c;由于第一次接触&#xff0c;所以刚开始碰了好多壁&#xff0c;于是趁现在过去不久&#xff0c;先总结总结。 Bootstraptable简单的来说就是一个表格控件&#xff0c;但是这个表格可不是一般的表格&#xff0c;分页、排序、…

JavaScript BOM

标题什么是BOM 1.BOM是Browser Object Model的简写&#xff0c;即浏览器对象模型。 2.BOM由一系列对象组成&#xff0c;是访问、控制、修改浏览器的属性的方法 3.BOM没有统一的标准(每种客户端都可以自定标准)。 4.BOM的顶层是window对象 BOM对象学习 window <!DOCTYPE h…

OJ8462-大盗阿福【各种dp之5】

题目 我们的黑虎阿福改行当小偷啦!然后他去偷东西&#xff0c;然后那个zz报警系统只有在他洗劫两家相邻的店才会报警&#xff0c;然后求他在不触发警报的情况下能拿到最多的钱。 &#xff08;注&#xff1a;没有偷了会扣钱的店铺&#xff09; 输入 2 3 1 8 2 4 10 7 6 1…

交换两个对象

//问题二&#xff1a;使用冒泡排序按学生成绩排序&#xff0c;并遍历所有学生信息for(int i 0;i < stus.length - 1;i){for(int j 0;j < stus.length - 1 - i;j){if(stus[j].score > stus[j 1].score){//如果需要换序&#xff0c;交换的是数组的元素&#xff1a;St…

JavaFX UI控件教程(二十四)之Password Field

翻译自 Password Field 在本章中&#xff0c;您将了解另一种类型的文本控件&#xff0c;即密码字段。 本PasswordField类实现一个专门的文本字段。通过显示回显字符串来隐藏用户键入的字符。图23-1显示了一个密码字段&#xff0c;其中包含提示消息。 图23-1带有提示消息的密…

3分钟内看完这,bootstraptable表格控件,受益匪浅!

今天分享一下bootstraptable的相关技能点&#xff0c;由于第一次接触&#xff0c;所以刚开始碰了好多壁&#xff0c;于是趁现在过去不久&#xff0c;先总结总结。Bootstraptable简单的来说就是一个表格控件&#xff0c;但是这个表格可不是一般的表格&#xff0c;分页、排序、查…

微软Azure开源开发者(深圳)峰会等你来

微软开发技术与云平台自从迈向开放、开源、跨平台的转型以来&#xff0c;已经受到全球开源社区们的关注。 从 Github 上高居世界首位的开源项目贡献数量&#xff0c;可以看到微软贯彻开源战略的实际行动。另一方面&#xff0c;微软也主动与开源社区做密切的技术交流。 本次 Azu…

OJ4121 and OJ2968-股票买卖 and Maximun sum【各种dp之6 and 9】

股票买卖 题目 阿福该炒股了&#xff0c;然后假设他已经预测到了后几天的股票&#xff0c;要求他最多买卖两次的赚到的最大值。 &#xff08;注&#xff1a;他只有第一次卖了才能再买&#xff09; 输入 3 7 5 14 -2 4 9 3 17 6 6 8 7 4 1 -2 4 18 9 5 2 输出 28 2…

面向对象VS面向过程

1.面向过程&#xff1a;强调的是功能行为&#xff0c;以函数为最小单位&#xff0c;考虑怎样做。 2.面向对象&#xff1a;强调具备了功能的对象&#xff0c;以类/对象为最小单位&#xff0c;考虑谁来做。