在 JFrame 上显示 JDialog(“请稍候”)

发布于 2024-12-25 15:49:37 字数 270 浏览 3 评论 0原文

我有一个 JFrame,这个 JFrame 有一个 JButton

我想在 JButton 中首先显示 JDialog(显示“请稍候”)并执行其他代码然后关闭JDialog

但是当显示JDialog停止时执行其他代码JButton

I have a JFrame and this JFrame has a JButton.

I want in JButton first show JDialog (display "Please wait") and execute other code then closing JDialog.

But when showing JDialog stopped to execute other code on JButton.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

渡你暖光 2025-01-01 15:49:37

Thread 上启动其他处理(例如在 SwingWorker) 并在其开头调用 modalDialog.setVisible(true)。在任务结束时调用 setVisible(false)

Start the other processing on a Thread (e.g. in a SwingWorker) and at the start of it, call modalDialog.setVisible(true). At the end of the task call setVisible(false).

居里长安 2025-01-01 15:49:37

我建议创建一个简单的 JDialog,然后在代码运行后将其处置。您可以使用以下代码创建 JDialog:

JDialog dialog = new JDialog();
JLabel label = new JLabel("Please wait...");
dialog.setLocationRelativeTo(null);
dialog.setTitle("Please Wait...");
dialog.add(label);
dialog.pack();

并像这样实现它:

dialog.setVisible(true); // show the dialog on the screen

        // Do something here

dialog.setVisible(false); // set visibility to false when the code has run

I would suggest creating a simple JDialog and then disposing it after your code has run. You can create your JDialog with the following code:

JDialog dialog = new JDialog();
JLabel label = new JLabel("Please wait...");
dialog.setLocationRelativeTo(null);
dialog.setTitle("Please Wait...");
dialog.add(label);
dialog.pack();

And implement it like this:

dialog.setVisible(true); // show the dialog on the screen

        // Do something here

dialog.setVisible(false); // set visibility to false when the code has run
原野 2025-01-01 15:49:37

也许 jdialog 处于模态模式,请尝试更改 jdialog 的模态属性:yordialog.setModal(false)

Maybe the jdialog is in modal mode, try change the modal property of the jdialog : yordialog.setModal(false).

你好,陌生人 2025-01-01 15:49:37

它停止了,因为您正在使用“主线程”,它用于执行代码并显示 JDialog。

要解决这个问题,您应该研究类似 SwingWorker 的内容

It stopped because you are using the "MAIN-Thread" which is used to execute the code and show the JDialog.

To solve this you should look into something like SwingWorker

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文