I have one project that I want to get input from jframe. I have to input 3 people data in only one time when code was running and write that data into file name person.txt.Those data have Name Nickname Age and when i compile my code, It dosn't work and I just only write one data in person.txt file. Help me please? I can't get those in my project. what should i do ?
public class JavaGui {
public String data = "";
JFrame f;
JLabel l1, l2, l3;
JTextField tf1, tf2, tf3;
JButton b;
JavaGui() {
FileWriter fw = null;
try {
fw = new FileWriter("person.txt");
} catch (IOException ex) {
Logger.getLogger(JavaGui.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter pw = new PrintWriter(fw);
f = new JFrame("Java GUI");
l1 = new JLabel("Name");
l1.setBounds(20, 50, 80, 30);
tf1 = new JTextField();
tf1.setBounds(100, 50, 200, 30);
l2 = new JLabel("Nickname");
l2.setBounds(20, 100, 80, 30);
tf2 = new JTextField();
tf2.setBounds(100, 100, 200, 30);
l3 = new JLabel("Age");
l3.setBounds(20, 150, 80, 30);
tf3 = new JTextField();
tf3.setBounds(100, 150, 200, 30);
b = new JButton("Save");
b.setBounds(100, 200, 70, 30);
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(tf2);
f.add(l3);
f.add(tf3);
f.add(b);
f.setSize(350, 350);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < 3; i++) {
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = "";
String nickname = "";
String ageS = "";
int age = 0;
try {
name = "Name : " + tf1.getText();
nickname = "Nickname : " + tf2.getText();
age = Integer.parseInt(tf3.getText());
if (age > 0 && age < 100) {
ageS = "Age : " + age;
} else {
JOptionPane.showMessageDialog(null, "Please Enter Age field by Positive number Or Less than 100");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter Age field by number type");
}
data += name + " " + nickname + " " + ageS;
pw.println(data);
pw.close();
}
});
}
}
public static void main(String[] args) {
new JavaGui();
}
}