我正在开发一个需要使用 jFileChooser Swing 窗口的项目。单击“取消”或“打开”时,窗口不会关闭。在查看 StackOverflow 上的许多帖子并查看 JFileChooser教程和文档后,我不知道是什么导致了这个重复问题。
我正在使用 NetBeans 使用它的 swing 编辑器。我还尝试了 Eclipse 中的程序作为测试,并收到了相同的结果。
新更新:
这是我对代码的 SSCCE 版本的尝试,感谢您的耐心等待:
//package imi_test;
import java.io.*;
import java.io.IOException;
import javax.swing.*;
public class FileSelector extends JFrame {
//private static BufferedWriter out;
//private static FileInputStream in;
private static String selPath;
private int val;
//private static UniqueReader2 ur;
public FileSelector() throws IOException {
initComponents();
}
public static String getSelPath(){
return selPath;
}
public static void writeNewPath(String path) throws IOException{
//This would write the selected folder's new unique file path to a file
System.out.println("New file path written");
/*
out = new BufferedWriter(new FileWriter("recentPaths.txt",true));
in = new FileInputStream("recentPaths.txt");
ur = new UniqueReader(new InputStreamReader(in));
ur.main(null);
try{
if(ur.linePres){
}else{
out.write(path);
out.newLine();
out.flush();
out.close();
FileChecker.setFilePath(path);
}
}catch(Exception e){System.err.println(e);}
*/
}
//*******************************************************
//Netbeans auto gen GUI code starts here
@SuppressWarnings("unchecked")
private void initComponents() {
jDialog1 = new javax.swing.JDialog();
jMenuItem1 = new javax.swing.JMenuItem();
jFrame1 = new javax.swing.JFrame();
folderSelector = new javax.swing.JFileChooser();
javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jMenuItem1.setText("jMenuItem1");
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
folderSelector.setCurrentDirectory(new java.io.File("Computer"));
folderSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
folderSelector.setAcceptAllFileFilterUsed(false);
folderSelector.setDialogTitle("Please Select a Folder");
val = folderSelector.showOpenDialog(FileSelector.this);
folderSelector.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
folderSelectorActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}
//Netbeans auto gen GUI code ends here
//*******************************************************
private void folderSelectorActionPerformed(java.awt.event.ActionEvent evt) {
if (val== JFileChooser.APPROVE_OPTION) {
try{
selPath = folderSelector.getSelectedFile().getPath();
writeNewPath(selPath);
this.dispose();
}catch(Exception ex){System.err.println(ex.getMessage() + "After folder selection");}
}
else{
selPath = null;
this.dispose();
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(FileSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
new FileSelector().setVisible(true);
}catch(Exception e){System.err.println(e.getMessage());}
}
});
}
// Variables declaration - do not modify
private javax.swing.JFileChooser folderSelector;
private javax.swing.JDialog jDialog1;
private javax.swing.JFrame jFrame1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
}
感谢您的时间和帮助解决这个问题。
解决方案: 在清除netbeans 放入的文件中的额外内容后,jDialog、JFrame 和jMenuItem,它按预期的方式工作。