当前位置: 代码迷 >> Java相关 >> 关于存储的问题?
  详细解决方案

关于存储的问题?

热度:180   发布时间:2008-03-01 15:33:44.0
关于存储的问题?
如果把jTextArea中的数据,保存在你指定的文件夹里,
也就是说当你电击保存按钮时自动弹出一个保存的界面来,
最好能给出代码
小弟 先谢谢大家了!!!
搜索更多相关的解决方案: 文件夹  最好  

----------------解决方案--------------------------------------------------------
回复楼主
/*
* Created on 2008-3-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author lenovo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
/*
* Test.java
* 本程序采用直接调用文件对话框来保存文件和自己定义文件的时候来设置文件路径,从而来保存
*/

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Save extends JApplet {
    JTextArea txt = new JTextArea(5, 10);
    JButton save = new JButton("保存");

    public void init() {
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(save);
        cp.add(txt);
        save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (txt.getText().equalsIgnoreCase(""))
                    //错误对话框
                    JOptionPane.showMessageDialog(null, "没必要保存为空的文档",
                            "Message", JOptionPane.INFORMATION_MESSAGE);
                else {//自己在定义文件的时候,写下路径.
                    Object[]options={"确定","取消"};//设置options
                    int sel=JOptionPane.showOptionDialog(null,
                            "是否保存","用户决定是否保存",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE,null,
                            options,options[0]);
                    if(sel==0){//当为确定的时候,就保存
                    String s=txt.getText();
                    FileWriter fw=null;
                    File file=new File("g:\\文档.txt");//自己添加路径
                    try{
                        fw=new FileWriter(file);
                        fw.write(s);
                        fw.close();
                     }catch(Exception e1){}
                     //直接调用java的文件对话框!!!方法2
                     JTextField filename=new JTextField();
                     JTextField dir=new JTextField();
                     JFileChooser c=new JFileChooser();
                     int rVal=c.showSaveDialog(Save.this);
                     if(rVal==JFileChooser.APPROVE_OPTION){
                         filename.setText(c.getSelectedFile().getName());
                         dir.setText(c.getCurrentDirectory().toString());
                     }
                     if(rVal==JFileChooser.CANCEL_OPTION){
                         filename.setText("你按下了cancel键");
                         dir.setText("");
                     }
                    }//按下取消,则退出frame
                    else System.exit(0);
                }
            }
        });
    }
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        Save test=new Save();
        jf.setTitle("Test");
        jf.setSize(200,300);
        jf.getContentPane().add(test);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.init();
        test.start();
        jf.setVisible(true);
    }
}

[[it] 本帖最后由 Springever 于 2008-3-4 04:18 编辑 [/it]]
----------------解决方案--------------------------------------------------------
非常感谢!!
----------------解决方案--------------------------------------------------------
客气了
----------------解决方案--------------------------------------------------------
  相关解决方案