我想在JTxteArea中编辑文件,通过文件保存对话框保存,请教如何实现?

解决方案 »

  1.   

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestFile extends JFrame implements ActionListener{
    JTextArea jta = new JTextArea();
    JButton jb = new JButton("保存");
    public TestFile(){
    Container container = this.getContentPane();
    container.add(jta);
    container.add(jb,"South");
    jb.addActionListener(this);
    this.setLocation(200,200);
    this.setSize(300,400);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e){
    String content = jta.getText();
    java.awt.FileDialog fd = new FileDialog(this,"save",FileDialog.SAVE);
    fd.setVisible(true);
    String name = fd.getFile();
    String directory = fd.getDirectory();
    String savePath = directory + name + ".txt";
    System.out.println (savePath);
    try{
    FileWriter fw = new FileWriter(savePath);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();
    fw.close();
    }catch(Exception e1){
    e1.printStackTrace();
    }
    }
    public static void main(String[] args){
    new TestFile();

    }
    }
      

  2.   

    注册个 SDN 成员,订阅他的 Java Newsletter 周刊。developer.sun.com 每个星期给你一篇文章,一般每篇文章讲到 3- 6 个知识点。你也可以到邮件中提到的站点上搜索 以前的每期文章。