我正在开发一个程序,用于保存四个文本字段和文本区域的内容,并在单击“发送”按钮时将其保存到文本文件中。输出应该如下所示:
To: [Text Field]
CC: [Second Text Field]
Bcc: [Third Text Field]
Subject: [Fourth Text Field]
Message:
[Text Area]
例如,文本文件的第一行将包含“To:”,文本字段的内容,然后将跳到下一行。
虽然我的程序编译,但文本文件仍为空白。我已经尝试了我能想到的一切,但似乎无法弄清楚原因。这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class EmailProg extends JFrame implements ActionListener {
private JPanel panNorth;
private JPanel panCenter;
private JPanel panSouth;
private JLabel toLabel;
private JLabel ccLabel;
private JLabel bccLabel;
private JLabel subLabel;
private JLabel msgLabel;
private JTextField toField;
private JTextField ccField;
private JTextField bccField;
private JTextField subField;
private JTextArea msgArea;
private JButton send;
public EmailProg() {
setTitle("Compose Email");
setLayout(new BorderLayout());
panNorth = new JPanel();
panNorth.setLayout(new GridLayout(4, 2));
JLabel toLabel = new JLabel("To:");
panNorth.add(toLabel);
JTextField toField = new JTextField(15);
panNorth.add(toField);
JLabel ccLabel = new JLabel("CC:");
panNorth.add(ccLabel);
JTextField ccField = new JTextField(15);
panNorth.add(ccField);
JLabel bccLabel = new JLabel("Bcc:");
panNorth.add(bccLabel);
JTextField bccField = new JTextField(15);
panNorth.add(bccField);
JLabel subLabel = new JLabel("Subject:");
panNorth.add(subLabel);
JTextField subField = new JTextField(15);
panNorth.add(subField);
add(panNorth, BorderLayout.NORTH);
panCenter = new JPanel();
panCenter.setLayout(new GridLayout(2, 1));
JLabel msgLabel = new JLabel("Message:");
panCenter.add(msgLabel);
JTextArea msgArea = new JTextArea(5, 15);
panCenter.add(msgArea);
add(panCenter, BorderLayout.CENTER);
panSouth = new JPanel();
panSouth.setLayout(new FlowLayout());
JButton send = new JButton("Send");
send.addActionListener(this);
panSouth.add(send);
add(panSouth, BorderLayout.SOUTH);
}
public void actionPerformed (ActionEvent event) {
try {
//Write labels and corresponding fields to text file
BufferedWriter outfile = new BufferedWriter(new FileWriter("email.txt"));
outfile.write("To: ");
outfile.write(toField.getText());
//repeat for CC, Bcc, and Subject labels/fields, and for Message label/text area
}
catch(FileNotFoundException e) {
System.out.println("File not found.");
}
catch(NullPointerException j){
System.out.println("Null.");
}
catch(IOException k){
System.out.println("IO Exception.");
}
JOptionPane.showMessageDialog(this,"Saved");
}
public static void main(String[] args) {
EmailProg win = new EmailProg();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.pack();
win.setVisible(true);
}
}
提前感谢您提供的任何帮助。
答案 0 :(得分:2)
该行
outfile.write(toField.getText());
如果导致应用程序进入NullPointerException
代码块。这是因为未设置变量toField
。
要修复,您可以从构造函数中的JTextField
声明中删除toField
关键字,以将其分配给类成员变量:
toField = new JTextField(15);
类似地,对于应用程序中您可能需要在构造函数范围之外的其他组件:
ccLabel = new JLabel("CC:");
ccField = new JTextField(15);
// etc.
另一个重要注意事项 - 不要忘记在写入后关闭输出流,否则任何缓冲写入的数据都将不写入磁盘。
outfile.close();
答案 1 :(得分:1)
将JTextField toField = new JTextField(15);
更改为toField = new JTextField(15);
。这将初始化您的类成员变量,而不是创建新的局部变量。
由于您已重新定义它,因此toField
中使用的类级别变量(actionPerformed()
)未初始化,因此出现问题。