如何在previos场景中获取文本字符串

时间:2017-11-29 07:55:33

标签: java javafx textfield

我有一个项目登录系统

主要场景:CMainWallet.java

@FXML private Label status;
@FXML private Label version;
@FXML public TextField user;
@FXML public TextField pass;
@FXML private Button login;
@FXML private Button exit;

@FXML
public void loginWallet(ActionEvent event) {

    String u = user.getText();
    String p = pass.getText();

    LoginModel loginModel = new LoginModel();

    if (loginModel.validationLogin(u, p)) {

        Stage stage = new Stage();

        try {
            Parent root = FXMLLoader.load(getClass().getResource("dashboard/Dashboard.fxml"));

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Wallet Dashboard");
            stage.show();
            login.getScene().getWindow().hide();

        } catch(Exception e) {

            e.printStackTrace();
        }
    } else {

        status.setText("Maaf Akun anda salah, Coba ulang lagi");
    }
}

Dashboard.java

@FXML Label user;

@Override
public void initialize(URL url, ResourceBundle rb) {

    CMainWallet main = new CMainWallet();

    user.setText("Hello "+main.user.getText());
}

我想在CMainWallet.java中获取字符串textfied 我在代码上的错误在哪里 我的代码是否有任何解决方案

谢谢:D

1 个答案:

答案 0 :(得分:0)

试试这个

class Dashboard implements Initializable {
    @FXML Label user;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
//        CMainWallet main = new CMainWallet();

//        user.setText("Hello "+main.user.getText());
    }

    public void setUsername(String username) {
        user.setText(username);
    }
}

主要场景

@FXML
public void loginWallet(ActionEvent event) {

    String u = user.getText();
    String p = pass.getText();

    LoginModel loginModel = new LoginModel();

    if (loginModel.validationLogin(u, p)) {

        Stage stage = new Stage();

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("dashboard/Dashboard.fxml"));
            Parent root = loader.load();

            Dashboard dashboard = loader.getController();
            dashboard.setUsername(u);

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Wallet Dashboard");
            stage.show();
            login.getScene().getWindow().hide();

        } catch(Exception e) {

            e.printStackTrace();
        }
    } else {

        status.setText("Maaf Akun anda salah, Coba ulang lagi");
    }
}

我假设你的其余逻辑都没问题