我在Java(GUI)中创建一个程序,当你填写TextFields并单击enter;姓名,年龄,电子邮件,国籍和手机号码将保存在名为 StoredInfo.txt 的文本文件中。我创建的程序没有删除您输入的数据如果你再次填写文本字段。
我想要做的是使用我创建的清除数据按钮,它将删除存储在文本文件中的所有数据( StoredInfo.txt )。
这是我的计划:
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SignUp extends JFrame implements ActionListener
{
//Variables
private JButton enter,clear;
private JLabel header,name,age,email,nationality,cellno;
private JTextField nameTF,ageTF,emailTF,nationalityTF,cellnoTF;
private Container container;
private PrintWriter pwriter;
//Constructor
public SignUp()
{
setTitle("Form");
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
container = this.getContentPane();
container.setBackground(Color.GRAY);
enter = new JButton("Enter");
clear = new JButton("Clear Data");
header = new JLabel("Form");
name = new JLabel("Name: ");
age = new JLabel("Age: ");
email = new JLabel("Email Address: ");
nationality = new JLabel("Nationality: ");
cellno = new JLabel("Cellphone #: ");
nameTF = new JTextField(20);
ageTF = new JTextField(20);
emailTF = new JTextField(20);
nationalityTF = new JTextField(20);
cellnoTF = new JTextField(20);
nameTF.addActionListener(this);
ageTF.addActionListener(this);
emailTF.addActionListener(this);
nationalityTF.addActionListener(this);
cellnoTF.addActionListener(this);
enter.addActionListener(this);
clear.addActionListener(this);
//Add section
this.add(header);
this.add(name);
this.add(age);
this.add(email);
this.add(nationality);
this.add(cellno);
this.add(header);
this.add(nameTF);
this.add(ageTF);
this.add(emailTF);
this.add(nationalityTF);
this.add(cellnoTF);
this.add(clear);
this.add(enter);
//SetBounds
enter.setBounds(180,270,80,40);
clear.setBounds(270,270,100,40);
header.setBounds(230,30,80,50);
header.setFont(new Font("Arial",Font.BOLD,25));
header.setForeground(Color.WHITE);
name.setBounds(80,90,40,40);
age.setBounds(80,120,40,40);
email.setBounds(80,150,110,40);
nationality.setBounds(80,180,100,40);
cellno.setBounds(80,210,100,40);
nameTF.setBounds(180,95,190,25);
ageTF.setBounds(180,125,190,25);
emailTF.setBounds(180,155,190,25);
nationalityTF.setBounds(180,185,190,25);
cellnoTF.setBounds(180,215,190,25);
name.setForeground(Color.WHITE);
age.setForeground(Color.WHITE);
email.setForeground(Color.WHITE);
nationality.setForeground(Color.WHITE);
cellno.setForeground(Color.WHITE);
//Setting Up Text File
try
{
File data = new File("StoredInfo.txt");
pwriter = new PrintWriter(new FileWriter(data,false));
if(data.exists())
{
}else
{
data.createNewFile();
}
}catch(Exception e)
{
e.printStackTrace();
}
setVisible(true);
}
//Actions
public void actionPerformed(ActionEvent e)
{
Object action = e.getSource();
if(action.equals(enter))
{
pwriter.println("Name: " + nameTF.getText());
pwriter.println("Age: " + ageTF.getText());
pwriter.println("Email: " + emailTF.getText());
pwriter.println("Nationality: " + nationalityTF.getText());
pwriter.println("CellNo #: " + cellnoTF.getText());
pwriter.println("---------------------------");
pwriter.flush();
pwriter.close();
}else if(action.equals(clear))
{
}
}
///Main
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SignUp();
}
});
}
}
答案 0 :(得分:0)
一般来说,new FileWriter(file);
会覆盖file
,将其留空。
在你的情况下,
else if (action.equals(clear)) {
// Need to close this first to avoid resource leak
pw.close();
File data = new File("StoredInfo.txt");
// I believe you will need pw later
pw = new PrintWriter(new FileWriter(data, false));
}
希望有所帮助。