Java 将JTextField输入保存到文本文件

Java 将JTextField输入保存到文本文件,java,swing,Java,Swing,我为用户信息创建了一个表单。我需要输入JTextFields的数据保存到文本文件中。我有一个动作监听器,它在按下按钮时构建GUI。需要帮助保存数据 static class Register implements ActionListener { public void actionPerformed (ActionEvent e){ //Creates new JPanel JFrame rFrame = ne

我为用户信息创建了一个表单。我需要输入JTextFields的数据保存到文本文件中。我有一个动作监听器,它在按下按钮时构建GUI。需要帮助保存数据

        static class Register implements ActionListener {

        public void actionPerformed (ActionEvent e){
            //Creates new JPanel 
            JFrame rFrame = new JFrame ("Register, Please Enter Your Information.");
            rFrame.setVisible(true);
            rFrame.setSize(800,800);
            JPanel rPanel = new JPanel(new GridLayout(0,2));
            rFrame.add(rPanel);

            //Creates register form
            JLabel Rfirstname = new JLabel("Firstname: "); rPanel.add(Rfirstname);
            JTextField firstname = new JTextField(40); rPanel.add(firstname);
            JLabel Rsurname = new JLabel("Surname: "); rPanel.add(Rsurname);
            JTextField surname = new JTextField(40); rPanel.add(surname);
            JLabel Rdob = new JLabel("D.O.B: "); rPanel.add(Rdob);
            JTextField dob = new JTextField(40); rPanel.add(dob);
            JLabel Raddress = new JLabel("Address: "); rPanel.add(Raddress);
            JTextField address = new JTextField(40); rPanel.add(address);
            JLabel Rpostcode = new JLabel("Post Code: "); rPanel.add(Rpostcode);
            JTextField postcode = new JTextField(40); rPanel.add(postcode);
            JLabel Rallergy = new JLabel("Allergy Info: "); rPanel.add(Rallergy);
            JTextField allergy = new JTextField(40); rPanel.add(allergy);
            JLabel Rcontact = new JLabel("Contact Details: "); rPanel.add(Rcontact);
            JTextField contact = new JTextField(40); rPanel.add(contact);

    }

我会在定义文本字段的函数中编写这样的代码,在您的问题中显示的方法中:

JTextField firstName=new JTextField();
    JButton but=new JButton("Save");
    but.addActionListener(e1->{
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("asdf.txt"));
            bw.write(firstName.getText());
            bw.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    });
在本例中,我只编写firstName的文本。如果你想写所有的字段,你必须对它们进行编码或类似的操作。
此外,如果使用Windows,则必须修改路径,还必须使用/而不是\作为路径。

我将在定义文本字段的函数中编写类似的代码,以您在问题中展示的方法为例:

JTextField firstName=new JTextField();
    JButton but=new JButton("Save");
    but.addActionListener(e1->{
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("asdf.txt"));
            bw.write(firstName.getText());
            bw.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    });
在本例中,我只编写firstName的文本。如果你想写所有的字段,你必须对它们进行编码或类似的操作。
此外,如果使用Windows,则必须修改路径,还必须使用/而不是\作为路径。

添加带有ActionListener的按钮,打开新的BufferedWriternew FileWriterPath_字符串;使用BufferedWriter的方法写入字符串?变量名不应以大写字符开头。您的一些变量是正确的,其他变量则不是。保持一致!!!注意论坛如何错误地突出显示不正确的名称???添加一个带有ActionListener的按钮,打开一个新的BufferedWriternew FileWriterPath_字符串;使用BufferedWriter的方法写入字符串?变量名不应以大写字符开头。您的一些变量是正确的,其他变量则不是。保持一致!!!注意论坛如何错误地突出显示不正确的名称???如果你不想点击按钮,你可以在任何有权访问JTextfields的地方进行。看起来不错,获得此错误Lambda表达式的参数e无法重新声明在封闭范围内定义的另一个局部变量,其中“e->{'在我的示例中,每次单击“保存”时都会覆盖文件。如果要将提交内容存储到多个文件中,可以在路径或文件名中添加一些参数。如果要附加,可以在google上搜索如何添加,在Stackoverflow上有几个问题。您的问题是:您已经在ActionListene中使用了er、 只需将e更改为任何其他字符串,例如e1。我修改了上面的代码。如果您不想在单击按钮时执行此操作,您可以在任何可以访问JTextfields的地方执行此操作。看起来不错,获取此错误Lambda表达式的参数e无法重新声明在封闭范围内定义的另一个局部变量,其中“e->”{'在我的示例中,每次单击“保存”时都会覆盖文件。如果要将提交内容存储到多个文件中,可以在路径或文件名中添加一些参数。如果要附加,可以在google上搜索如何添加,在Stackoverflow上有几个问题。您的问题是:您已经在ActionListene中使用了er、 只需将e更改为任何其他字符串,例如e1。我修改了上面的代码。