Java 当传递多个文件时,Swing窗口被卡住
我正在创建一个代码审查应用程序,它接受Java 当传递多个文件时,Swing窗口被卡住,java,swing,swingworker,Java,Swing,Swingworker,我正在创建一个代码审查应用程序,它接受.resource格式化文件并共享结果。它适用于.resource格式文件中的3-4个文件夹,但当文件夹大小增加时,窗口会卡住 public class CodeReviewWindow extends JDialog { private final JButton jbtOk = new JButton("Choose File"); private final JButton jbtCancel = new JButton("Cance
.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();
}
}
公共类CodeReviewWindows扩展JDialog{
私有最终JButton jbtOk=新JButton(“选择文件”);
私有最终JButton jbtCancel=新JButton(“取消”);
专用最终JLabel jlblStatus=新JLabel(“”);
公共代码审阅窗口(){
这个(空,真);
}
public CodeReviewWindow(最终JFrame父级,布尔模式){
超级(父级、模态);
JPanel p2=新的JPanel();
p2.添加(jbtOk);
p2.添加(jbtCancel);
JPanel p5=新的JPanel(新的BorderLayout());
p5.添加(p2,BorderLayout.CENTER);
p5.添加(jlblStatus,BorderLayout.NORTH);
jlbstatus.set前台(颜色为红色);
jlbstatus.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(新的BorderLayout());
添加(p5,BorderLayout.CENTER);
包装();
setLocationRelativeTo(空);
setDefaultCloseOperation(在关闭时处理);
addWindowListener(新的WindowAdapter(){
@凌驾
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
jbtOk.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
JFileChooser jfc=新的JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue=jfc.showOpenDialog(null);
Map mapOfRuleViolation=新建HashMap();
BufferedInputStream bis=null;
if(returnValue==JFileChooser.APPROVE\u选项){
File selectedFile=jfc.getSelectedFile();
if(selectedFile.getName().endsWith(“.resource”)){
试一试{
ZipFile ZipFile=新ZipFile(selectedFile.getAbsolutePath());
枚举似乎类似错误发生在我无法复制的某些代码中。我看到您的应用程序正在多个线程中处理这些文件(从pmdConfiguration.setThreads(4)行判断)。您是否检查了这些线程中是否存在任何异常(如OutOfMemory)?还要注意,您不应该在操作侦听器本身中进行任何主要处理,因为这会阻塞整个UI。只需调用new Thread(){your processing}.start()也可以,我个人从不使用SwingWorker。没有例外,如果我打开窗口让程序运行,它将在30-40分钟内完成。那么可能这就是它需要多长时间?如果总是在单独的线程中进行处理,那么Swing应该不会有任何问题。您可以使用JProgressBar在进度。如果您准确地说明了您的期望与现在发生的事情,以及一个我也可以运行的解决该问题的代码,可能会有所帮助。您是否尝试过使用publish()方法,然后在工作线程中重写process()方法,而不是使用setprogress来显示信息?也许您可以在那里打印或显示您需要的内容
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, "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);
// 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, 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();
}
}
}
}
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();