【问题标题】:Modify a txt file using a JTextArea and a JButton使用 JTextArea 和 JButton 修改 txt 文件
【发布时间】:2016-01-02 20:22:14
【问题描述】:

我对 java 比较陌生,我正在尝试创建一个程序,它可以从 txt 文件(位置、标题、艺术家、标签)中读取有关不同歌曲的一系列信息,并允许用户直接修改文件通过程序中包含的 JTextArea(按下 JButton 后)。 我能够确保从程序中正确识别文件中包含的所有不同信息(实际上我可以手动添加更多歌曲而没有任何问题)但我不知道如何允许用户从JTextArea。

文件内容:

1;X;艾德希兰;庇护;

2;在孤独的时刻;SAM SMITH;CAPITOL;

3;从未如此好过;OLLY MURS;史诗;

4;四;一个方向;SYCO 音乐;

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 中的行和列,而不是 JTextArea 中的非结构化文本。看到这个tutorial
  • 还有一个List&lt;CD&gt; 包含数据的“行”而不是“列”的单独列表也可能更有意义
  • JTextArea 有读/写方法,但我同意 JasonM1,JTable 会是更好的选择

标签: java swing jbutton jtextarea


【解决方案1】:

您应该使用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();
                }
            }
        }
    });

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 2017-11-19
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多