从另一个对象获取对象的公共变量(JAVA)

时间:2015-05-15 15:49:48

标签: java

我有2个类:'disability'和'newDisability',我在'newDisability'中创建了一个公共变量,它存储了我正在创建的残疾的名称,但每次我从'disability'引用它时我得到原始值我使用它定义了'newDisability'的名称,而不是我用它更改它的新名称。

public class disability extends javax.swing.JFrame {
    String disability ="";

    // Button that creates a newDisability JFrame
    private void newDisabilityActionPerformed(java.awt.event.ActionEvent evt){                                                  
        newDisability new= new newDisability();
        if (new.flag==true){
            disability = new.name;
        }
    }           
}

newDisability有一个JTextField,它取值并将其存储到'name'中,但我无法从'disability'中获取这个新值

Public class newDisability extends javax.swing.JFrame {
    public String name= "ooo";
    public boolean flag=false;

    // The Button that creates the new disability and closes this window
    private void createActionPerformed(java.awt.event.ActionEvent evt) {
       name= (String) jTextField1.getText();
       flag=true;
       this.dispose();
    }
}

如何获取我在TextField中输入的新值,而不是我在“newDisability”类开头时最初定义的值(“ooo”)

3 个答案:

答案 0 :(得分:0)

您可以在newDisability类中创建两个构造函数,默认的无参数构造函数和另一个在参数中接受新nameflag值的构造函数,因此您的代码应该是这样的:

    public newDisability() {
         super();
    }
    public newDisability(String name, boolean flag) {
         this.name = name;
         this.flag = flag;
    }

您在createActionPerformed方法中的调用应该是这样的:

private void createActionPerformed(java.awt.event.ActionEvent evt) {
   newDisability new= new newDisability((String) jTextField1.getText(), true);
   this.dispose();
}

修改

或者您可以编辑代码以每次更新名称:

Public class newDisability extends javax.swing.JFrame {
public String name= "ooo";
public boolean flag=false;
private newDisability disability = new newDisability();
// The Button that creates the new disability and closes this window
private void createActionPerformed(java.awt.event.ActionEvent evt) {
   disability.name= (String) jTextField1.getText();
   disability.flag=true;
   this.dispose();
}
}

答案 1 :(得分:0)

public class disability extends javax.swing.JFrame {
String disability ="";

// Button that creates a newDisability JFrame
private void newDisabilityActionPerformed(java.awt.event.ActionEvent evt){                                                  
    newDisability new= new newDisability(this);

}

 public void setName(String name){                                                  
    this.disability= name;

}      

我认为这是因为当你按下按钮创建新的Jframe时会触发代码,并且当你刚刚创建它时会立即得到名称,这可能就是你总是得到默认值的原因。只有在newDisability的操作完成后才能获得名称。

但是,如果您将代码添加到按钮的位置,它可能会有所帮助。

还应考虑更改变量“new”的名称

修改

您可以尝试从newDisability

更新残疾的其他方式
Public class newDisability extends javax.swing.JFrame {
public String name= "ooo";
public boolean flag=false;
private disability dis;

 public newDisability(disability d)
 {
   this.dis=d;
 }

 // The Button that creates the new disability and closes this window
 private void createActionPerformed(java.awt.event.ActionEvent evt) {
    name= (String) jTextField1.getText();
    flag=true;
    this.dis.setName(name);
    this.dispose();
 }
}

}

和newDisability类

position

答案 2 :(得分:0)

newDisability方法中创建newDisabilityActionPerformed()的新实例时,您将获得默认值:

    newDisability new= new newDisability();

这将使用默认值" ooo"创建newDisability的新实例。

要使用输入的新值,您必须将具有新值的newDisability实例传递给disability类。这样您就可以访问在文本字段中输入的新值。