Reputation: 2115
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
JFrame
JFrame
object is created inside the main
methodJFrame
AND implements ActionEvent
interfaceSure, 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!
Upvotes: 6
Views: 675
Reputation: 2165
in which the main class extends JFrame
where the JFrame object is created inside the main method
Upvotes: 0
Reputation: 168845
"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 JComponent
or JPanel
, then add that to a frame, ..applet, window, JInternalFrame
, dialog, constraint of a layout, part of a split pane..
As to the listeners. Rather than traverse a huge if
/else
structure in the single actionPerformed()
method to determine the required action, it is more optimal to either:
AbstractAction
that might be used for multiple controls ('copy' button, menu item etc.).So (generally) for the:
JFrame
, don't extend.Upvotes: 6
Reputation: 10789
Standard approach: use EventQueue in method main, that creates main form. In that case all your operations will be asynchronous
Upvotes: 0
Reputation: 4324
Honestly, it depends on the situation. One basic rule when coding is to "code to abstract classes or interfaces".
So, in a nutshell, have a class extending (or implementing) a JFrame (or whatever interface or class) and/or have one doing the same thing with ActionListener.
It is all about the maintainability, flexibility and cleanness of your code.
Upvotes: 2