首页 > 解决方案 > 我不能在 AccountController 中为全名和钱设置文本。错误代码是“不能从静态上下文引用非静态变量”

问题描述

我不能用静态设置文本。请帮我在 AccountController 中设置全名和钱的文本。请让我举几个例子

我将 String full1 和 String money 从 Menu 传递到 Account 并设置 Text 有错误

菜单控制器类

public class MenuController implements Initializable {
    @FXML
    private Text nameuser;

    String account;
    void setData(String fullname, String username) {
        nameuser.setText(fullname);
        account = username;
    }

    @FXML
    private void btnsp(ActionEvent event) throws Exception {
        try {
            Connection con;
            con = Connect.Connect();

            PreparedStatement pstm = con.prepareStatement("select fullname, money from account where username = ?");
            pstm.setString(1, account);
            ResultSet result  = pstm.executeQuery();
            if(result.next()) {
                System.out.println(result.getString(1));
                System.out.println(result.getString(2));
            } else {
                System.out.println("Fail!");
            }
            String fullname1 = result.getString(1);
            String money = result.getString(2);
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Account.fxml"));
            Parent root = loader.load();
            Scene scene = new Scene(root);
            Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            AccountController MenuController = loader.<AccountController>getController();
            AccountController.setData(fullname1, money);
            app_stage.hide();
            app_stage.setScene(scene);
            app_stage.show();  
        } catch (SQLException ex) {
            System.out.println("SQLException");
            System.out.println("Error message: " + ex);
        }
    }
}

帐户控制器类

public class AccountController implements Initializable {
    @FXML
    private Text fullname;
    @FXML
    private Text money;

    static void setData(String fullname1, String money) {
        fullname.setText(fullname1);
        fullname.setText(money);
    }
}

标签: javafx

解决方案


推荐阅读