簡體   English   中英

如何將變量傳遞給另一個類Java

[英]How to pass variable to another class java

我有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為null我該怎么辦? 謝謝你的評論

切勿在代碼中實例化控制器。 而是: FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); FXMLDocumentController fx = loader.getController(); FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); FXMLDocumentController fx = loader.getController();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM