Basic Java GUI Design

In the GUI book we use in the class, there are many examples of how graphical user interfaces are created in Java. There are so many examples that I am very confused as to which one should be used when it comes to a large application.

So I saw examples

  • in which the main class extends the JFrame
  • where the JFrame is created inside the main method
  • where the main class extends the JFrame and implements the ActionEvent interface
  • where listener classes are declared inside the main class

Of course, I can work with all of these, but right now, since I have no experience, I do not see the benefits of using any of them. In fact, is one of them the right way to do this, or does it depend on my desire?

Thanks!

+6
source share
4 answers

Is There A or Is There A? This is a question to ask when considering extending a class. If the new class is "Is A", the extended frame, but if the class just needs a frame reference, do not expand .

In fact, if a custom component is required , extend the JComponent or JPanel , then add it to the frame, .. applet, window, JInternalFrame , dialog, layout constraint, part of the split panel.

Listeners

As for the listeners. Instead of traversing the huge if / else structure in one actionPerformed() method to determine the desired action, this is more optimal for:

  • Create a listener for each control that needs it.
  • Create an instance of AbstractAction that can be used for several controls (copy button, menu item, etc.).

Summary

So (usually) for:

  • JFrame not distributed.
  • Listeners, create and add as needed.
+6
source

Honestly, it depends on the situation. One of the basic rules for coding is "coding abstract classes or interfaces."

So, in short, there is a class that extends (or implements) a JFrame (or any other interface or class) and / or performs a single action with an ActionListener.

It's all about maintainability, flexibility and cleanliness of your code.

+2
source

Standard approach: use the EventQueue in the main method, which creates the main form. In this case, all your operations will be asynchronous

0
source

in which the main class extends the jframe

  • The main calss does not need to extend the JFrame. if not, you must create a JFrame object like any other class.

where the jframe is created inside the main method

  • If MainClass extends the JFrame, it is created inside c'tor (in super ()).
0
source

Source: https://habr.com/ru/post/904637/


All Articles