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 c开发者_C百科reate 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)
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
I would make it a JPanel
. That way you could reuse it in other components or drop it into a JFrame
(by calling setContentPane
) if you want to run it as a standalone. The only reason for you to need a JDialog
is if you want to make your component modal.
精彩评论