我目前正在做一个项目,基本上是一个接触跟踪表单。我试图实现的功能是,当"打印"时,按钮按下,它将数据输入到记事本文本。虽然这个项目只是为了好玩,但我正努力完成它,以便我能更多地了解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
您的文件编写代码没有问题,问题在于您的返回值。在note()
方法中,指定它返回一个ActionListener
,并将该ActionListener
添加到print
组件中。但是如果我看一下你的代码,你根本没有返回任何ActionListener
!你只需要return null;
。修复方法很简单:只返回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();
...
}
};
}
现在,上面的代码使用了一个叫做匿名类的东西。我创建了ActionListener
的一个新子类,其中我已经覆盖了actionPerformed()
方法,然后我返回它。但是,由于ActionListener
只有一个方法,因此可以通过使用lambda表达式进一步缩短此代码。下面是一个使用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,而不是返回ActionListener
。如果不修改note()
方法,这也可以正常工作:
print.addActionListener((event) -> note());