swing—在java中将值从一个窗体传递到另一个窗体

nzk0hqpo  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(465)

我想把stuid值从 LoginFormIndoorCategoriesForm ,紧随其后 Home 以及 Categories 登录后的表单。
我试过在中重载这个方法 IndoorCategoriesForm ```
String id;
//overload the constructor
public IndoorCategoriesForm(String stuId){
initComponents();
//get student id from the login
this.id = stuId
labelStudentId.setText(id);
}

然后使用 `getText()` 方法获取表中的值。当我运行项目时,数据库中的已注册表显示sportid的值,但studentid为空。
indoorcategoris表格:

private void btnEnrollBasketBallActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pst;

//query to enrol the user 
String enrollUserQuery = "INSERT INTO `Enrolled`(`StuId`, `SpId`) VALUES (?, ?)";

//get student id from the login text field
String stuId = labelStudentId.getText();

//basketball sport id
String basketball = "1002";       

try {
        pst = DbConnection.getConnection().prepareStatement(enrollUserQuery);
        pst.setString(1, stuId);
        pst.setString(2, basketball);

        if (pst.executeUpdate() != 0){
            //if enrolling successfull, show enroll success form
            EnrollSuccessfullForm esf = new EnrollSuccessfullForm();
            esf.setVisible(true);
            esf.pack();
            esf.setLocationRelativeTo(null);
            esf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(null, "You have already enrolled");
        }

} catch (SQLException ex){
    Logger.getLogger(IndoorCategoriesForm.class.getName()).log(Level.SEVERE, null, ex);
}

}

登录表单:

private void buttonLogInActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement pst;
ResultSet rs;

//get stu id and password
String stuId = jTextFieldId.getText();
String pass = String.valueOf(jPasswordField.getPassword());

//check if the stuId exist in the database
String userLoginQuery = "SELECT * FROM `Student` WHERE `Stu_Id` = ? AND `Stu_Password` = ?";

if(stuId.trim().equals(""))
{
    JOptionPane.showMessageDialog(null, "Please enter a user ID", "Empty Field", 2);

}
else if(pass.trim().equals("")) {
    JOptionPane.showMessageDialog(null, "Please enter a password", "Empty Field", 2);
}
else
{
    try {
        pst = DbConnection.getConnection().prepareStatement(userLoginQuery);
        pst.setString(1, stuId);
        pst.setString(2, pass);
        rs = pst.executeQuery();

        if(rs.next()){
            //get value from the student Id to pass to Indoor categories form
            new IndoorCategoriesForm(stuId).setVisible(false);

            //shows the home page
            HomeForm hf = new HomeForm();
            hf.setVisible(true);
            hf.pack();
            hf.setLocationRelativeTo(null);
            hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.dispose();
        }
        else {
            JOptionPane.showMessageDialog(null, "Invalid user Id or pasword", "Login Error", 2);
        }

    } catch (SQLException ex) {
        Logger.getLogger(LogInForm.class.getName()).log(Level.SEVERE, null, ex);
    }   
}

}

odopli94

odopli941#

即使是不好的做法,你也可以在你的室内框架里有一个公共字符串变量,

public String passedId;

当您想从登录表单传递值时,可以创建室内框架的示例,然后设置字符串变量passedid:

IndoorFrame iF = new IndoorFrame();
iF.passedId = pass your variable here

只需为要传递数据的帧创建一个示例,然后将该类中的字符串变量设置为要传递给它的值。
在任何情况下,就像在评论中提到的那样,多个jframe可能都不是好的做法。
祝你好运

相关问题