我有兩個控制器的登錄和主,當logincontroller驗證用戶然後maincontroller持有控制,我試過這個方法,但沒有工作,userName變量給出了空值。從另一個控制器獲取字符串變量值javafx
的LoginController類:
public class loginController implements Initializable{
private String user;
public String getUser() {
return user;
}
if((user.equals(loginUsername.getText()) && (pwd.equals(loginPassword.getText())))){
switch(role){
case "Admin": Stage adminStage=new Stage();
FXMLLoader adminLoader = new FXMLLoader(getClass().getResource("/FXML/Admin/Admin.fxml"));
Parent adminRoot = adminLoader.load();
AdminController adminController = (AdminController)adminLoader.getController();
adminController.setLoginController(this);
}
maincontroller類:當你的控制器得到由FXMLLoader
初始化
public class AdminController implements Initializable {
@FXML
private Label userName;
public void setLoginController(LoginController loginController) {
this.loginController = loginController;
}
public void initialize(URL url, ResourceBundle rb) {
loginController =new LoginController();
try {
db = new DBConnector();
// String user = loginController.getUser();
System.out.println(user);
String query = "SELECT u_full_name,img FROM Users WHERE u_username='"+user+"'";
db.setResultSet(query);
while(db.getResultset().next()){
userName.setText(db.getResultset().getString(1));
imageUpload.setImage(new Image("file:"+db.getResultset().getString(2)));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "can't retreive photo",e.toString(),0);
}
}
你是否在你的代碼中使用'setLoginController'? – nafas
你在哪裏設置'user'字段? –
是的,看看登錄控制器類我已經設置 –