在jbuilder中,有个选择文件框:FileChooser,但是要保存文件该怎样做阿
        有没有现成的类可以调用 阿, jbuilder中有没有组件阿 ?

解决方案 »

  1.   

    Jbuilder下写的记事本,希望对你有帮助
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.io.*; 
    public class Jishiben extends JFrame implements ActionListener{ 
    private ObjectOutputStream output; 
    private ObjectInputStream input; 
    private String fileBody=""; 
    boolean isCreate = false; 
    boolean isOpen = false; 
    boolean openFileSeccess = false; 
    boolean createFileSeccess = false; 
    private JMenuBar menubar; 
    private JMenu wenjian; 
    private JMenu bianji; 
    private JMenu bangzhu; 
    private JMenuItem xinjian; 
    private JMenuItem dakai; 
    private JMenuItem baocun; 
    private JMenuItem tuichu; 
    private JMenuItem fuzhi; 
    private JMenuItem zhantie; 
    private JMenuItem jianqie; 
    private JMenuItem chazhao; 
    private JMenuItem quanxuan; 
    private JMenuItem riqi; 
    private JMenuItem guanyu; 
    private JMenuItem help; 
    private JTextArea area; 
    private JPanel mainPanel; 
    private JPanel areaPanel; 
    public Jishiben() { 
    super("-记事本"); 
    menubar = new JMenuBar(); 
    menubar.add(wenjian = new JMenu("文件(F)")); 
    menubar.add(bianji = new JMenu ("编辑(E)")); 
    menubar.add(bangzhu = new JMenu("帮助(H)")); 
    wenjian.add(xinjian = new JMenuItem("新建(N)")); 
    wenjian.add(dakai = new JMenuItem("打开(O)")); 
    wenjian.add(baocun = new JMenuItem("保存(S)")); 
    wenjian.add(tuichu = new JMenuItem("退出(X)")); 
    bianji.add(fuzhi = new JMenuItem("复制")); 
    bianji.add(jianqie = new JMenuItem("剪切")); 
    bianji.add(zhantie = new JMenuItem("粘贴")); 
    bianji.add(chazhao = new JMenuItem("查找")); 
    bianji.add(quanxuan = new JMenuItem("全选")); 
    bianji.add(riqi = new JMenuItem("日期")); 
    bangzhu.add(guanyu = new JMenuItem("关于")); 
    bangzhu.add(help = new JMenuItem("帮助")); 
    wenjian.setFont(new Font("Dialog",0,12)); 
    bianji.setFont(new Font("Dialog",0,12)); 
    bangzhu.setFont(new Font("Dialog",0,12)); 
    xinjian.setFont(new Font("Dialog",0,12)); 
    dakai.setFont(new Font("Dialog",0,12)); 
    baocun.setFont(new Font("Dialog",0,12)); 
    tuichu.setFont(new Font("Dialog",0,12)); 
    fuzhi.setFont(new Font("Dialog",0,12)); 
    jianqie.setFont(new Font("Dialog",0,12)); 
    zhantie.setFont(new Font("Dialog",0,12)); 
    chazhao.setFont(new Font("Dialog",0,12)); 
    quanxuan.setFont(new Font("Dialog",0,12)); 
    riqi.setFont(new Font("Dialog",0,12)); 
    guanyu.setFont(new Font("Dialog",0,12)); 
    help.setFont(new Font("Dialog",0,12)); 
    wenjian.setMnemonic(′f′); 
    dakai.setMnemonic(′o′); 
    bianji.setMnemonic(′e′); 
    bangzhu.setMnemonic(′h′); 
    xinjian.setMnemonic(′n′); 
    baocun.setMnemonic(′s′); 
    tuichu.setMnemonic(′x′); 
    xinjian.addActionListener(this); 
    dakai.addActionListener(this); 
    baocun.addActionListener(this); 
    tuichu.addActionListener(this); 
    fuzhi.addActionListener(this); 
    zhantie.addActionListener(this); 
    chazhao.addActionListener(this); 
    quanxuan.addActionListener(this); 
    riqi.addActionListener(this); 
    guanyu.addActionListener(this); 
    help.addActionListener(this); 
    area = new JTextArea(); 
    Container container = getContentPane(); 
    mainPanel = new JPanel(); 
    JScrollPane areaPanel=new JScrollPane(area); 
    area.setLineWrap(true); 
    areaPanel.setWheelScrollingEnabled(true); 
    mainPanel.setLayout(new BorderLayout()); 
    container.add("Center",mainPanel); 
    mainPanel.add("North",menubar); 
    mainPanel.add("Center",areaPanel); 
    setSize(800,600); 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Dimension frameSize = getSize(); 
    if (frameSize.height > screenSize.height) { 
    frameSize.height = screenSize.height; 

    if (frameSize.width > screenSize.width) { 
    frameSize.width = screenSize.width; 

    setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 
    setVisible(true); 
    show(); 
    } public void actionPerformed( ActionEvent e) { 
    if(e.getSource()==xinjian) 
    xinjian(); 
    if(e.getSource()==dakai) 
    dakai(); 
    if(e.getSource()==baocun) 
    baocun(); 
    if(e.getSource()==tuichu) 
    tuichu(); 
    if(e.getSource()==guanyu) 
    guanyu(); 
    if(e.getSource()==help) 
    help(); 
    if(e.getSource()==fuzhi) 
    area.copy(); 
    if(e.getSource()==jianqie) 
    area.cut(); 
    if(e.getSource()==zhantie) 
    area.paste(); 

    private void xinjian() 

    createFile(); 
    if(createFileSeccess == true) 
    isCreate = true; 

    private void dakai() { 
    openFile(); 
    if(openFileSeccess == true) 

    isOpen = true; 
    try{ 
    fileBody = (String)input.readObject(); 
    area.setText(fileBody); 
    } catch(ClassNotFoundException cnfex) 

    JOptionPane.showMessageDialog(this,"Unable to create object","Class not Found", 
    JOptionPane.ERROR_MESSAGE); 

    catch(IOException io) 

    JOptionPane.showMessageDialog(this,"Error during read from file","Read Error", 
    JOptionPane.ERROR_MESSAGE); 



    private void baocun()/
    { if(isCreate==true) 

    try{ 
    fileBody = area.getText(); // 
    output.writeObject(fileBody); // 
    output.flush(); 
    isCreate=false; 
    isOpen = false; // 
    } catch(IOException io) // 

    closeFile(); // 


    if(isOpen==true) 

    createFile(); 
    if(createFileSeccess==true) 

    try{ 
    fileBody = area.getText(); // 
    output.writeObject(fileBody); // 
    output.flush(); 
    } catch(IOException io) // 

    closeFile(); // 



    if(isCreate!=true&&isOpen!=true) 
    {
    createFile(); 
    if(createFileSeccess==true) 

    isCreate = true; // 
    try{ 
    fileBody = area.getText(); 
    output.writeObject(fileBody); // 
    output.flush(); 

    // 
    catch(IOException io) 

    closeFile(); 

    }//end if 


    private void tuichu() { 
    System.exit(0); 

    private void closeFile() 

    try{ 
    output.close(); 
    System.exit(0); 

    catch(IOException ex){ 
    JOptionPane.showMessageDialog(this, 
    "Error closing file", 
    "Error",JOptionPane.ERROR_MESSAGE); 
    System.exit(1); 

    } private void openFile() 

    JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setFileSelectionMode( 
    JFileChooser.FILES_ONLY ); 
    int result = fileChooser.showOpenDialog( this ); 
    if ( result == JFileChooser.CANCEL_OPTION ) 
    return; 
    File fileName = fileChooser.getSelectedFile(); 
    if ( fileName == null || 
    fileName.getName().equals( "" ) ) 
    JOptionPane.showMessageDialog( this, 
    "Invalid File Name", 
    "Invalid File Name", 
    JOptionPane.ERROR_MESSAGE ); 
    else { 
    // Open the file 
    try { 
    input = new ObjectInputStream( 
    new FileInputStream( fileName ) ); 
    openFileSeccess = true;//// 
    dakai.setEnabled( true ); 

    catch ( IOException e ) { 
    JOptionPane.showMessageDialog( this, 
    "Error Opening File", "Error", 
    JOptionPane.ERROR_MESSAGE ); 



    //////// 
    //create a file 
    private void createFile() 

    JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setFileSelectionMode( 
    JFileChooser.FILES_ONLY ); 
    int result = fileChooser.showSaveDialog( this ); 
    // clicked Cancel button on dialog 
    if ( result == JFileChooser.CANCEL_OPTION ) 
    return; 
    File fileName = fileChooser.getSelectedFile(); 
    if ( fileName == null || 
    fileName.getName().equals( "" ) ) 
    JOptionPane.showMessageDialog( this, 
    "Invalid File Name", 
    "Invalid File Name", 
    JOptionPane.ERROR_MESSAGE ); 
    else { 
    // Open the file 
    try { 
    output = new ObjectOutputStream( 
    new FileOutputStream( fileName ) ); 
    createFileSeccess = true; 
    xinjian.setEnabled( true ); 

    catch ( IOException e ) { 
    JOptionPane.showMessageDialog( this, 
    "Error Opening File", "Error", 
    JOptionPane.ERROR_MESSAGE ); 



    private void guanyu() 

    JOptionPane.showMessageDialog(this," java版记事本 "+ "\n" + 
    " By yellow ","关于记事本",2); 

    private void help() 

    JOptionPane.showMessageDialog(this,"复制(Ctrl+c),粘贴(Ctrl+v), 剪切(Ctrl+x) ", 
    "help",2); 

    public static void main(String args[]) 

    Jishiben app = new Jishiben(); 
    app.addWindowListener( 
    new WindowAdapter(){ 
    public void windowClosing(WindowEvent e) 

    System.exit(0); 


    ); 

    }
      

  2.   

    FileDialog(this,"Name",FileDialog.SAVE)
      

  3.   

    问题以解决
    FileChooser fc=new FileChooser();
    fc.showSaveDialog();
    谢谢laughsmile(海边的星空) 、 hjzhank(译名)