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