我在控制台中工作,我想用颜色代码将文本转换为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();
屏幕截图: screenshot
输出(我发送过一次消息):
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
如果消息中包含有明显的字符(屏幕截图),则文本将成功转换。
对不起,我的英语不好,谢谢您的帮助!