各位老大帮帮忙,找了半天也没有发现给JFileChooser 里面的撤销按钮加事件的地方,还有就是那个JTextField里面怎么设置文件名啊?

解决方案 »

  1.   

    你干嘛要要给按钮加事件?
    你直接给jFileChooser加就行了。
    或者用int n = fc.showOpenDialog(this);
            if (n == JFileChooser.APPROVE_OPTION) {}
            else if (n == .....
    做处理
    jFileChooser.setSelectedFile可以设置textfield里面的文件名。
      

  2.   

    JFileChooser jf = new JFileChooser();
    int returnVal = jf.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      //添加点击打开按钮后的事件
    } else {
      //添加点击撤销按钮后的事件
    }
    JTextField里的文件名不是会自动根据你所选的文件设上去的吗?不需要你自己设啊
      

  3.   

    帮你顶~~~~~~~~~~~~
    ------------------------------------
    体验速度,体验CSDN新版论坛助手:http://community.csdn.net/Expert/TopicView.asp?id=3108679
      

  4.   

    if (fileChooser.showOpenDialog(null) == JFileChooser.CANCEL_OPTION)// 这就构成一个Cancel事件啦
    {}
      

  5.   

    int n = fc.showOpenDialog(this);
            if (n == JFileChooser.APPROVE_OPTION) {}
            else if (n == .....
    我运行类似的代码,然后出现的对话框中,我选中一个txt文档,按“打开”,然后不停的出现这个对话框,根本打不开那个txt文档,为啥呢?
      

  6.   

    public class FileAccessApplet extends JApplet {
      private JTextField
        filename = new JTextField(),
        dir = new JTextField();
      private JButton
        open = new JButton("Open"),
        save = new JButton("Save");
      private JEditorPane ep = new JEditorPane();
      private JScrollPane jsp = new JScrollPane();
      private File file;
    class OpenL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          JFileChooser c = new JFileChooser();
          c.setFileFilter(new TextFileFilter());
          // Demonstrate "Open" dialog:
          int rVal = c.showOpenDialog(FileAccessApplet.this);
          if(rVal == JFileChooser.APPROVE_OPTION) {
            file = c.getSelectedFile();
            filename.setText(file.getName());
            dir.setText(c.getCurrentDirectory().toString());
            try {
              System.out.println("Url is " + file.toURL());
              ep.setPage(file.toURL());
              // ep.repaint();
            } catch (IOException ioe) {
              throw new RuntimeException(ioe);
            }
          }
          if(rVal == JFileChooser.CANCEL_OPTION) {
            filename.setText("You pressed cancel");
            dir.setText("");
          } else {
            save.setEnabled(true);
          }
        }
      }
      class SaveL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          JFileChooser c = new JFileChooser(file);
          c.setSelectedFile(file);
          // Demonstrate "Save" dialog:
          int rVal = c.showSaveDialog(FileAccessApplet.this);
          if(rVal == JFileChooser.APPROVE_OPTION) {
            filename.setText(c.getSelectedFile().getName());
            dir.setText(c.getCurrentDirectory().toString());
            try {
              FileWriter fw = new FileWriter(file);
              ep.write(fw);
            } catch (IOException ioe) {
              throw new RuntimeException(ioe);
            }
          }
          if(rVal == JFileChooser.CANCEL_OPTION) {
            filename.setText("You pressed cancel");
            dir.setText("");
          }
        }
      }
    }