求关于Jfilechooser选择保存路径的代码,我的意思就是:File file= new file("路径"),现在那个路径我通过jfilechooser选择,求代码

解决方案 »

  1.   

    http://www.java2s.com/Tutorial/Java/0240__Swing/1260__JFileChooser.htm
      

  2.   

    能不能告诉我该用哪个,初次接触JAVA,望指点,最好源代码
      

  3.   

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;public class FileChooserDemo {
        
        private JFileChooser chooser = new JFileChooser();
        private JButton saveButton = new JButton("Save");
        private JLabel label = new JLabel("  ");
        
        public FileChooserDemo(){
    JFrame frame = new JFrame("JFileChooser Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//选择目录,选择文件的话注释掉这行。

    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    saveButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        int retValue = chooser.showSaveDialog(saveButton);
        if(retValue == JFileChooser.APPROVE_OPTION){
    label.setText(chooser.getSelectedFile().toString());
        }
    }
        }); frame.getContentPane().add(saveButton,BorderLayout.CENTER);
    frame.getContentPane().add(label,BorderLayout.SOUTH);
    frame.setPreferredSize(new Dimension(200,100));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
        }
            public static void main(String[] args){
    EventQueue.invokeLater(new Runnable(){
    public void run(){
        new FileChooserDemo();
    }
        });
        }
    }
      

  4.   

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;public class FileChooserDemo {
        
        private JFileChooser chooser = new JFileChooser();
        private JButton saveButton = new JButton("Save");
        private JLabel label = new JLabel("  ");
        
        public FileChooserDemo(){
    JFrame frame = new JFrame("JFileChooser Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//选择目录,选择文件的话注释掉这行。
    saveButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        int retValue = chooser.showSaveDialog(saveButton);
        if(retValue == JFileChooser.APPROVE_OPTION){
    label.setText(chooser.getSelectedFile().toString());
        }
    }
        }); frame.getContentPane().add(saveButton,BorderLayout.CENTER);
    frame.getContentPane().add(label,BorderLayout.SOUTH);
    frame.setPreferredSize(new Dimension(200,100));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
        }
            public static void main(String[] args){
    EventQueue.invokeLater(new Runnable(){
    public void run(){
        new FileChooserDemo();
    }
        });
        }
    }
      

  5.   

    要求很简单,用利用jfilechooser,通过输入要保存的文件名,在可视化目录下生成那个文件,各位大仙帮帮忙,楼上的高手,你给的不是我想要的啊
      

  6.   


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    public class Test extends JFrame implements ActionListener {
        JPanel pnlMain;
        JTextField txtfile;
        JButton btnSelect;
        JFileChooser fc = new JFileChooser();
        public Test() {
            pnlMain = new JPanel();
            this.getContentPane().add(pnlMain);
            txtfile = new JTextField(10);
            btnSelect = new JButton("选择");
            btnSelect.addActionListener(this);
            pnlMain.add(txtfile);
            pnlMain.add(btnSelect);
        }    public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnSelect) {
                fc.setSelectedFile(new File(txtfile.getText()));
                int intRetVal = fc.showOpenDialog(this);
                if (intRetVal == JFileChooser.APPROVE_OPTION) {
                    txtfile.setText(fc.getSelectedFile().getPath());
                }
            }
        }
        public static void main(String[] args) {
            JFrame f = new Test();
            f.setSize(200, 300);
            f.setVisible(true);
        }
    }这样?