java - 使用构造函数初始化变量

标签 java swing

我有两个类,第一个是我的主类,第二个是我的编辑框架类。

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 发送到我的第一个类。

我的第二堂课:

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 为空?

更新:

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);
        }

    }

更新GUIDialog类:

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。

最佳答案

Java 对象有点像真实的对象。而 new 的作用正如它的名字所暗示的那样:它创建了一个新对象。让我们举一个简单的例子:

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

box1 是一个装满糖果的盒子。 box2 是一个不同的盒子,里面什么都没有,因为只有 box1 装满了糖果。

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

如果要updateGUI修改已有的RecordTableGUI对象,需要有这个对象的引用:

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。

关于java - 使用构造函数初始化变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177249/

相关文章:

java - 无法在react-native模拟器上运行应用程序

java - 如何访问try-catch中的变量?

java - 如何将字符串解析为 java.sql.date

java - 更改实例化对象的值

java - 向 JTable 添加组件

java - 将 32 位整数拆分为 4 个 8 位整数

java - 使用 Linux 重新启动 Java 应用程序

java - java swing如何从不同的操作系统绘制到屏幕上?

java - 维度对象的优点和缺点?

java - 慢速 Swing GUI 启动时间