swing无法在Java框架中设置文本
嘿,我正在创建一个java项目。其中我有一个插入记录框,在插入框上我有一个输入父亲ID的选项,如果用户不知道父亲ID,那么我设置了一个按钮来查找父亲ID。 当用户单击该按钮时,将出现新帧,用户可以搜索id,结果将显示在表格中,当用户单击特定记录时,该帧将被释放,并应在前一帧上设置相应的id
我已经为它编写了代码,它正在将值传递到前一帧,但它没有将值设置为我想要的textfield。我哪里做错了?这是代码
家庭服务。爪哇
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());
}
}
# 1 楼答案
您的问题是,您正在另一个类中创建一个新的FamilyInsert对象,并更改其状态,但这会使原始FamilyInsert对象的状态保持不变。您需要做的是将原始显示的FamilyInsert的引用传递到第二个对象中,然后更改其状态
更改此项:
更像是:
将引用传递到类中并用于设置字段:
然后使用传入的引用更改原始对象的状态
您还希望第二个窗口是JDialog而不是JFrame。请参阅:The Use of Multiple JFrames, Good/Bad Practice?
# 2 楼答案
你能试试吗