打开保存文件的对话框应该用 JFileChooser类import java.awt.event.*;
import javax.swing.*;public class OpenDialog extends JFrame implements ActionListener 
       {
        JButton open = new JButton("打开");
        JFileChooser chooser = new JFileChooser();
        
        public OpenDialog()
              {
               super("打开");
               setSize(300,350);
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
               JPanel pane = new JPanel();
                      open.addActionListener(this);      
                      pane.add(open);
               setContentPane(pane);
              }
    
              public void main(String[] args)
                     {
                      int state = chooser.showOpenDialog(null);
                      File file = chooser.getSeletedFile();
                      String s = "CANCELED";
                      
                      if (file!=null && state==JFileChooser.APPPOVE_OPTION)
                          {s=file.getPath();
                            // 这里加入文件输入流 (便可完成打开一个文件)
                          }
                      JOptionPane.showMessageDialog(null,s);
                      }
             }             不知道这是不是你想要的!

解决方案 »

  1.   

    啊发现了不好意思 忘记了事件处理import java.awt.event.*;
    import javax.swing.*;public class OpenDialog extends JFrame implements ActionListener 
           {
            JButton open = new JButton("打开");
            JFileChooser chooser = new JFileChooser();
            
            public OpenDialog()
                  {
                   super("打开");
                   setSize(300,350);
                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   
                   JPanel pane = new JPanel();
                          open.addActionListener(this);      
                          pane.add(open);
                   setContentPane(pane);
                  }
                  
                  public static void main(String[] args)
                         {
                            OpenDialog  opendialog = new OpenDialog();
                                        opendialog.show();
                         }
        
                  public void actionPerformed(ActionEvent e)
                         {
                          int state = chooser.showOpenDialog(null);
                          File file = chooser.getSeletedFile();
                          String s = "CANCELED";
                          
                          if (file!=null && state==JFileChooser.APPPOVE_OPTION)
                              {s=file.getPath();
                                // 这里加入文件输入流 (便可完成打开一个文件)
                              }
                          JOptionPane.showMessageDialog(null,s);
                          }
                 }
      

  2.   

    if (file!=null && state==JFileChooser.APPPOVE_OPTION) 
    修改成if (file!=null && state==JFileChooser.APPROVE_OPTION)由于我在网吧上网 没有调试环境程序会存在一些错误 最好能明白我的思路 然后去创建自己的程序
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;public class OpenDialog extends JFrame implements ActionListener 
           {
            JButton open = new JButton("打开");
            JFileChooser chooser = new JFileChooser();
            
            public OpenDialog()
                  {
                   super("打开");
                   setSize(300,350);
                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   
                   JPanel pane = new JPanel();
                          open.addActionListener(this);  
                          pane.add(text);    
                          pane.add(open);
                   setContentPane(pane);
                  }
                  
                  public static void main(String[] args)
                         {
                            OpenDialog  opendialog = new OpenDialog();
                                        opendialog.show();
                         }
        
                  public void actionPerformed(ActionEvent e)
                         {
                          int state = chooser.showOpenDialog(null);
                          File file = chooser.getSelectedFile();                      if (file!=null && state==JFileChooser.APPROVE_OPTION)
                              {
                                try{
                                   FileWriter filew = new FileWriter(file);
                                   BufferedWriter buff = new BufferedWriter(filew);
                                   boolean eof = false;
                                   while(!eof)
                                        {
                                         String line = buff.readLine();
                                         if (line==null)
                                            eof=true;
                                         else 
                                            System.out.Println(line);
                                         }
                                         buff.close();  
                               
                                  }catch(IOException e) {System.out.printf      ("Error:"+e.toMessage());}
                               
                                                                      
                               }
                         }
                 }如果有错误请与我联系 最好找一个关于JAVA IO的文档看一下,就会明白了