更改在不同场景中输入的新场景中的标签文本(javafx) [英] Change label text in a new scene which was entered in different scene (javafx)
问题描述
我正在尝试更改标签中的文本,即在不同场景的文本字段中输入的文本. 我制作了2个FXML文件,第一个包含一个文本字段和确定"按钮,第二个包含一个标签(带有文本"Label"). 我的目标是在文本字段中输入文本,然后按确定"->打开新场景,标签将其文本更改为我在文本字段中输入的文本. 当标签,文本字段和确定"按钮都在同一场景中时,我很容易更改标签文本,但是当我在打开新场景时这样做时,我将失败... 经过研究,我为每个FXML文件制作了一个控制器,并为它们之间进行通信提供了一个"MainController". 这是我的主要课程:
I'm trying to change a text in a label, a text which was entered in a text field in a different scene. I made 2 FXML files, the first one contains a textfield and "ok" button, the second one contains a label(with the text "Label"). My goal is to enter a text in the textfield, and when I press "ok"-> open the new scene and the label will change it's text to the text I entered in the text field. I easily changed the label text when the label, the textfield and the ok button were all in the same scene, but when I do it while opening a new scene I fail... After some research, I made a controller for each FXML file, and a "MainController" that will communicate between them. This is my main class:
public class MainBanana extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("view/Welcome.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("MokaApp");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
}
我的第一个场景控制器:
my first scene controller:
public class WelcomeController {
@FXML
public TextField nameField;
@FXML
private Button okButton;
private MainController main;
@FXML
public void okClicked(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("Person.fxml"));
okButton.getScene().setRoot(root);
System.out.println(nameField.getText());
main.setLblFromTf(nameField.getText());
}
public void init(MainController mainController) {
main=mainController;
}
}
第二个场景控制器:
public class PersonController {
@FXML
public Label nameLabel;
private MainController main;
public void init(MainController mainController) {
main=mainController;
}
}
启动程序时,将打开欢迎"场景,在文本字段中输入文本,但是每当我按下确定"按钮时,场景都会切换到第二个场景,但标签文本保持不变(标签),并且在此行(位于WelcomeController中)上得到一个nullpointerexception错误:main.setLblFromTf(nameField.getText());
When I launch the program, The Welcome scene is opened, I enter a text to the textfield, but whenever I press the "ok" button, the scene changes to the second scene, but the label text stays the same(label) and I get a nullpointerexception error on this line(located in WelcomeController): main.setLblFromTf(nameField.getText());
很抱歉,很长的帖子.
推荐答案
您不需要到处都引用MainController
.
最简单的方法是:
public class PersonController {
@FXML
private Label nameLabel ;
public void setName(String name) {
nameLabel.setText(name);
}
}
那你就可以做
public class WelcomeController {
@FXML
private TextField textField ;
@FXML
private Button okButton ;
@FXML
public void okClicked() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Person.fxml"));
Parent root = loader.load();
PersonController personController = loader.getController();
personController.setName(textField.getText());
okButton.getScene().setRoot(root);
}
}
这篇关于更改在不同场景中输入的新场景中的标签文本(javafx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!