In the GUI book we use in class there are many examples of how graphical user interfaces are made in Java. So many examples, that I’m very confused regarding which one should be used when it comes down to a big application.
So I’ve seen examples
- in which the main class extends
JFrame - where the
JFrameobject is created inside themainmethod - where the main class extends
JFrameAND implementsActionEventinterface - where Listener classes are declared inside the main class
Sure, I can work with all of these, but right now, as I don’t have any kind of experience, I don’t see the benefit of using any of them. Is actually one of them the correct way to do it or it depends on my sittuation?
Thank you!
"Is A" or "Has A"? This is the question that should be asked when considering extending a class. If the new class "Is A" frame, extend frame, but if the class just needs a reference to a frame, don’t extend.
In fact, if a custom component is required, extend a
JComponentorJPanel, then add that to a frame, ..applet, window,JInternalFrame, dialog, constraint of a layout, part of a split pane..Listeners
As to the listeners. Rather than traverse a huge
if/elsestructure in the singleactionPerformed()method to determine the required action, it is more optimal to either:AbstractActionthat might be used for multiple controls (‘copy’ button, menu item etc.).Summary
So (generally) for the:
JFrame, don’t extend.