我只是试图在单击“接受”按钮时将loadingbar.gif加载到框架中。并且在ActionListener到达结束之前它不会添加到框架,这会破坏显示加载条的整个目的。我尝试添加.repaint(),但这不起作用。
acceptButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
lbl.setVisible(true);
lbl.repaint();
try
{............
如果我正在讨论代码,那就是这个问题。一旦ActionListener完成后面的所有操作,它就会执行它。
public class PrintSheet extends JDialog {
private JPanel contentPane;
private MainFrame mainGui;
private DbWorker db;
private Connection con;
private JLabel lbl;
public PrintSheet(final MainFrame mainGui) {
setResizable(false);
this.mainGui = mainGui;
setAlwaysOnTop(true);
setBounds(100, 100, 180, 125);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
URL url = getClass().getResource("/images/loading_bar_small.gif");
Icon img = new ImageIcon(url);
lbl = new JLabel(img);
lbl.setBounds(0, 70, 164, 17);
contentPane.setLayout(null);
lbl.setVisible(false);
getContentPane().add(lbl);
JLabel lblPrintSignSheet = new JLabel("Print Sign-In Sheet: ");
lblPrintSignSheet.setBounds(10, 11, 128, 14);
contentPane.add(lblPrintSignSheet);
JButton acceptButton = new JButton("Accept");
acceptButton.setBounds(10, 36, 65, 23);
acceptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lbl.setVisible(true);
lbl.repaint();
try
{
db = new DbWorker(DatabaseStore.getAddress(), DatabaseStore.getPort(),
DatabaseStore.getUserName(), DatabaseStore.getPassword());
con = db.getConnection();
}
catch (Exception e1)
{
e1.printStackTrace();
new DbConnectErrorDialog().setVisible(true);
}
String OUT_PUT = "./sign_in_sheet.xlsx";
String REPORT = "/certificates/Signing_List.jrxml";
HashMap<String, Object> map = new HashMap<>();
//map.put("FirstName",firstName);
try
{
JasperDesign jd = JRXmlLoader.load(getClass().getResourceAsStream(REPORT));
JasperReport jr = JasperCompileManager.compileReport(jd);
JasperPrint jp = JasperFillManager.fillReport(jr, map, con);
JRXlsxExporter export = new JRXlsxExporter();
export.setExporterInput(new SimpleExporterInput(jp));
export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT)));
SimpleXlsxReportConfiguration config = new SimpleXlsxReportConfiguration();
export.setConfiguration(config);
export.exportReport();
JOptionPane.showMessageDialog(mainGui, "<html>The sign-in sheet was successfully created</html>");
lbl.setVisible(false);
setVisible(false);
lbl.repaint();
//Opens file after completed export
OpenFile(OUT_PUT);
} catch (JRException ex) {
ex.printStackTrace();
lbl.setVisible(false);
lbl.repaint();
JOptionPane.showMessageDialog(mainGui, "<html>The sign-in sheet could not be created due to an error:<br>" + ex + "</html>", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
contentPane.add(acceptButton);
JButton button = new JButton("Cancel");
button.setBounds(85, 36, 79, 23);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
contentPane.add(button);
}
public void OpenFile(String path)
{
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File(path);
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
}
}