无法在Java Frame

时间:2018-03-15 13:51:48

标签: java swing jframe

嘿我正在创建一个java项目。其中我有一个插入记录框,在插入框架上我有一个输入父ID的选项,如果用户不知道父id,所以我设置了一个按钮来查找父id。 当用户点击该按钮时,将出现新框架并且用户可以搜索id,结果将显示在表格中,当用户点击特定记录时,框架将被处置并应在之前设置相应的ID帧。

我已经为它编写了代码,它将值传递给前一帧,但它没有将值设置为我想要的文本字段。我在哪里做错了?这是代码。

FamilyInsert.java

public class FamilyInsert extends javax.swing.JFrame {

/**
 * Creates new form FamilyInsert
 */

int id = DBManager.genID();
public int fid;
public FamilyInsert() {
    initComponents();
    txtId.setText(""+id);
    txtName.requestFocus();
}



public void setFid(int fid){
    txtFid.setText(""+fid);
    System.out.println("setFID "+fid);
}

public void reset()
{
    txtName.setText("");
    txtFather.setText("");
    txtFid.setText("");
    txtCity.setText("");
    txtState.setText("");
    txtName.requestFocus();

}


private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
   int id = Integer.parseInt(txtId.getText());
   String name = txtName.getText();
   String fname = txtFather.getText();
   int fid = Integer.parseInt(txtFid.getText());
   String city = txtCity.getText();
   String state = txtState.getText();
   Family family = new Family(id,name,fname,fid, city,state);
    boolean flag = false;

    flag = DBManager.insertMember(family);
    if(flag==true){
        JOptionPane.showMessageDialog(this,"Successfully Saved");
        id++;
        txtId.setText(""+id);
        reset();
    }
    else
    {
        JOptionPane.showMessageDialog(this,"Error Occured");
    }



}                                         

private void txtFidActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
}                                      

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
  SearchFatherFrame f = new SearchFatherFrame();
  f.setLocationRelativeTo(null);
  f.setVisible(true);

}                                         

}

并从搜索框架中获取:

 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
       int id;
        if(evt.getClickCount()==2){
            if(jTable1.getSelectedRow()!=-1)
            {
                int index = jTable1.getSelectedRow();
                Family s = list.get(index);
                id = s.getId();
                System.out.println("ID from search frame "+id);
                FamilyInsert f = new FamilyInsert();
                f.setFid(id);
                this.dispose();
                //JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());                    
            }
        }

2 个答案:

答案 0 :(得分:1)

您的问题是,您正在另一个类中创建 new FamilyInsert对象,并更改其状态,但这会使原始FamilyInsert对象的状态保持不变。您需要做的是将原始显示的FamilyInsert的引用传递给第二个对象,然后更改其状态。

改变这个:

SearchFatherFrame f = new SearchFatherFrame();

更像是:

SearchFatherFrame f = new SearchFatherFrame(this);

将引用传递给类并用于设置字段:

public class SearchFatherFrame {
    private FamilyInsert familyInsert;

    public SearchFatherFrame(FamilyInsert familyInsert) {
        this.familyInsert = familyInsert;
        // other code....
    }
}

然后使用传入的引用来更改原始对象的状态。

if(jTable1.getSelectedRow()!=-1) {
    int index = jTable1.getSelectedRow();
    Family s = list.get(index);
    id = s.getId();
    System.out.println("ID from search frame "+id);

    // FamilyInsert f = new FamilyInsert();
    // f.setFid(id);
    familyInsert.setFid(id); // **** add

    this.dispose();
    //JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());                    
}

您还希望第二个窗口是JDialog而不是JFrame。请参阅:The Use of Multiple JFrames, Good/Bad Practice?

答案 1 :(得分:-2)

你能试试吗

public void setFid(int fid){
        txtFid.setText(""+fid);
        System.out.println("setFID "+fid);
        yourJFrame.setVisible(true); //Reloads the frame
}