Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Java GUI design

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 JFrame object is created inside the main method
  • where the main class extends JFrame AND implements ActionEvent interface
  • 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!

like image 240
Mihai Neacsu Avatar asked Dec 28 '11 22:12

Mihai Neacsu


People also ask

Can you make a GUI with Java?

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.

What should I use for GUI in Java?

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.

Is Java Swing good for GUI?

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.


2 Answers

"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..

Listeners

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:

  1. Create a listener for each control that needs it.
  2. Create an instance of an AbstractAction that might be used for multiple controls ('copy' button, menu item etc.).

Summary

So (generally) for the:

  1. JFrame, don't extend.
  2. Listeners, create and add as needed.
like image 169
Andrew Thompson Avatar answered Sep 28 '22 15:09

Andrew Thompson


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.

like image 24
Mechkov Avatar answered Sep 28 '22 17:09

Mechkov