gpt4 book ai didi

java - 将事件处理添加到 Jframe 对话框

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:59 25 4
gpt4 key购买 nike

所以我有以下代码来制作一个快速弹出框,通知我的用户程序已完成运行。我想在用户单击“确定”后处理一些额外的事件,特别是我想关闭一些驱动程序和 sql 连接。我怎样才能做到这一点?

这是制作 jframe 的代码

javax.swing.JFrame optionFrame = new javax.swing.JFrame();
JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");
optionFrame.toFront();
optionFrame.repaint();
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

最佳答案

只需在显示对话框后立即执行此操作,但在 this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

同时删除该部分:

 optionFrame.toFront();
optionFrame.repaint();

这一行(消息对话框弹出窗口):

 JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");

将保持调用它的线程,直到用户单击“确定”或关闭弹出窗口,无需执行任何额外操作,此行之后的任何代码都将在弹出窗口消失后执行。

根据您的示例,它可能是:

 ...
JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");
// do your clean-up work here (probably invoke some clean up service method)
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

关于java - 将事件处理添加到 Jframe 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com