简体   繁体   English

如何在新打开的jframe上关闭以前的jframe

[英]How to close previous jframe on the new open jframe

I have a problem in disposing the two JFrames in one click of a button. 我在一次单击按钮中放置两个JFrame时遇到问题。 I have a 2 JFrames(SUC_Forms and add_suc) when the button "ADD RECORDS" in SUC_Forms was click another new JFrame will show which is the add_suc jframe that contains textboxes for registration/filing. 当单击SUC_Forms中的“ ADD RECORDS”按钮时,我有2个JFrames(SUC_Forms和add_suc),另一个新的JFrame将显示为add_suc jframe,其中包含用于注册/提交的文本框。 When i press ok to validate and insert all the data in the database, a JDialogbox will pop-up for a confirmation. 当我按OK确认并将所有数据插入数据库时​​,将弹出一个JDialogbox进行确认。 Once I hit "Ok" both JFrames will dispose and then SUC_Forms-JFrame will show again containing the updated data. 一旦我单击“确定”,两个JFrame将被处置,然后SUC_Forms-JFrame将再次显示,其中包含更新的数据。 I have a trouble on how the current JFrames has an access to the previous and active JFrame. 我对当前的JFrame如何访问以前的活动JFrame有麻烦。 Can you pls help me to solve this problem? 您能帮我解决这个问题吗? Thanks in advance.. 提前致谢..

Heres my print screen. 这是我的打印屏幕。

在此处输入图片说明

SUC_Forms Code: SUC_Forms代码:

    add_suc add = new add_suc();
    add.show();

add_suc Code: add_suc代码:

private void btn_okActionPerformed(java.awt.event.ActionEvent evt) { 私人void btn_okActionPerformed(java.awt.event.ActionEvent evt){

    int verify = JOptionPane.showConfirmDialog(rootPane,"Are you sure you want to add this record?","", JOptionPane.YES_NO_OPTION);

    if(verify == 0)
    {

       String moa_id = txt_id.getText();
       Object region = cmb_region.getSelectedItem();
       String holder = txt_holder.getText();
       String brgy = txt_brgy.getText();
       String municipality = txt_municipality.getText();
       String province = txt_province.getText();
       String president = txt_president.getText();
       Object month = cmb_month.getSelectedItem();
       Object day = cmb_day.getSelectedItem();
       String year = txt_year.getText();
       String area = txt_area.getText();
       String allotment = txt_allotment.getText();
       String activities = txt_activities.getText();
       String status = txt_status.getText();
       String fill = "";

       if(moa_id.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill Moa ID!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }

       if(region.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please select region!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(holder.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill holder!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(brgy.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill barangay!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(municipality.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill municipality!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(province.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill province!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
        else if(president.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill president/representative!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
        else if(month.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please set the month of Date Approved field!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }

       else if(day.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please set the date of Date Approved field!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(year.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please set the year of Date Approved field!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(area.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill area!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(allotment.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill allotment!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(activities.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill activities/roles!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }
       else if(status.equals(fill))
       {
           JOptionPane.showMessageDialog(rootPane, "Please fill status!", "Message",JOptionPane.ERROR_MESSAGE,null);
       }

       else{

        try{

        st = con.createStatement();

        String query =  "insert into moa_suc values('"+moa_id+"','"+region+"','"+holder+"','"+brgy+"','"+municipality+"','"+province+"','"+president+"','"+month+"','"+day+"','"+year+"','"+area+"','"+allotment+"','"+activities+"','"+status+"')";

        st.executeUpdate(query);

        txt_id.setText("");
        cmb_region.setSelectedItem(0);
        txt_holder.setText("");
        txt_brgy.setText("");
        txt_municipality.setText("");
        txt_province.setText("");
        txt_president.setText("");
        cmb_month.setSelectedItem(0);
        cmb_day.setSelectedItem(0);
        txt_year.setText("");
        txt_area.setText("");
        txt_allotment.setText("");
        txt_activities.setText("");
        txt_status.setText("");

        JOptionPane.showMessageDialog(rootPane, "Successfully added to the record!", "Message",JOptionPane.INFORMATION_MESSAGE,null);
        this.dispose();

    }
    catch(SQLException err){

        System.out.println(err.toString());   
    }

     }

    }
    else
    {
        JOptionPane.showMessageDialog(rootPane, "Sorry! No data added in the record!", "Message",JOptionPane.INFORMATION_MESSAGE,null);
    }

}

Since the JFrame you want to add the new record to is still open you could use a method in SUC_Forms where you pass the new record as a parameter. 由于您要添加新记录的JFrame仍处于打开状态,因此可以在SUC_Forms中使用一个方法,在其中将新记录作为参数传递。 To be able to do that you would have to get a reference to SUC_Forms in add_suc. 为此,您必须在add_suc中获得对SUC_Forms的引用。 For example you can pass this reference to the constructor of add_suc when you are opening this Frame. 例如,当您打开此框架时,可以将此引用传递给add_suc的构造函数。 Then you can call the method to add the record using the reference to SUC_Forms. 然后,您可以调用方法以使用对SUC_Forms的引用来添加记录。

Your constructor would look something like this: 您的构造函数如下所示:

add_suc(SUC_Forms parent)
{}

And in SUC_Forms you create a method like: 然后在SUC_Forms中创建一个类似以下的方法:

public void addRecord(-here you pass the required parameters-)
{}

Your method call in add_suc would then be: 那么您在add_suc中的方法调用将是:

parent.addRecord(-parameters-);

I hope this helps you a bit. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM