Java setVisible(false)在ActionListener结束之前不会工作
我只是想在点击Accept按钮时将loadingbar.gif加载到框架中。在ActionListener结束之前,它不会添加到帧中,这违背了显示加载条的全部目的。我尝试添加.repaint(),但这不起作用 acceptButton.addActionListener(新ActionListener(){ 这就是我所说的代码,它只是在ActionListener完成所有操作后才执行Java setVisible(false)在ActionListener结束之前不会工作,java,swing,button,awt,actionlistener,Java,Swing,Button,Awt,Actionlistener,我只是想在点击Accept按钮时将loadingbar.gif加载到框架中。在ActionListener结束之前,它不会添加到帧中,这违背了显示加载条的全部目的。我尝试添加.repaint(),但这不起作用 acceptButton.addActionListener(新ActionListener(){ 这就是我所说的代码,它只是在ActionListener完成所有操作后才执行 public class PrintSheet extends JDialog { private JPane
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
}
}
}
}
公共类打印页扩展JDialog{
私有JPanel内容窗格;
专用主机mainGui;
私人DBDB;
私人连接;
私人JLabel-lbl;
公共打印页(最终主机主GUI){
可设置大小(假);
this.mainGui=mainGui;
setAlwaysOnTop(真);
立根(100100180125);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
URL=getClass().getResource(“/images/loading_bar_small.gif”);
Icon img=新图像图标(url);
lbl=新的JLabel(img);
lbl.立根(0,70,164,17);
contentPane.setLayout(null);
lbl.setVisible(假);
getContentPane().add(lbl);
JLabel lblPrintSignSheet=新JLabel(“打印登录表:”);
lblPrintSignSheet.立根(10,11,128,14);
contentPane.add(lblPrintSignSheet);
JButton acceptButton=新JButton(“接受”);
acceptButton.setBounds(10,36,65,23);
acceptButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
lbl.setVisible(真);
lbl.repaint();
尝试
{
db=新的DbWorker(DatabaseStore.getAddress(),DatabaseStore.getPort(),
DatabaseStore.getUserName(),DatabaseStore.getPassword());
con=db.getConnection();
}
捕获(异常e1)
{
e1.printStackTrace();
新建DbConnectErrorDialog().setVisible(true);
}
在工作表.xlsx中填写“//签名”;
String REPORT=“/certificates/Signing_List.jrxml”;
HashMap=newHashMap();
//地图放置(“名字”,名字);
尝试
{
JasperDesign jd=JRXmlLoader.load(getClass().getResourceAsStream(REPORT));
JasperReport jr=jaspecompilemanager.compileReport(jd);
JasperPrint jp=JasperFillManager.fillReport(jr、map、con);
JRXlsxExporter export=新的JRXlsxExporter();
setExporterInput(新的SimpleExporterInput(jp));
setExporterOutput(新的SimpleOutputStreamExporterOutput(新文件(输出));
SimpleXlsxReportConfiguration=new SimpleXlsxReportConfiguration();
export.setConfiguration(config);
export.exportReport();
showMessageDialog(mainGui,“登录表已成功创建”);
lbl.setVisible(假);
setVisible(假);
lbl.repaint();
//完成导出后打开文件
OpenFile(输出);
}捕获(JREX异常){
例如printStackTrace();
lbl.setVisible(假);
lbl.repaint();
JOptionPane.showMessageDialog(mainGui,“由于错误,
”+ex+,“错误”,JOptionPane.error\u消息,无法创建登录表);
}
}
});
contentPane.add(acceptButton);
JButton按钮=新JButton(“取消”);
按钮.立根(85,36,79,23);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
setVisible(假);
}
});
contentPane.add(按钮);
}
公共void OpenFile(字符串路径)
{
if(Desktop.isDesktopSupported()){
试一试{
File myFile=新文件(路径);
Desktop.getDesktop().open(myFile);
}捕获(IOEX异常){
//没有注册PDF的应用程序
}
}
}
}
我建议使用revalidate
,但这与布局管理API有关,您似乎已经决定不需要它,并且可以更好地使用它,所以您出现问题也就不足为奇了我使用的是FlowLayout,但它不是那样工作的,所以我将它改为Absolute,但仍然没有做任何事情您可以使用更多的flexible layout manager,如GridBagLayout
,则您只需在包含标签的容器上调用revalidate
和repaint
,您不应尝试在ActionListener中执行任何需要几毫秒以上的操作,因为您将阻塞事件线程-不会发生重新绘制直到actionListener返回。@webminer07查看并
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
}
}
}
}