No Layout in Java (Absolute Positioning)

May 28, 2020
Tutorials
without layout in java

This tutorial will show you how to provide a java swing program without a layout. We’ll be going to use the keyword null here in the setLayout method then do the Absolute Positioning using the setBounds method.

Let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of noLayout.java.

2. Import javax.swing.* package library import javax.swing.*; //used to access JFrame, JLabel, and JTextField class.

import javax.swing.*; //used to access JFrame, JLabel, and JTextField class

3. Now, we will have a User Interface of having the LookAndFeelDecorated UI and set it to true.

JFrame.setDefaultLookAndFeelDecorated(true);

We will initialize variables in our Main, variable frame as JFrame, label1 and label2 as JLabel, and textField1 and textField2 as JTextField.

JLabel label1 = new JLabel("Name:");
JLabel label2 = new JLabel("Website:");


JTextField textField1 = new JTextField("Lyndon R. Bermoy", 15);
JTextField textField2 = new JTextField("www.sourcecodester.com", 15);

4. To set the layout without a layout, we will use the keyword null here in the setLayout method of the frame.

frame.getContentPane().setLayout(null);

Now, we will proceed with the absolute positioning of our components with the use of the setBounds method. The setBounds method specifies the x-position, y-position, width of the components, and height of the components respectively. Here, we will use the setBounds method of the two labels and two textfields and make sure that they don’t overlap with each other.

label1.setBounds(20, 20, 200, 40);
label2.setBounds(20, 60, 200, 40);


textField1.setBounds(125, 20, 200, 40);
textField2.setBounds(125, 65, 200, 40);

5. Add then the components using the add method.

frame.getContentPane().add(label1);
frame.getContentPane().add(textField1);
 
 frame.getContentPane().add(label2);
frame.getContentPane().add(textField2);

 

Lastly, set its size, visibility to true, and have its close operation.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);

Output:

without layout in java

Here’s the full code of this tutorial:

import javax.swing.*; //used to access JFrame, JLabel, and JTextField class
 
 
public class noLayout{
 
  public static void main(String[] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Without Layout");
 
    JLabel label1 = new JLabel("Name:");
    JLabel label2 = new JLabel("Website:");
 
    JTextField textField1 = new JTextField("Lyndon R. Bermoy", 15);
    JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
 
    label1.setBounds(20, 20, 200, 40);
    label2.setBounds(20, 60, 200, 40);
 
    textField1.setBounds(125, 20, 200, 40);
    textField2.setBounds(125, 65, 200, 40);
 
 
    frame.getContentPane().setLayout(null);
 
    frame.getContentPane().add(label1);
    frame.getContentPane().add(textField1);
 
     frame.getContentPane().add(label2);
    frame.getContentPane().add(textField2);
 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.