0

好的,我正试图让这个小程序写入文件,我错过了什么?它创建文件但不会写入文件,只是继续清除框以准备下一个条目。我以前这样做过,但不是用Applet。谁能帮助我。谢谢

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class collectGui extends JApplet implements ActionListener {

    // PAINT METHOD
    public void paint(Graphics g)
    {
        Font font = new Font("Arial", Font.BOLD, 18);
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString("Enter Client Name And Phone Number", 40, 25);
        resize(400, 400);
        this.setBackground(Color.BLUE);
    }

    // CREATES OBJECTS
    private static final long serialVersionUID = 1L;
    TextField text1;
    TextField text2;
    Label label1, label2,label3;
    Button button;
    Font font = new Font("Arial", Font.BOLD, 11);

    // INIT METHOD
    public void init() {
        setLayout(null);
        repaint();

        // YOUR CLIENTS NAME
        label1 = new Label("Clients Name: ");
        label1.setBounds(65, 47, 150, 25);
        setFont(font);
        add(label1);

        // CLIENT NAME ENTRY
        text1 = new TextField(5);
        text1.setBounds(225, 46, 125, 25);
        add(text1);

        // CLIENT PHONE NUMBER
        label2 = new Label("Client Phone Number: ");
        label2.setBounds(65, 100, 150, 25);
        setFont(font);
        add(label2);

        // CLIENT PHONE NUMBER ENTRY BOX
        text2 = new TextField(5);
        text2.setBounds(225, 100, 125, 25);
        add(text2);

        //LABEL 3 TELLS FILE WAS WRITTEN
        label3 = new Label ("");
        label3.setBounds(50,235,300,125);
        setFont(font);
        add(label3);

        // BUTTON
        button = new Button("Enter");
        button.setBounds(160, 175, 100, 20);
        add(button);

        // ACTION LISTENER
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
       //CODE TO WRITE TO FILE
           String number = text2.getText();
           //Integer.parseInt(number);
            String name = text1.getText();
            try
            {
            Path file =
            Paths.get("C:\\Users\\User\\Desktop\\college     assignments\\1Java2\\FinalApp\\Data.txt");
            label3.setText("             You Created A  Data.txt file");
                String s = "";
                String delimiter = ",";
                OutputStream output = new BufferedOutputStream(Files.newOutputStream(file,CREATE));
                 BufferedWriter writer = new
                 BufferedWriter(new OutputStreamWriter(output));

                //FORMATS THE WAY THE ENTRY IS SAVED TO THE FILE
                    s = "Name =  " + name + delimiter + "Number =  " + number;
                    writer.write(s, 0, s.length());
                    writer.newLine();
                    //CLEAR
                    text1.setText("");
                    text2.setText("");
                    writer.close();
            }
            catch(Exception e) {
                    label3.setText("message:  " + e);
            } 
        }
    //output.flush();
    //writer.close();
    //input.close();
    //x.close();

//output.flush();
//writer.close();

//input.close();
//x.close();
    }   
4

0 回答 0