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!
The Java language provides a set of user interface components from which GUI forms can be built. The IDE's GUI Builder assists you in designing and building Java forms by providing a series of tools that simplify the process.
It comprises graphical units like buttons, labels, windows, etc. via which users can connect with an application. Swing and JavaFX are two commonly used applications to create GUIs in Java.
Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.
"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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With