如何将变量传递给另一个类Java

时间:2018-07-30 12:55:00

标签: java javafx getter-setter

我有FXMLDocumentController.java,我想将名称(文本填充)发送给anoterclass

public class FXMLDocumentController implements Initializable { 
@FXML
private Label label;
@FXML
private Button button;
@FXML
private TextField name;
private String sss = "";
@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
    sss=name.getText();
    setSss(sss);
    System.out.print(sss);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    
public String getSss() {
    return sss;
}
public void setSss(String sss) {
    this.sss = sss;
}   

这是另一个类login.java

public class Login extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
    FXMLDocumentController fx = new FXMLDocumentController();
    System.out.print(fx.getSss());
}   

我调试并且fx.getSss为空 我该怎么办? 谢谢你的评论

2 个答案:

答案 0 :(得分:0)

从不实例化代码中的控制器。 代替: FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); FXMLDocumentController fx = loader.getController();

答案 1 :(得分:-1)