简体   繁体   English

JavaFx在场景打开时设置标签文本

[英]JavaFx Set Label Text on Scene Open

I have an FXML file with an empty Label named welcomeText . 我有一个带有空标签Label的FXML文件welcomeText It's the main Scene of my program and I would like to set the label text to something like that: Hello "username" when I start the program on Windows or Linux. 这是我程序的主要场景,我想将标签文本设置为以下内容:在Windows或Linux上启动程序时, 您好为“用户名”

public class MainAdminController implements Initializable {

@FXML
private Label welcomeText;
final String username = System.getProperty("user.name");

@FXML
private void SetWelcome() {
welcomeText.setText("Hello " +username);
 }
}

But it isn't show anything. 但是它什么也没显示。 Any idea how can I set the Label properly when I open the scene? 知道打开场景时如何正确设置标签吗? Thanks. 谢谢。

The SetWelcome method is superfluous. SetWelcome方法是多余的。

Define an initialize() method for your controller and it will automatically be invoked when the FXMLLoader loads a new document linked to the controller. 为您的控制器定义一个initialize()方法,当FXMLLoader加载链接到该控制器的新文档时,它将自动被调用。

public void initialize() {
    welcomeText.setText("Hello " +username);
}

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

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