可变数据输入记事本

Variable data entry to a notepad

我目前正在从事一个基本上是接触者追踪表的项目。我试图实现的功能是,当按下“打印”按钮时,它将数据输入到记事本文本中。虽然这个项目只是为了好玩,但我正在努力完成它,以便我可以更多地了解 java。

这是我的代码:


package com.company;

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class Main extends JFrame implements ActionListener {
    //Initialization of Form
    JPanel panel;
    JLabel Name_label;
    JLabel Address_label;
    JLabel Temp_label;
    JLabel Age_label;
    JLabel Contact_label;
    JLabel Date_label;
    JLabel msg_label;
    JLabel msg2_label;
    static JTextField address_text;
    static JTextField name_text;
    static JTextField age_text;
    static JTextField temp_text;
    static JTextField cn_text;
    static JTextField date_text;
    JButton submit, exit, print;


    Main() {
        //Name Label
        Name_label = new JLabel();
        Name_label.setText("Name: ");
        name_text = new JTextField();
        //Date Label
        Date_label = new JLabel();
        Date_label.setText("Date: ");
        date_text = new JTextField();
        //Address Label
        Address_label = new JLabel();
        Address_label.setText("Complete Address: ");
        address_text = new JTextField();
        //Contact Number
        Contact_label = new JLabel();
        Contact_label.setText("Contact Number");
        cn_text = new JTextField();
        //Age Label
        Age_label = new JLabel();
        Age_label.setText("Age:");
        age_text = new JTextField();
        //Temperature Label
        Temp_label = new JLabel();
        Temp_label.setText("Temperature: ");
        temp_text = new JTextField();
        //Submit
        submit = new JButton("SUBMIT");
        panel = new JPanel(new GridLayout(9, 3));
        exit = new JButton("EXIT");
        print = new JButton("Print");
        //Message
        msg_label = new JLabel();
        msg2_label = new JLabel();
        //Adding elements
        panel.add(Name_label);
        panel.add(name_text);
        panel.add(Date_label);
        panel.add(date_text);
        panel.add(Address_label);
        panel.add(address_text);
        panel.add(Contact_label);
        panel.add(cn_text);
        panel.add(Age_label);
        panel.add(age_text);
        panel.add(Temp_label);
        panel.add(temp_text);
        panel.add(msg_label);
        panel.add(msg2_label);
        panel.add(submit);
        panel.add(exit);
        panel.add(print);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //Listeners
        submit.addActionListener(this);
        add(panel, BorderLayout.CENTER);
        setTitle("Contact Tracing Form");
        setSize(1000, 530);
        setVisible(true);
        exit.addActionListener((event) -> System.exit(0));
        print.addActionListener(note());
    }

    private ActionListener note() {
        File file = new File("/users/agcao/contactform.txt");
        String name = name_text.getText();
        String date = date_text.getText();
        String address = address_text.getText();
        String age = age_text.getText();
        String cn = cn_text.getText();
        String temp = temp_text.getText();


        FileWriter fw = null;
        try {
            fw = new FileWriter(file.getAbsoluteFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert fw != null;
        BufferedWriter bw = new BufferedWriter(fw);
        try {
            bw.write(name);
            bw.write(date);
            bw.write(address);
            bw.write(age);
            bw.write(cn);
            bw.write(temp);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //step2: write it
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Done");
        return null;
    }

    public static void main(String[] args) throws IOException {
        new Main();
    }







    @Override
    public void actionPerformed(ActionEvent e) {
        //Initialization
        String name = name_text.getText();
        double tempe = Double.parseDouble(temp_text.getText());
        int Iage = Integer.parseInt(age_text.getText());
        //Process
        if (Iage < 15 && tempe >= 37.5)
            msg_label.setText("Sorry, " + name + ", You cannot enter this establishment due to your age and temperature!");
        else if (tempe >= 37.5)
            msg_label.setText("Sorry, " + name + ", You cannot enter this establishment due to your temperature!");
        else if (Iage < 15)
            msg_label.setText("Sorry," + name + "you cannot enter this establishment due to your age!");
        else
            msg_label.setText("Hello  " + name + ", You may enter this building. Thank you");

    }
}

    enter code here

sorry if it is too redundant or poorly made. I am still currently trying to become a better developer. Thanks

你的文件写入代码没有问题,问题是你的 return 值。在 note() 方法中,您指定它 return 是一个 ActionListener,然后将 ActionListener 添加到 print 组件。但是,如果我查看您的代码,您根本 return 没有任何 ActionListener!你刚刚return null;。修复很简单:只是 return ActionListener,而不是 null。下面是您的 note() 方法,已修改。为了简洁起见,我将其缩减为前 3 行。

private ActionListener note() {
    return new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            File file = new File("contactform.txt");
            String name = name_text.getText();
            String date = date_text.getText();
            ...
        }
    };
}

现在,上面的代码使用了一种叫做 anonymous class. I create a new subclass of ActionListener where I've overridden the actionPerformed() method, and then I return it. Because ActionListener only has one method, though, this code can be shortened even further by using a lambda expression 的东西。这是一个使用 lambda 的示例:

private ActionListener note() {
    return (event) -> {
        File file = new File("contactform.txt");
        String name = name_text.getText();
        String date = date_text.getText();
        ...
    };
}

使用 lambda 的另一种可能性是在将 ActionListener 添加到 print 组件时插入 lambda 而不是 returning ActionListener .这同样有效,无需修改 note() 方法:

print.addActionListener((event) -> note());