如何在使用FXML和JavaFX动态创建TextFlow时保留新行字符
下面的代码动态创建一个TextFlow和最初包含新行字符的文本(可能是图像和其他窗格) 但是,使用FXML的ByteArrayInputStream方法去掉了所有新行 尝试同时使用Windows和Unix的行尾字符,这并不是说这会有什么不同,但当您尝试不同的组合时,却徒劳无功 通常是要把新线路接回来,但在这里我没有控制权。其他加载方法似乎采用URL和资源链接,但这是一个被操纵的字符串 示例 java代码:如何在使用FXML和JavaFX动态创建TextFlow时保留新行字符,java,javafx,fxml,fxmlloader,Java,Javafx,Fxml,Fxmlloader,下面的代码动态创建一个TextFlow和最初包含新行字符的文本(可能是图像和其他窗格) 但是,使用FXML的ByteArrayInputStream方法去掉了所有新行 尝试同时使用Windows和Unix的行尾字符,这并不是说这会有什么不同,但当您尝试不同的组合时,却徒劳无功 通常是要把新线路接回来,但在这里我没有控制权。其他加载方法似乎采用URL和资源链接,但这是一个被操纵的字符串 示例 java代码: package simpleexample; import java.io.ByteAr
package simpleexample;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import com.jcabi.xml.XMLDocument;
public class TestFxmlWithBreaks extends Application {
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(createEntry(), 750, 750, Color.web("#666970"));
primaryStage.setScene(scene);
primaryStage.show();
}
private VBox createEntry() {
VBox entryBox = new VBox();
entryBox.setFillWidth(true);
entryBox.setSpacing(20);
entryBox.setMaxWidth(750);
try {
XMLDocument test = new XMLDocument(this.getClass().getResourceAsStream("inputFxml.xml"));
System.out.println(test.toString());
InputStream stream = new ByteArrayInputStream(test.toString().getBytes(StandardCharsets.UTF_8));
TextFlow element = new FXMLLoader().load(stream);
element.setMaxWidth(750);
entryBox.getChildren().add(element);
} catch (Exception e) {
//log something
}
return entryBox;
}
public static void main(String[] args) {
launch(args);
}
}
inputFxml.xml:
<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?><TextFlow xmlns:fx="http://javafx.com/fxml" styleClass="paragraph">
<Text style="-fx-font-size: 20;">A list of stuff</Text>
<Text>
* stuff
* more stuff
* even more stuff, all with new lines
</Text>
<Text style="-fx-font-size: 15;">This should demonstrate it</Text>
</TextFlow>
清单
*东西
*更多的东西
*甚至更多的东西,都有新的线条
这应该证明这一点
因此,在找到更好的解决方案之前,我选择了插入标记字符串,然后在加载fxml后替换它的黑客方法
不是很好,但对于我的用例来说已经足够了
public void markNewLinesInXmlTextNodes(Node node) { //w3c node
for(int i = 0 ; i < node.getChildNodes().getLength() ; i++) {
Node child = node.getChildNodes().item(i);
markNewLinesInXmlTextNodes(child);
}
if (node instanceof Element) {
Element el = (Element) node;
if (el.getTagName().toLowerCase().equals("text")) {
el.setTextContent(el.getTextContent().replaceAll("\n", "_LINEBREAK_"));
}
}
}
public void addNewLinesInFxTextNodes(javafx.scene.Node node) {
if (node instanceof javafx.scene.Parent) {
javafx.scene.Parent parent = (javafx.scene.Parent) node;
for(int i = 0 ; i < parent.getChildrenUnmodifiable().size() ; i++) {
javafx.scene.Node child = parent.getChildrenUnmodifiable().get(i);
addNewLinesInFxTextNodes(child);
}
}
if (node instanceof javafx.scene.text.Text) {
Text el = (Text) node;
el.setText(el.getText().replaceAll("_LINEBREAK_", "\n"));
}
}
public void markNewLinesInXmlTextNodes(节点节点){//w3c节点
对于(int i=0;i
因此,在找到更好的解决方案之前,我选择了插入标记字符串,然后在加载fxml后替换它的黑客方法
不是很好,但对于我的用例来说已经足够了
public void markNewLinesInXmlTextNodes(Node node) { //w3c node
for(int i = 0 ; i < node.getChildNodes().getLength() ; i++) {
Node child = node.getChildNodes().item(i);
markNewLinesInXmlTextNodes(child);
}
if (node instanceof Element) {
Element el = (Element) node;
if (el.getTagName().toLowerCase().equals("text")) {
el.setTextContent(el.getTextContent().replaceAll("\n", "_LINEBREAK_"));
}
}
}
public void addNewLinesInFxTextNodes(javafx.scene.Node node) {
if (node instanceof javafx.scene.Parent) {
javafx.scene.Parent parent = (javafx.scene.Parent) node;
for(int i = 0 ; i < parent.getChildrenUnmodifiable().size() ; i++) {
javafx.scene.Node child = parent.getChildrenUnmodifiable().get(i);
addNewLinesInFxTextNodes(child);
}
}
if (node instanceof javafx.scene.text.Text) {
Text el = (Text) node;
el.setText(el.getText().replaceAll("_LINEBREAK_", "\n"));
}
}
public void markNewLinesInXmlTextNodes(节点节点){//w3c节点
对于(int i=0;i
您能给出一个在这里失败的text.toString()
示例吗?也就是说,以某种方式做出一个决定?很难看出你想做什么。。。为什么不使用Java而不是FXML创建TextFlow
,并生成文本内容呢?没问题,先生。我添加了一个简单的示例。这里的XMLDocument本质上是无操作的,但实际上会有XSL转换。我不能用java创建它,因为有很多第三方内容正在被转换。我只是不想使用getResourceAsStream或类似的工具,以防它的工作方式有所不同。println会像我期望的那样打印整个文件。这应该行得通。请注意,它与流没有任何关系,但是:如果您只是以“正常”方式加载该FXML文件(TextFlow TextFlow=fxmloader.load(getClass().getResource(“inputFxml.FXML”);
),您将得到相同的结果。我会看看以后是否能找到解决办法。最终会在URL上调用openStream,因此您可能会在不同的地方发现相同的问题。请注意,这里也有,也许该解决办法也会起作用。您能否展示一个失败的text.toString()
示例?也就是说,以某种方式做出一个决定?很难看出你想做什么。。。为什么不使用Java而不是FXML创建TextFlow
,并生成文本内容呢?没问题,先生。我添加了一个简单的示例。这里的XMLDocument本质上是无操作的,但实际上会有XSL转换。我不能用java创建它,因为有很多第三方内容正在被转换。我只是不想使用getResourceAsStream或类似的工具,以防它的工作方式有所不同。println会像我期望的那样打印整个文件。这应该行得通。请注意,它与流没有任何关系,但是:如果您只是以“正常”方式加载该FXML文件(TextFlow TextFlow=fxmloader.load(getClass().getResource(“inputFxml.FXML”);
),您将得到相同的结果。我会看看以后是否能找到解决办法。最终会在URL上调用openStream,因此您可能会在不同的地方发现相同的问题。请注意,这里也有解决办法,也许该解决办法也会起作用。