簡體   English   中英

使用JTextArea和JButton修改txt文件

[英]Modify a txt file using a JTextArea and a JButton

我是Java的新手,我正在嘗試創建一個程序,該程序從txt文件(位置,標題,藝術家,標簽)讀取一系列有關不同歌曲的信息,並允許用戶直接通過JTextArea修改文件包含在程序中(按下JButton之后)。 我能夠確保從程序中正確識別文件中包含的所有不同信息(實際上,我可以手動添加更多歌曲而沒有任何問題),但是我不知道如何允許用戶從文件中修改文件。 JTextArea中。

文件內容:

1; X; ED SHEERAN; ASYLUM;

2;在寂寞的小時中; SAM SMITH; CAPITOL;

3;從來沒有更好; OLLY MURS;史詩;

4; 4;一個方向;西科音樂;

5;希望航行;喬治·埃茲拉;哥倫比亞;

CD面板:

import java.io.*;
import java.util.*;
import javax.swing.*;

public class CDPanel {

private static String newLine = "\n";
public static CD myCD;
public static JTextArea myTextArea = new JTextArea(15, 30);

public static void main(String[] args) throws FileNotFoundException, IOException {

    List<Integer> position = new ArrayList<>();
    List<String> title = new ArrayList<>();
    List<String> artist = new ArrayList<>();
    List<String> label = new ArrayList<>();

    JButton addCD = new JButton("Press to add the CD");

    JFrame frame = new JFrame("CD List");
    JPanel panel = new JPanel();

    int numberOfLines = 0;

    BufferedReader br = new BufferedReader(new FileReader("cd.dat"));
    String line = null;
    while ((line = br.readLine()) != null) {
        String data[] = line.split(";");

        for (int i=0; i<4; i++) {
            if (i == 0) {
                int value = Integer.parseInt(data[i]);
                position.add(value);
            }

            if (i == 1) {
                title.add(data[i]);
            }

            if (i == 2) {
                artist.add(data[i]);
            }

            if (i == 3) {
                label.add(data[i]);
            }
        }
        numberOfLines++;
    }

    for (int i=0; i<numberOfLines; i++) {
        myCD = new CD(position.get(i), title.get(i), artist.get(i), label.get(i));
        myTextArea.append(String.valueOf(myCD + newLine));
    }
    panel.add(myTextArea);
    panel.add(addCD);
    frame.add(panel);
    frame.setSize(30, 15);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

光盤:

public class CD {

public int position;
public String title, artist, label;

public CD (int positionInit, String titleInit, String artistInit, String labelInit) {
    position = positionInit;
    title = titleInit;
    artist = artistInit;
    label = labelInit;
}

public String toString() {
return position + ";" + title + ";" + artist + ";" + label + ";";
}
}

在此先感謝您的幫助,如果我的代碼(和我的英語)不完美,請多謝。但是,再次,我是這個世界的新手,並且我正嘗試盡快提高自己的知識:)

然后,您應該使用JTable或信任用戶在TextArea對其進行修改;

ActionListener添加到JButton並使用myTextArea.write()寫入cd.dat文件。

JButton addCD = new JButton("Press to add the CD");
addCD.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            FileWriter fw = null;
            try {
                fw = new FileWriter("cd.dat");
                myTextArea.write(fw);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    fw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM