我想显示用户在下一个场景中的Label中在TextField中输入的值。我已经设置了3个按钮,一个按钮前进到下一个场景,第二个按钮回到第一个,而第三个按钮(位于第二个场景中)向您显示用户按下时输入的值。
当按下按钮在第二个场景中显示它时,我不知道如何从第一个场景的TextField中获取值。它说找不到符号(并且变量的类已导入并公开)
另一方面,我如何才能使输入的值(以及随后显示的值)是对象的变量(当然是动态的)呢?
这是控制Scene.fxml(第一个场景)的类
public class FXMLController implements Initializable {
//In this TextField users introduces an int or String
@FXML
TextField introduceVal;
//BUTTON TO GO TO SCENE2
@FXML
private void goNext(ActionEvent event)throws IOException{
Parent Scene2Parent = FXMLLoader.load(getClass().getResource("/fxml/Scene2.fxml"));
Scene Scene2Scene = new Scene(Scene2Parent);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Scene2Scene);
window.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
控制Sccene2.fxml的类
public class FXMLController2 implements Initializable {
@FXML
Label waterDrank;
//BUTTON TO GET BACK TO SCENE
@FXML
private void goBack(ActionEvent event)throws IOException{
Parent Scene1Parent = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene Scene1Scene = new Scene(Scene1Parent);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Scene1Scene);
window.show();
}
//Button that shows the value when pressed
@FXML
private void showWater (ActionEvent event){
//shows the value that the user input previously on scene
waterDrank.setText(introduceVal);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
编辑:
更改自
waterDrank.setText(introduceVal);
到
waterDrank.setText(FMXL.introduceVal);
现在我面临问题错误:无法从静态上下文中引用非静态变量IntroductionVal。