【问题标题】:Pass a value form a form to another form in java在java中将一个值从一个表单传递到另一个表单
【发布时间】:2021-03-09 00:59:17
【问题描述】:

我想将 stuId 值从LoginForm 传递给IndoorCategoriesForm,登录后位于HomeCategories 表单旁边。

我试过重载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 为空。

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

【问题讨论】:

标签: java swing netbeans jform jform-designer


【解决方案1】:

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

public String passedId;

当你想从你的登录表单中传递值时,你创建一个 Indoor 框架的实例,然后设置 String 变量 passId:

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

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

无论如何,就像在 cmets 中提到的那样,多个 JFrame 可能不是好的做法。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多