我写了一段目录选择的代码,可是弹出的目录选择的窗口不能正常关闭,点击关闭和取消时都变成最小化了,必须再点击一次关闭按钮才可以关闭,代码如下: JFileChooser chooser = new JFileChooser();         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只能选择目录
         int returnVal = chooser.showOpenDialog(parent);
         if(returnVal == JFileChooser.APPROVE_OPTION) {
          selectPath =chooser.getSelectedFile().getPath() ;           chooser.hide();
           startCreateOperation();
       
         } 
         
请问各位,我应该如何正常关闭JFileChooser弹出来的窗口呢?

解决方案 »

  1.   

    不用使用chooser.hide();选择完成之后它自动就关闭了。
      

  2.   

    参考一下public class JFileChooserDemo extends JFrame{
    private static final long serialVersionUID = 1L;
    private JFrame jFrame = null;
    private JButton jFileButton = null;
    JFileChooser jfChooser = new JFileChooser("D:\\..\\..");
    public JFileChooserDemo(){
    jFrame = this;
    this.setSize(496,260);
    jFileButton = new JButton("导入文件");
    jFrame.add(jFileButton);
    jfChooser.setDialogTitle("导入文件");
    jfChooser.setFileFilter(new FileFilter() {
    @Override
    public boolean accept(File f){
    if(f.getName().endsWith("data") || f.isDirectory()) return true;
    return false;
    }
    @Override
    public String getDescription(){
    // TODO Auto-generated method stub
    return "数值型数据(*.data)";
    }
    });
    jFileButton.addMouseListener(new MyMouseListener());
    this.setVisible(true);
    } class MyMouseListener extends MouseAdapter{
    public void mouseClicked(java.awt.event.MouseEvent e){
    int result = jfChooser.showOpenDialog(jFrame);
    if(result == JFileChooser.APPROVE_OPTION){ // 确认打开
    File fileIn = jfChooser.getSelectedFile();
    if(fileIn.exists()){
    JOptionPane.showMessageDialog(jFrame,"OPEN"); // 提示框
    }else{
    }
    }else
    if(result == JFileChooser.CANCEL_OPTION){
    System.out.println("Cancel button is pushed.");
    }else
    if(result == JFileChooser.ERROR_OPTION){
    System.err.println("Error when select file.");
    }
    }
    } public static void main(String[] args){
    new JFileChooserDemo();
    }
    }
      

  3.   

    我换了个控件实现目录选择,可是display消毁时报java.lang.NullPointerException错,代码如下:  Display display = new Display ();
           Shell shell = new Shell (display);       DirectoryDialog dialog = new DirectoryDialog (shell);
           selectPath = dialog.open() ;
           System.out.println ( selectPath );
           
           while (!shell.isDisposed()) {
            
           if (!display.readAndDispatch ()) display.sleep ();      
            
           }
           display.dispose ();
      

  4.   

    如果display不消毁的话窗口仍然不能正常关闭,关闭时变成最小化,哪位高手指点一下啊,急急!!