用构造函数初始化变量

用构造函数初始化变量

本文介绍了用构造函数初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级,第一个是我的主班级,第二个是我的编辑框架班级.

I have two class, first is my main class, and second class in my edit frame class.

public class RecordTableGUI extends JFrame implements ActionListener {
    String newName;
    public RecordTableGUI(String newReceivedName) {
        newName = newReceivedName;
        System.out.println("new name in new constructor : " + newName);  //prints new name correctly
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == editButton) {
            Object oldName = table.getValueAt(table.getSelectedRow(), 1);
            System.out.println("old name: " + oldName);  // prints old name correctly

            this.setVisible(false);
            new UpdateGUI(String.valueOf(oldName));
            System.out.println("new name in problem area: " + newName); // why null?
        }
    }
}

我的第二个类(UpdateGUI)在其构造函数中提供oldName,并对其进行编辑,当我单击okButton时,它将newName发送给我的第一个类.

My second class(UpdateGUI) gives oldName in it's constructor and after edit it, When i click to okButton , it send newName to my first Class.

我的第二堂课

public class UpdateGUI extends JFrame implements ActionListener {
String oldName, newName;
    public UpdateGUI(String oldname) {
    oldName = oldname;
....
}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okButton) {
    newName = tf.getText();      //tf is JTextfield
    new RecordTableGUI(newName);
    this.setVisible(false);
    }
}

我的问题是,为什么newName为null?

My problem is that Why newName is null?

更新:

public class RecordTableGUI extends JFrame implements ActionListener {
    public RecordTableGUI(String newReceivedName) {
    setNewName(newReceivedName);
}
        public void actionPerformed(ActionEvent e) {
        if (e.getSource() == editButton) {
            Object oldName = table.getValueAt(table.getSelectedRow(), 1);
        System.out.println("old name: " + oldName);

        RecordTableGUI recordObject = new RecordTableGUI();
        UpdateGUIDialog updDialog = new UpdateGUIDialog(String.valueOf(oldName), recordObject);
        }

    }

UpdateGUIDialog类:

UpdateGUIDialog Class:

public class UpdateGUIDialog extends JDialog implements ActionListener {
    RecordTableGUI recordtablegui;
    public UpdateGUIDialog(String old, RecordTableGUI recordGUI) {
    oldName = old;
    recordtablegui = recordGUI;
}
    @Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okButton) {
    newName = tf.getText();
    recordtablegui.setNewName(newName);
    this.dispose();

}
}
 }

输出:

old name:james      //prints correctly
new name: null       //prints null
new name in set method: rrr      //prints correctly

我需要打印rrr而不是null.

I need to print rrr instead of null.

推荐答案

Java对象有点像真实对象. new的用途与它的名字相同:它创建了一个新对象.让我们举一个简单的例子:

Java objects are somewhatlike real objects. And new does what its name suggests: it creates a new object. Let's take a simple example:

Box box1 = new Box();
Box box2 = new Box();
box1.fillWithCandies(candies);

box1是一个装满糖果的盒子. box2是一个不包含任何东西的盒子,因为只有box1装满了糖果.

box1 is a box filled with candies. box2 is a different box, which doesn't contain anything, because only box1 has been filled with candies.

在您的代码中,updateGUI的actionPerformed()方法使用新名称创建一个新的RecordTableGUI对象.那不会改变第一个.

In your code, updateGUI's actionPerformed() method creates a new RecordTableGUI object, with the new name. That won't change anything to the first one.

如果要updateGUI修改现有的RecordTableGUI对象,则需要具有对该对象的引用:

If you want updateGUI to modify the existing RecordTableGUI object, it needs to have a reference to this object:

public class updateGUI extends JFrame implements ActionListener {

    private RecordTableGUI recordTableGUIToUpdateWhenOKIsClicked;

    public updateGUI(RecordTableGUI recordTableGUIToUpdateWhenOKIsClicked, ...) {
        this.recordTableGUIToUpdateWhenOKIsClicked =
            recordTableGUIToUpdateWhenOKIsClicked;
        ...
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == okButton) {
            newName = tf.getText();
            this.recordTableGUIToUpdateWhenOKIsClicked.setNewName(newName);
        }
    }
}

在使用Swing之前,您应该使用简单的示例进行练习.您还应该遵守Java命名约定.并且updateGui类应该是JDialog,而不是JFrame.

You should practice with simpler examples before using Swing. You should also respect the Java naming conventions. And the updateGui class should be a JDialog, not a JFrame.

这篇关于用构造函数初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:15