Good day. I am a beginner, only now I started learning Java. Recently, I found an example of initializing a GUI form using Java code. Here is how it is given:

import javax.swing.*; public class AbsoluteBoundsTest extends JFrame { public AbsoluteBoundsTest(){ super("Absolute bounds test"); JPanel content = new JPanel(); content.setLayout(null); JLabel lblFirstName = new JLabel("First name"); lblFirstName.setBounds(5,5,95,21); JLabel lblLastName = new JLabel("Last name"); lblLastName.setBounds(5,30,95,21); JTextField tfFirstName = new JTextField(20); tfFirstName.setBounds(100,5,120,21); JTextField tfLastName = new JTextField(20); tfLastName.setBounds(100,30,120,21); JButton btnOk = new JButton("Ok"); btnOk.setBounds(65,60,75,21); JButton btnCancel = new JButton("Cancel"); btnCancel.setBounds(145,60,75,21); content.add(lblFirstName); content.add(lblLastName); content.add(tfFirstName); content.add(tfLastName); content.add(btnOk); content.add(btnCancel); setSize(230,130); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setContentPane(content); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Throwable thrown) { thrown.printStackTrace(); } AbsoluteBoundsTest abt = new AbsoluteBoundsTest(); abt.setVisible(true); } } 

If I want to create one more form according to this principle, do I need to create it with another class? Or can be done in one class?

    3 answers 3

    It is better to create some abstract / ordinary class, and from it to make two heirs, IMHO.

      No, if class instances do not have links to any external resources (and in your example they do not use any global or limited resources except RAM itself), then all instances of this class will be independent copies.

      PS You should get a deeper insight into the very concept of "class" in programming.

      • Well, well, if I create two separate java classes with the same code in Intellij Idea, can they be somehow combined? so that from the first class, by pressing the button, the second class is called? - Slava
      • Then you will have a very strong duplication of the code, because there will be very much the same. Try to use inheritance, and the general functionality to take out in one method. - Alex654
      • @Slava: Yes; You should watch / read several video lessons in the genus "java classes", "java for beginners", "java for teapots". Moreover, you may even start your studies with C, because the very posing of your questions screams that you are very much confused in the most basic things. - Artyom Ionash

      This class runs the program in a frame (that is, in a window) and what is written in the constructor is already built on it. To solve this problem for you, you need to write a class (structure) and when you need to create an object. This will make you unique and you can create different forms, at the program stage, for example: through events or after doing something else.