【问题标题】:How to close previous jframe on the new open jframe如何在新打开的 jframe 上关闭以前的 jframe
【发布时间】:2016-04-26 01:19:04
【问题描述】:

我在一次单击按钮时处理两个 JFrame 时遇到问题。当单击 SUC_Forms 中的“添加记录”按钮时,我有 2 个 JFrame(SUC_Forms 和 add_suc),另一个新的 JFrame 将显示哪个是包含用于注册/归档的文本框的 add_suc jframe。当我按确定验证并插入数据库中的所有数据时,将弹出一个 JDialogbox 进行确认。一旦我点击“Ok”,两个 JFrame 都会处理,然后 SUC_Forms-JFrame 将再次显示包含更新的数据。我对当前的 JFrame 如何访问前一个和活动的 JFrame 有疑问。你能帮我解决这个问题吗?提前谢谢..

这是我的打印屏幕。

SUC_Forms 代码:

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

add_suc 代码:

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

}

【问题讨论】:

  • 而不是关闭框架并重新打开它。重新加载 GUI 不是更有意义吗?
  • 我不这么认为。但就我而言,我想重新加载 JFrame 以便能够刷新其内容,尤其是我的 jTable。我认为再次重新加载 JFrame 是刷新数据的简单方法。我是java netbeans的新手,所以我对如何使用不同的方法有一点了解。我希望通过使用论坛和提问将有助于提高我对此的了解。无论如何,感谢您对我的问题/问题表现出兴趣。 :)

标签: java mysql netbeans


【解决方案1】:

由于您要添加新记录的 JFrame 仍处于打开状态,您可以使用 SUC_Forms 中的方法将新记录作为参数传递。为此,您必须在 add_suc 中获得对 SUC_Forms 的引用。例如,您可以在打开此 Frame 时将此引用传递给 add_suc 的构造函数。然后您可以调用该方法以使用对 SUC_Forms 的引用添加记录。

你的构造函数看起来像这样:

add_suc(SUC_Forms parent)
{}

在 SUC_Forms 中,您可以创建如下方法:

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

您在 add_suc 中的方法调用将是:

parent.addRecord(-parameters-);

我希望这对您有所帮助。

【讨论】:

  • 谢谢。我尝试使用你的建议。我希望它会奏效。再次感谢:)
  • 没问题。尝试后随时报告:)
  • 什么也没发生。或者我可能不完全理解你对我的问题的回答哈哈哈..
  • 您能具体说明“无”的含义吗?您收到错误消息吗?您是否已调试并调用了 addRecord() 方法?
  • addRecord(); 的目的是什么?我已经创建了一种将记录插入数据库的方法,并且效果很好。我想要的是当按下 add_suc 表单中的“确定按钮”时,add_suc 和 SUC_Forms JFrame 将关闭在一起。并再次显示 SUC_Forms..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 2019-02-22
相关资源
最近更新 更多