javafx - ActionListener 中的 TextField getText() 问题
问题描述
我在控制台中工作,我想将文本转换为带有颜色代码的 html 格式。如果我从文本字段发送文本,我无法替换字符,并且我的文本不会被转换。这是一个例子:
我的测试代码:
WebViewConsole webViewConsole = new WebViewConsole();
webViewConsole.appendText("§1This is a §l§2Test §r§1, just §l§2because§r§1!\n");
TextField textField = new TextField();
textField.setText("§1This is a §l§2Test §r§1, just §l§2because§r§1!\n");
System.out.println(textField.getText().replaceAll("§", ""));
System.out.println(textField.getText().contains("§"));
Button button = new Button("Send");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
for (String htmlText : webViewConsole.getHtmlFormat(textField.getText())) {
System.out.println(htmlText);
}
String text = textField.getText();
webViewConsole.appendText(textField.getText());
System.out.println(text.replaceAll("§", ""));
System.out.println(text.contains("§"));
}
});
HBox hBox = new HBox(textField, button);
VBox vBox = new VBox(webViewConsole.getConsole(), hBox);
primaryStage.setScene(new Scene(vBox, 500, 300));
primaryStage.show();
截图: 截图
输出(我发送了一次消息):
1This is a l2Test r1, just l2becauser1!
true
<span style="color:white;font-weight:normal"></span>
<span style="color:blue;font-weight:normal">This is a </span>
<span style="color:darkgreen;font-weight:bold">Test </span>
<span style="color:white;font-weight:normal"></span>
<span style="color:blue;font-weight:normal">, just </span>
<span style="color:darkgreen;font-weight:bold">because</span>
<span style="color:white;font-weight:normal"></span>
<span style="color:blue;font-weight:normal">!</span>
1This is a l2Test r1, just l2becauser1!
true
如果消息包含该 stange 字符(屏幕截图),则文本将成功转换。
对不起我的英语不好,谢谢你的帮助!
解决方案
推荐阅读
- c# - 数据合并问题
- php - 使用 INNER JOIN 从两个 SQL 表中获取数据,显示 HTML 表
- django - How to configure lighttpd to serve django static files?
- python - Return index of original matrix that was transformed in a diagonal list - Python 3
- indexer - 在 iManage Worksite 中调试全文搜索
- r - 在数据帧列表上循环应用函数
- c++ - *(double*)&a 有什么作用?
- symfony - Symfony 3.4 RequestStack -> MasterRequest -> ClientIp
- c++ - 2 dimensional array of TCHAR
- r - 在 R/R-studio 中为仪表板创建更丰富的速度计仪表图表?