Technicaltalk.netAlright! So this is my second tutorial in Java Programming that consider GUI. I’ll immediately get started, but first you can check the other tutorial in this link if you want to :
Creating Frames in JavaThat code will be used in this one.
OK, so first of all, this will explain how to add components to our last frame we created before. For example, let’s add one Label and one Text Field. This is how the code goes :
import java.awt.*;
import javax.swing.*;
public class JComponent {
public static void main (String [] args) {
JFrame f = new JFrame("Testing the Frame");
JLabel label = new JLabel ("Enter Your Text :");
JTextField text = new JTextField (20);
f.setVisible(true);
f.setSize(270,80);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout (new FlowLayout() );
f.add(label);
f.add(text);
}
}
Let’s look at what was added to the previous code. Some few lines were included to give the frame a label and a text field on it. Let’s take a closer look on that lines:
import java.awt.*;this code is for including a package. We need to set a layout so we can add the components in a particular form. So to do that, we first need to import the
java.awt package.
Then the next
FOUR line were explained previously. No need to repeat.
The line comes next :
JLabel label = new JLabel ("Enter Your Text :");This is how we create a
label. The idea is exactly the same as creating the Frame. We need to call the constructor and just give a name to the object. In this case, it is :
labelAnd as you can see, we include a caption or title to the label :
"Enter Your Text :"That’s optional. Meaning it just can be empty brackets :
()But better to give a caption for any label we create.
Next thing is creating a text field object. By this line :
JTextField text = new JTextField (20);Again, the same idea, we gave text as the name of the object and another thing which is inside the brackets, a Parameter.
It’s only the length of the object. And so, we set it as
20.
After that, we will see three common lines we had them before. I hope I don’t need to explain again, but if you’re not getting that, just go to the link above, You’ll find the details there.
Anyway, next line will be:
f.setLayout (new FlowLayout() );so this is how to set the layout we mentioned in the beginning.
We need to call the method
setLayout() following the frame name
f.
In the brackets, there is the Keyword
new followed by the layout name (or type) which is
FlowLayout(). And we don’t forget the empty brackets
().
Of course, there are several types of layout manager, but we don’t have to mention them all.
FlowLayout is only the better one in this code so we’ll just set it up and it does get the job done. That’s what all matters.
Next, adding the label and the text field to the Frame by the following:
f.add(label);
f.add(text);it obviously simple. Just calling method add and putting the object name which we want to add in between the brackets.
That’s it.
Check out the code and see the result.

*** Please do not forget to leave your comments about this article ***