当前位置: 首页 > 知识库问答 >
问题:

JFileChooser未正确关闭

杨宏儒
2023-03-14

我正在处理一个需要使用jFileChooser Swing窗口的项目。当单击“取消”或“打开”时,窗口不会关闭。在查看了StackOverflow上的许多文章以及JFileChooser教程和文档之后,我不知道是什么导致了这个重复的问题。

我正在与NetBeans一起使用它的swing编辑器。我也在Eclipse中尝试了该程序作为测试,并收到了相同的结果。

//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                   
}

共有1个答案

司空叶五
2023-03-14

这里的主要问题是文件选择器被添加到框架中!我的“一天的学习项目”是,这甚至是可能的。

import java.io.*;
import java.io.IOException;
import javax.swing.*;

public class FileSelector extends JFrame {

    private static String selPath;
    private int val;

    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");
    }

    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);

        pack();
    }

    public static void main(String args[]) {
        /* 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());}
            }
        });
    }

    private javax.swing.JFileChooser folderSelector;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenuItem jMenuItem1;
}
 类似资料:
  • 有人能帮我吗?

  • 我正在尝试使用Springboot反应式webclient进行HTTP调用。远程服务器错误导致连接关闭。 请查找以下使用Webclient进行rest调用的代码。 Webclient创建的代码: 第一次通话后,我收到以下日志: 当我在一段时间后(比如10分钟)拨打电话时,连接将变为非活动状态。我正在获取以下日志: 我发现连接没有正确返回到池。配置中是否缺少任何内容?我是否已正确关闭连接?我想这应该

  • 问题内容: 我想在Java中实现SSL代理。我基本上打开了两个套接字,并运行了两个线程,这些线程将写入他们从中读取的内容,反之亦然。每个线程如下所示: 每个线程只会关闭输入套接字,因此最终两个套接字都会关闭。 但是,如果我想使用an 怎么办?似乎那里不支持这些方法。这是我得到的例外。 我想出的是: 每当套接字结束时,我都必须捕获并忽略套接字末尾异常。 我的问题是: 如果不受支持,我怎么会从那里得到

  • 我最近将microservices后端迁移到Spring boot v2.6.1 Spring cloud v2021.0.0(旧版本是v2.2.1.RELEASE)。 以前,设置包括发现服务器(Eureka)、网关(Zuul)和各种可通过网关从外部访问的微服务。所有这些都启用了TLS/安全端口,因此所有请求(发现、注册、网关转发等)都需要SSL信任存储配置。 由于Zuul在这个版本中不再受支持,

  • 问题内容: 我正在使用Play编写一个部署在Tomcat中的webapp。因为该应用程序不会处理大量数据,所以我在Hibernate中使用默认的H2数据库。当我想部署新版本的应用程序时,我关闭了tomcat,擦除了旧的webapp和WAR,添加了新的WAR,然后开始备份。 直到几天前,当我添加数据库组件时,它一直有效。现在,我经常无法重新部署该应用程序。当我删除旧目录时,它将使用以下结构自动重新生