Well, you can only call the set close operation method on a JFrame. The close operation happens when the close button is clicked; the only class which has a suitable close button is a JFrame.
You can compile your code without a
top level container, but you can only make it visible if you have a top level container.