gpt4 book ai didi

JavaFX 更新文本区域

转载 作者:搜寻专家 更新时间:2023-11-01 02:27:23 24 4
gpt4 key购买 nike

我有一个简单的 JavaFX 应用程序,它有一个 TextArea。我可以在 start() 方法中使用以下代码更新 textArea 的内容:

new Thread(new Runnable() {

public void run() {

for (int i = 0; i < 2000; i++) {

Platform.runLater(new Runnable() {
public void run() {
txtarea.appendText("text\n");
}
});
}
}
}).start();

代码只是将 text 字符串写入 TextArea 2000 次。我想从在 start() 方法之外实现的函数更新此 textArea。

public void appendText(String p){
txtarea.appendText(p);
}

可以从使用 JavaFX 应用程序更新 TextArea 的任意程序调用此函数。我如何在 appendText 函数中执行此操作?

最佳答案

您可以为需要写入 javafx.scene.control.TextArea 的类提供对包含 public void appendText(String p) 的类的引用方法,然后调用它。我建议您还传递调用该​​方法的类的指示,例如:

public class MainClass implements Initializable {
@FXML
private TextArea txtLoggingWindow;
[...more code here...]
public void appendText(String string, String string2) {
txtLoggingWindow.appendText("[" + string + "] - " + string2 + "\n");
}
}

public class SecondClass {
private MainClass main;
public SecondClass(MainClass mClass) {
this.main = mClass;
}
public void callMainAndWriteToArea() {
this.main.appendText(this.getClass().getCanonicalName(), "This Text Goes To TextArea");
}
}

关于JavaFX 更新文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18597644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com