Java Jframe获取输入并写入文件

时间:2017-11-17 13:54:16

标签: java swing file-io io printwriter

我有一个项目,我想从jframe获取输入。我必须在代码运行时一次输入3个人数据并将该数据写入文件名person.txt.Those数据有名称昵称年龄,当我编译我的代码时,它没有工作,我只是在person.txt文件中写入一个数据。请帮帮我?我无法在项目中获得这些内容。我该怎么办 ?

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

 }

2 个答案:

答案 0 :(得分:0)

  

我想在

中添加多个人的数据

您想使用FileWriter(而不是PrintWriter)写入文件。

然后,当您创建FileWriter时,您可以将“追加”数据添加到文件中。阅读FileWriter API以获取要使用的相应构造函数。

答案 1 :(得分:0)

经过测试和正常工作。我已经添加了下一个按钮,所以每次在StringBuilder中添加新数据时最后都可以保存那些人。你可以删除计数器(我添加了计数器只添加3个人)并制作没有限制。

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

        } 

    });
 }

}