0

我想从 to 传递stuId,它在登录后出现在表单旁边。LoginFormIndoorCategoriesFormHomeCategories

我试过重载方法IndoorCategoriesForm

String id;
//overload the constructor
public IndoorCategoriesForm(String stuId){
    initComponents();
    //get student id from the login 
    this.id = stuId
    labelStudentId.setText(id);
}

然后使用该getText()方法获取表内的值。当我运行项目时,数据库中的Enrolled表显示了SportId的值,但StudentId为空。

室内类别表格:

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);
        }   
    }
}
4

1 回答 1

0

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

public String passedId;

当你想从你的登录表单传递值时,你创建一个室内框架的实例,然后设置字符串变量passedId:

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

只需创建一个您想要传递数据的框架实例,然后将该类中的 String 变量设置为您想要传递给它的值。

在任何情况下,就像评论中提到的那样,多个 JFrame 可能不是好的做法。

祝你好运

于 2020-11-28T20:22:58.510 回答