传递多个文件时,摆动窗口被卡住

我正在创建一个代码审查应用程序,该程序将使用.resource格式的文件并共享结果。它适用于.resource格式文件中的3-4个文件夹,但是当文件夹大小增加时,窗口将卡住。

public class CodeReviewWindow extends JDialog {

    private final JButton jbtOk = new JButton("Choose File");
    private final JButton jbtCancel = new JButton("Cancel");

    private final JLabel jlblStatus = new JLabel(" ");

    public CodeReviewWindow() {
        this(null,true);
    }

    public CodeReviewWindow(final JFrame parent,boolean modal) {
        super(parent,modal);
        JPanel p2 = new JPanel();
        p2.add(jbtOk);
        p2.add(jbtCancel);

        JPanel p5 = new JPanel(new BorderLayout());
        p5.add(p2,BorderLayout.CENTER);
        p5.add(jlblStatus,BorderLayout.NORTH);
        jlblStatus.setforeground(Color.RED);
        jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);

        setLayout(new BorderLayout());
        add(p5,BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                system.exit(0);
            }
        });


        jbtOk.addactionListener(new actionListener() {
            @Override
            public void actionPerformed(actionEvent e) {
                JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

                int returnValue = jfc.showOpenDialog(null);
                Map<String,List<RuleViolation>> mapOfRuleViolation = new HashMap<>();
                BufferedInputStream bis = null;
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = jfc.getSelectedFile();
                    if (selectedFile.getName().endsWith(".resource")) {
                        try {
                            ZipFile zipFile = new ZipFile(selectedFile.getabsolutePath());

                            Enumeration<? extends ZipEntry> entries = zipFile.entries();
                            while (entries.hasMoreElements()) {
                                ZipEntry entry = entries.nextElement();
                                File file = new File(entry.getName());
                                String name = file.getName();
                                if (entry.isDirectory()) {
                                    continue;
                                } else {
                                    if (name.endsWith(".js")) {
                                        bis = new BufferedInputStream(zipFile.getInputStream(entry));
                                        ClassLoader classLoader = Thread.currentThread().getcontextClassLoader();
                                        InputStream ruleSetsInputStream = classLoader.getResourceAsStream("xml/javascript.xml");
                                        File ruleSet = PmdReviewService.stream2file(ruleSetsInputStream,"javascript",".xml");
                                        PMDConfiguration pmdConfiguration = new PMDConfiguration();
                                        pmdConfiguration.setReportFormat("text");
                                        pmdConfiguration.setRuleSets(ruleSet.getPath());
                                        pmdConfiguration.setThreads(4);
                                        SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(pmdConfiguration);
                                        Rulesetfactory rulesetfactory = RulesetsFactoryUtils.getRulesetfactory(pmdConfiguration,new ResourceLoader());
                                        RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(pmdConfiguration.getRuleSets(),rulesetfactory);

                                        PmdReviewService pmdReviewService = new PmdReviewService(sourceCodeProcessor,ruleSets);
                                        List<RuleViolation> review = pmdReviewService.review(bis,file);
                                        mapOfRuleViolation.put(file.getName(),review);

                                        Document document = new Document();
                                        try {
                                            PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(name.replace(".js","") + ".pdf"));
                                            document.open();
                                            document.add(new Paragraph("Code Review For " + name + " on " + new Date()));

                                            //ZapfDingbatsList List Example
                                            ZapfDingbatsList zapfDingbatsList = new ZapfDingbatsList(43,30);
                                            for (RuleViolation ruleViolation : review) {
                                                zapfDingbatsList.add(new ListItem("Begin Line : " + ruleViolation.getBeginColumn() +
                                                        "\nIssue : " + ruleViolation.getDescription()));
                                            }

                                            document.add(zapfDingbatsList);
                                            document.close();
                                            writer.close();
                                            bis.close();
                                        } catch (DocumentException e1) {
                                            e1.printStackTrace();
                                        } catch (FileNotFoundException e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                }
                            }

                            zipFile.close();
                        } catch (IOException ex) {
                            System.err.println(ex);
                        } finally {
                            setVisible(false);
                            parent.dispose();
                            system.exit(0);
                        }
                    }

                }
            }
        });
        jbtCancel.addactionListener(new actionListener() {
            @Override
            public void actionPerformed(actionEvent e) {
                setVisible(false);
                parent.dispose();
                system.exit(0);
            }
        });
    }
}


public class StaticCodeAnalysis extends JFrame {


    private CodeReviewWindow passDialog;

    public StaticCodeAnalysis() {
        passDialog = new CodeReviewWindow(this,true);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                passDialog.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        StaticCodeAnalysis staticCodeAnalysis = new StaticCodeAnalysis();

    }

}

使用SwingWorker进行了尝试,现在该应用程序不会卡住,但是如果文件夹中有7个以上的文件可用,则它永远不会返回任何内容。

SwingWorker swingWorker = new SwingWorker() {

    @Override
    protected Object doInBackground() throws Exception {
        Random random = new Random();
        int progress = 0;
        setProgress(0);
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File file = new File(entry.getName());
            String name = file.getName();
            if (entry.isDirectory()) {
                continue;
            } else {
                // Same logic
                    BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                    ClassLoader classLoader = Thread.currentThread().getcontextClassLoader();
                    InputStream ruleSetsInputStream = classLoader.getResourceAsStream("xml/javascript.xml");
                    File ruleSet = PmdReviewService.stream2file(ruleSetsInputStream,".xml");
                    PMDConfiguration pmdConfiguration = new PMDConfiguration();
                    pmdConfiguration.setReportFormat("text");
                    pmdConfiguration.setRuleSets(ruleSet.getPath());
                    pmdConfiguration.setThreads(4);
                    SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(pmdConfiguration);
                    Rulesetfactory rulesetfactory = RulesetsFactoryUtils.getRulesetfactory(pmdConfiguration,new ResourceLoader());
                    RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(pmdConfiguration.getRuleSets(),rulesetfactory);

                    PmdReviewService pmdReviewService = new PmdReviewService(sourceCodeProcessor,ruleSets);
                    // Make random progress.
                    progress += random.nextInt(10);
                    setProgress(Math.min(progress,100));
                    List<RuleViolation> review = pmdReviewService.review(bis,file);
                    mapOfRuleViolation.put(file.getName(),review);

                    Document document = new Document();
                    try {
                        PdfWriter writer = PdfWriter.getInstance(document,"") + ".pdf"));
                        document.open();
                        document.add(new Paragraph("Code Review For " + name + " on " + new Date()));

                        //ZapfDingbatsList List Example
                        ZapfDingbatsList zapfDingbatsList = new ZapfDingbatsList(43,30);
                        for (RuleViolation ruleViolation : review) {
                            zapfDingbatsList.add(new ListItem("Begin Line : " + ruleViolation.getBeginColumn() +
                                    "\nIssue : " + ruleViolation.getDescription()));
                        }

                        document.add(zapfDingbatsList);
                        document.close();
                        writer.close();
                        bis.close();
                    } catch (DocumentException e1) {
                        e1.printStackTrace();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        return null;
    }

    @Override
    protected void done() {
        try {
            zipFile.close();
            setProgress(100);
            setVisible(false);
            parent.dispose();
            system.exit(0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        super.done();
    }
};

swingWorker.execute();
dasiy123zs 回答:传递多个文件时,摆动窗口被卡住

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2694222.html

大家都在问