gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:27:35 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com