嗨,我的进度栏有问题。我有一个Start类,它是一个JFrame类,一个Excel类是一个普通的java类。 Excel类对其他类进行一些调用。现在代码本身可以正常工作,现在我尝试添加进度条。但是,当我尝试更新进度条时,它会更具有Start功能:
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()), this);
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);
}
}
我使用此功能之外的功能更新进度条:
public void updateProgressBar(int value) {
progressBar.setValue(value);
}
这是我的Excel函数:
public class Excel {
Start start;
File outputFolder;
File logoFolder;
public void readExcel(File excelFile, File output, File logoLocation, Start start) throws IOException, InvalidFormatException, ParseException {
this.outputFolder = output;
this.logoFolder = logoLocation;
// 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;
int totalRows = sheet.getPhysicalNumberOfRows();
for (Row row : sheet) {
currentRow++;
updateProgressBarFromCurrentRow(currentRow, totalRows);
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);
}
}
}
}
}
public void updateProgressBarFromCurrentRow(int current, int totalRows) {
start.updateProgressBar(current/totalRows*100);
}
}
更新进度条后,它会立即回到“开始”功能并执行最后几行代码。我做错什么了吗?我没有看到错误。