以下是我正在處理的代碼。基本上我需要打開一個盒子並讓用戶在相應的文本字段中輸入他們的數據。一切都很好。當我從actionPerformed方法打印時,所有東西都輸出正確,但是當我從main調用gui.displayPersonInfo方法時,它將所有值顯示爲null。在盒子打開之前,它正在執行displayPersonInfo方法,即使我之後調用方法。任何人都知道我的代碼有什麼問題? (下面輸出)將用戶輸入到GUI的值不會保存嗎?
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 ([email protected]");
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
電子郵箱:null
A名稱(現在將其從actionPerformed中打印出來) 個的ADRESS一市一省郵政編碼
一些
另一個號碼
電子郵件
名:名稱
地址:一個ADRESS一市一省郵政編碼
家庭電話號碼:一些
工作電話號碼:其他號碼
電子郵件:電子郵件
BUILD SUCCESSFUL(總時間:20秒)
你有沒有試圖調試你的程序,或者你想我們其中的一個人來幫助你解決一個簡單的問題? – Tdorno