简体   繁体   English

TextArea未在JavaFX中更新

[英]TextArea not updating in JavaFX

I'm defining the TextArea in my controller class like this: 我在控制器类中定义了TextArea,如下所示:

@FXML 
private TextArea txtAreaStatus;

And I'm trying to append text to the TextArea using this code: 我正在尝试使用以下代码将文本追加到TextArea:

@FXML
public void clickGo (ActionEvent event) {

    txtAreaStatus = new TextArea("");

    txtAreaStatus.appendText("data");

    System.out.println("clicked");
}

I'm really confused as to why my text area is not updating. 我真的很困惑为什么我的文本区域没有更新。 No errors whatsoever. 没有任何错误。

When I click the button, clicked gets printed on the screen. 当我单击按钮时, clicked会打印在屏幕上。

What am I doing wrong? 我究竟做错了什么?

Whenever you are using FXML and Controller combination, controls references are annotated with @FXML in the controller. 无论何时使用FXMLController组合,控件引用都在控制器中用@FXML注释。 The objects are injected into their respective references when the fxml is loaded. 加载fxml时,会将对象注入到它们各自的引用中。 Therefore you don't need to do define a new object for them. 因此,您无需为它们定义新对象。

In your code you need to remove : 在您的代码中,您需要删除:

txtAreaStatus = new TextArea("");

because this makes you loose the reference to the object of TextField on the scene and defines a new Textfield object (which is not on the scene). 因为这使您可以松开对场景中TextField对象的引用,并定义一个新的 Textfield对象(不在场景中)。 You are later trying to do operations on this new object. 您稍后尝试对该新对象进行操作。

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

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