gpt4 book ai didi

java - 用户输入 GUI 的值未保存?

转载 作者:行者123 更新时间:2023-12-02 06:56:34 26 4
gpt4 key购买 nike

下面是我正在编写的代码。基本上我需要一个框来打开并让用户在相应的文本字段中输入数据。一切都运转良好。当我从 actionPerformed 方法打印时,所有内容都正确输出,但是当我从 main 调用 gui.displayPersonInfo 方法时,它将所有值显示为 null。它在盒子打开之前首先执行 displayPersonInfo 方法,即使我在之后调用该方法。有人知道我的代码有什么问题吗? (输出如下)

package userInput;

import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Person extends JFrame{

String Name;
String Address;
String PhoneNumHome;
String PhoneNumWork;
String email;

JLabel label, label2;
JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8;
JButton button;

public Person(){
setLayout(new FlowLayout());
label = new JLabel("Enter your name");
add(label);
tf1 = new JTextField(10);
add(tf1);

label = new JLabel("Enter your Address (street number + street name)");
add(label);
tf2 = new JTextField(10);
add(tf2);

label = new JLabel("Enter your city");
add(label);
tf3 = new JTextField(10);
add(tf3);

label = new JLabel("Enter your province");
add(label);
tf4 = new JTextField(10);
add(tf4);

label = new JLabel("Enter your postal code");
add(label);
tf5 = new JTextField(10);
add(tf5);

label = new JLabel("Enter your home phone number (306-xxx-xxx)");
add(label);
tf6 = new JTextField(10);
add(tf6);

label = new JLabel("Enter your work phone number (306-xxx-xxx)");
add(label);
tf7 = new JTextField(10);
add(tf7);

label = new JLabel("Enter your email (user@emailservice.xxx");
add(label);
tf8 = new JTextField(10);
add(tf8);

button = new JButton("Next");
add(button);

event e = new event();
button.addActionListener(e);
}

public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
String address1, pnum, wnum, a;
try{
String word = tf1.getText();
Name = word;
System.out.println(Name);
Address = tf2.getText();
Address = Address + " " + tf3.getText();
Address = Address + " " + tf4.getText();
Address = Address + " " + tf5.getText();
address1 = Address;
System.out.println(Address);
PhoneNumHome = tf6.getText();
pnum = PhoneNumHome;
PhoneNumWork = tf7.getText();
wnum = PhoneNumWork;
email = tf8.getText();
a = email;
System.out.println(PhoneNumHome);
System.out.println(PhoneNumWork);
System.out.println(email);

saveInfo(word, address1, pnum, wnum, a);
displayPersonInfo();
System.exit(0);

}catch(Exception ex){}
}
}

public void displayPersonInfo(){
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Home Phone Number: " + PhoneNumHome);
System.out.println("Work Phone Number: " + PhoneNumWork);
System.out.println("Email: " + email);
}

public void saveInfo(String name, String address, String Hphone, String Wphone, String Email){
Name = name;
Address = address;
PhoneNumHome = Hphone;
PhoneNumWork = Wphone;
email = Email;
}

public static void main(String[] args) {
Person gui = new Person();

gui.displayPersonInfo();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Enter Information");
gui.setSize(350,330);
gui.setLocation(500,250);
gui.setVisible(true);

}

}

这里是输出:(它的作用就好像 displayPersonInfo 首先发生)
运行:
姓名:空
地址:空
家庭电话号码: null
工作电话号码: null
邮箱:空
名称(现在它从 actionPerformed 中打印它)一个地址一个城市一个省一个邮政编码
一个数字
另一个号码
一封电子邮件
姓名:一个名字
地址:一个地址一个城市一个省一个邮政编码
家庭电话号码:一个号码
工作电话号码:另一个号码
电子邮件:一封电子邮件
构建成功(总时间:20 秒)

最佳答案

您的 saveInfo 函数似乎是多余的,因为您在调用它之前已经设置了每个值,但我不知道为什么 saveInfo 将这些值清空。尝试在 saveInfo 之前调用 displayPersonInfo 并看看会发生什么。

关于java - 用户输入 GUI 的值未保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256817/

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