Java Jframe get input and write to file [duplicate]

懵懂的女人 提交于 2021-02-05 12:19:24

问题


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

 }

回答1:


I want to add more than one person's data in

You want to write to the file using a FileWriter (not a PrintWriter).

Then when you create the FileWriter you can "append" data to the file. Read the FileWriter API for the appropriate constructor to use.




回答2:


Tested and working fine.I have added next button so each time you append new data in StringBuilder and at the end you can save that persons.You can remove the counter(i have added counter to add only 3 persons) and make it without limit.

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaGui {
JFrame f;
JLabel l1, l2, l3;
JTextField tf1, tf2, tf3;
JButton b,next;
int counter;

StringBuilder data = new StringBuilder();
JavaGui() {}

public static void main(String[] args) {

    FileWriter fw = null;
    try{
        fw = new FileWriter("person.txt");
        PrintWriter pw = new PrintWriter(fw);
        new JavaGui().go(pw);

       }catch (IOException ex) {
        Logger.getLogger(JavaGui.class.getName()).log(Level.SEVERE, null, ex);
    }


}

public void go(PrintWriter pw) {

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

    next = new JButton("Next");
    next.setBounds(100, 200, 70, 30);

    b = new JButton("Save");
    b.setBounds(180, 200, 70, 30);

    f.add(l1);f.add(tf1);
    f.add(l2);f.add(tf2);
    f.add(l3);f.add(tf3);
    f.add(next);
    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);

    next.addActionListener(new ActionListener() {

        @Override
        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.append(name + " " + nickname + " " + ageS);
               data.append(System.getProperty("line.separator"));

               counter++;

               tf1.setText("");
               tf2.setText("");
               tf3.setText("");

               if(counter > 2)
                   next.setEnabled(false);
        }

    });

    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

            pw.print(data.toString());
            pw.close();

        } 

    });
 }

}


来源:https://stackoverflow.com/questions/47352224/java-jframe-get-input-and-write-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!