为Swing小应用程序/应用程序创建自定义模式对话框 [英] Creating a custom modal dialog for a Swing applet / application

查看:106
本文介绍了为Swing小应用程序/应用程序创建自定义模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Swing应用程序,该应用程序需要充当浏览器中的applet或独立应用程序,即它可以包含在JFrame或JApplet中.

I'm writing a Swing application that needs to function either as an applet in a browser or as a standalone application, i.e. it may be contained in either a JFrame or a JApplet.

在这种情况下,我想向用户显示一个自定义模式对话框(即,一个具有自定义布局和逻辑的复杂对话框,而不仅仅是简单的JOptionPane提示之一).如果对话框是完全包含在应用程序窗口中的轻量级组件,那就很好.

In this context, I'd like to display a custom modal dialog box to the user (i.e. a complex dialog with a custom layout and logic, not just one of the simple JOptionPane prompts). It is fine if the dialog is a lightweight component fully contained within the application window.

同时,应用程序中将发生后台处理(网络线程,动画等).在显示对话框时,这需要继续.

At the same time, there will be background processing happening in the application (network threads, animations etc.). This needs to continue while the dialog is displayed.

实现此目标的最佳方法是什么?

What would be the best approach to implement this?

推荐答案

看看JDialog.如果将其设置为模式,它将运行自己的事件处理以使GUI保持最新状态,同时捕获鼠标和键盘事件以供自己使用.

Take a look at JDialog. If you set it modal it will run its own event processing to keep the GUI up to date, while capturing mouse and keyboard events for its own use.

我已经看过了它使用的代码,而这实际上并不是您想要尝试重新发明的东西.

I've looked at the code it uses and it's really not something you want to try to reinvent.

如果以非模式运行它,则可能需要添加一个在最终关闭时要调用的侦听器.这是通过addWindowListener和覆盖windowClosing的WindowAdapter完成的.

If you run it non modal, you 'll probably need to add a listener to be called when it finally closes. That is done with addWindowListener and a WindowAdapter that overrides windowClosing.

对于构造函数的owner参数,我使用

As for the owner parameter for the constructor, I use

    Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class, comp);

其中comp是一些可见的组件.

where comp is some visible component.

之所以起作用,是因为始终有一个顶级窗口,无论是作为applet还是作为应用程序运行.

It works because there is always a top level Window, whether running as an applet or application.

这篇关于为Swing小应用程序/应用程序创建自定义模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆