简体   繁体   English

如何像打印到控制台一样打印到JAVAFX GUI

[英]How to print to JAVAFX gui like printing to console

Trying to print data to a gui made by javafx. 尝试将数据打印到javafx制作的gui中。

...
for(Entry<Date, String> entry : tmap.entrySet()) {
    Date key = entry.getKey();
    String value = entry.getValue();

    System.out.println(key + " => " + value);

I do not want to use setText() as it wont act like in console, it will clear to only one text every loop of TreeMap 我不想使用setText()因为它不会像在控制台中那样工作,它会在TreeMap每个循环中仅清除一个文本

The only way to do this is by concatenating the original text with the new content and setting the resulting String using setText . 唯一的方法是将原始文本与新内容连接起来,并使用setText设置结果String

In your case you can improve the efficiency of doing this by using a StringBuilder : 您可以使用StringBuilder来提高执行此操作的效率:

StringBuilder sb = new StringBuilder(textNode.getText());

for(Entry<Date, String> entry : tmap.entrySet()) {
    Date key = entry.getKey();
    String value = entry.getValue();

    sb.append(key).append(" => ").append(value).append('\n');
    ...
}
textNode.setText(sb.toString());

In case you're using a TextInputControl , appendText may be worth a look, but in case you know you're adding multiple String s, StringBuilder is probably more efficient. 如果您使用的是TextInputControl ,则值得看一下appendText ,但是如果您知道要添加多个String ,则StringBuilder可能会更高效。

for(Entry<Date, String> entry : tmap.entrySet()) {
    Date key = entry.getKey();
    String value = entry.getValue();

    textNode.appendText(key + " => " + value + '\n');
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM