我的进度栏有问题。 它不会更新,实际上我的整个jframe在忙于执行代码时都会冻结。我设法为进度栏设置了正确的值。
我的代码创建传单,这些传单可以独立工作,并且可以完美地完成工作。只是我的JFrame本身在执行此操作时冻结,这也导致我无法使用任何按钮。 为什么我的JFrame在执行代码时冻结?
开始jframe类:
if (excelFileLocation.getText().equals("")) {
JOptionPane.showMessageDialog(null, "U heeft geen excel bestand gekozen, kies eerst een excel bestand.", "Geen excel bestand gekozen.", JOptionPane.ERROR_MESSAGE);
} else if (folderLocation.getText().equals("")) {
JOptionPane.showMessageDialog(null, "U heeft geen eindmap gekozen voor de gemaakte flyers, kies eerst een eindmap.", "Geen map gekozen.", JOptionPane.ERROR_MESSAGE);
} else if (logoFolderLocation.getText().equals("")) {
JOptionPane.showMessageDialog(null, "U heeft map gekozen met de schoollogo's, kies eerst een map.", "Geen map gekozen.", JOptionPane.ERROR_MESSAGE);
} else {
try {
makeFlyers.setBackground(Color.red);
makeFlyers.setEnabled(false);
JOptionPane.showMessageDialog(null, "", "Flyers worden gemaakt!", JOptionPane.INFORMATION_MESSAGE);
Excel excel = new Excel();
excel.readExcel(new File(excelFileLocation.getText()), new File(this.folderLocation.getText()), new File(logoFolderLocation.getText()), progressBar);
System.out.println("Klaar met flyers maken!");
JOptionPane.showMessageDialog(null, "Alle flyers zijn succesvol gemaakt!", "Succes!", JOptionPane.PLAIN_MESSAGE);
resetButton(makeFlyers);
} catch (IIOException ex) {
JOptionPane.showMessageDialog(null, "Het logo kan niet worden geplaatst, kijk of er correct word verwezen naar de logo.", "Fout bij plaatsen van logo", JOptionPane.ERROR_MESSAGE);
handleException(ex);
resetButton(makeFlyers);
} catch (InvalidFormatException | ParseException ex) {
handleException(ex);
resetButton(makeFlyers);
} catch (IndexOutOfBoundsException | NullPointerException ex) {
System.out.println("Klaar met flyers maken!");
JOptionPane.showMessageDialog(null, "Alle flyers zijn succesvol gemaakt!", "Succes!", JOptionPane.PLAIN_MESSAGE);
resetButton(makeFlyers);
} catch (IOException ex) {
handleException(ex);
JOptionPane.showMessageDialog(null, "Het excel bestand wat u wilt gebruiken kan niet worden gevonden, of staat nog open in een ander venster.", "Fout bij uitlezen van Excel bestand", JOptionPane.ERROR_MESSAGE);
resetButton(makeFlyers);
}
}
这是我的Excel函数:
public class Excel {
JProgressBar progressBar;
File outputFolder;
File logoFolder;
public void readExcel(File excelFile, File output, File logoLocation, JProgressBar pb) throws IOException, InvalidFormatException, ParseException {
this.outputFolder = output;
this.logoFolder = logoLocation;
progressBar = pb;
// Retrieving the number of sheets in the Workbook
try ( // Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(excelFile)) {
for (Sheet sheet : workbook) {
if (sheet.getSheetName().equals("secundair flyer")) {
createFlyersFromExcel(sheet);
}
}
}
}
public void handleException(Exception ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
System.out.println(sw.toString());
}
public void createFlyersFromExcel(Sheet sheet) throws IOException, ParseException {
DataFormatter dataFormatter = new DataFormatter();
boolean firstRow = false;
int currentRow = 0;
progressBar.setMaximum(sheet.getPhysicalNumberOfRows());
System.out.println(progressBar.getMaximum());
for (Row row : sheet) {
currentRow++;
if (firstRow == false) {
firstRow = true;
} else {
if (!dataFormatter.formatCellValue(row.getCell(0)).equals("0")) {
List<String> values = new ArrayList<>();
for (Cell cell : row) {
cell.setCellFormula(null);
String cellValue = dataFormatter.formatCellValue(cell);
if (!cellValue.equals("0")) {
values.add(cellValue);
} else {
values.add(null);
}
}
if (values.get(1) == null) {
} else {
School s = new School(values, logoFolder);
Flyer flyer = new Flyer(s, outputFolder);
}
}
}
progressBar.setValue(currentRow);
}
}
}