swing:JPanel 与模式/非模式对话框?
我想在库中实现一些功能,并将其用作我的应用程序的 GUI 构建块。我想我想将它实现为扩展 JPanel 的东西,这样它就可以作为组件嵌入到其他窗口中。
我是否有理由使用 JDialog
来代替?创建一个显示 JPanel
以及最少其他适当组件的 JDialog
有多容易? (例如,对于无模式对话框,只有边框/关闭框/等;对于模态对话框,相同+确定/取消)
I want to implement some functionality in a library and make it available as a GUI building block for my applications. I think I would like to implement it as something that extends a JPanel
, so it can be embedded as a component in other windows.
Is there a reason I should use a JDialog
instead? How easy is it to create a JDialog
which displays a JPanel
along with minimal other appropriate components? (e.g. just a border/closebox/etc for modeless dialog; for modal, the same + an OK/Cancel)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅如果您想要一个对话框,则应该扩展 JDialog,如果您只想要一个可以在其他 Windows 或框架中使用的面板,则应该扩展 JPanel。
是的,创建一个 JDialog 很容易,它只包含一个带有边框、关闭框和确定/取消(模式和非模式)的 JPanel。
查看如何创建对话框和< a href="http://java.sun.com/docs/books/tutorial/uiswing/components/panel.html" rel="nofollow noreferrer">如何使用面板
You should extend JDialog only if you want a Dialog, and if you want just a Panel that you can use in other Windows or Frames you should extend JPanel.
Yes, it is easy to create an JDialog just containing a JPanel with a border, closebox and OK/Cancel, both modal and not modal.
Have a look at How to Make Dialogs and How to Use Panels
我会将其设为
JPanel
。这样,您可以在其他组件中重用它,或者如果您想将其作为独立运行,则可以将其放入JFrame
中(通过调用setContentPane
)。您需要JDialog
的唯一原因是如果您想让组件成为模态组件。I would make it a
JPanel
. That way you could reuse it in other components or drop it into aJFrame
(by callingsetContentPane
) if you want to run it as a standalone. The only reason for you to need aJDialog
is if you want to make your component modal.