我只需点击一下按钮即可处理两个JFrame。当按钮" ADD RECORDS"时,我有2个JFrame(SUC_Forms和add_suc)。在SUC_Forms中单击另一个新的JFrame将显示哪个是add_suc jframe,其中包含用于注册/归档的文本框。当我按下确认并在数据库中插入所有数据时,将弹出一个JDialogbox进行确认。一旦我点击了#34; 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);
}
}
答案 0 :(得分:0)
由于您要添加新记录的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-);
我希望这对你有所帮助。