No Layout in Java (Absolute Positioning)

This tutorial will teach you how to provide a java swing program without a layout. We will use the keyword null here in the setLayout method and we will do the Absolute Positioning using the setBounds method. So, now 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.
  1. 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.
  1. 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.
  1.     JLabel label1 = new JLabel("Name:");
  2.     JLabel label2 = new JLabel("Website:");
  3.    
  4.     JTextField textField1 = new JTextField("Lyndon R. Bermoy", 15);
  5.     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.
  1.  frame.getContentPane().setLayout(null);
Now, we will have an absolute positioning of our components and we will use the setBounds method. The setBounds method specify 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.
  1.     label1.setBounds(20, 20, 200, 40);
  2.     label2.setBounds(20, 60, 200, 40);
  3.    
  4.     textField1.setBounds(125, 20, 200, 40);
  5.     textField2.setBounds(125, 65, 200, 40);
5. Add then the components using the add method.
  1.     frame.getContentPane().add(label1);
  2.     frame.getContentPane().add(textField1);
  3.    
  4.      frame.getContentPane().add(label2);
  5.     frame.getContentPane().add(textField2);
Lastly, set its size, visibility to true, and have its close operation.
  1.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  2.     frame.setSize(400, 200);
  3.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; //used to access JFrame, JLabel, and JTextField class
  2.  
  3.  
  4. public class noLayout{
  5.  
  6.   public static void main(String[] args) {
  7.        
  8.     JFrame.setDefaultLookAndFeelDecorated(true);
  9.     JFrame frame = new JFrame("Without Layout");
  10.    
  11.     JLabel label1 = new JLabel("Name:");
  12.     JLabel label2 = new JLabel("Website:");
  13.    
  14.     JTextField textField1 = new JTextField("Lyndon R. Bermoy", 15);
  15.     JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
  16.      
  17.     label1.setBounds(20, 20, 200, 40);
  18.     label2.setBounds(20, 60, 200, 40);
  19.    
  20.     textField1.setBounds(125, 20, 200, 40);
  21.     textField2.setBounds(125, 65, 200, 40);
  22.    
  23.    
  24.     frame.getContentPane().setLayout(null);
  25.    
  26.     frame.getContentPane().add(label1);
  27.     frame.getContentPane().add(textField1);
  28.    
  29.      frame.getContentPane().add(label2);
  30.     frame.getContentPane().add(textField2);
  31.    
  32.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.     frame.setSize(400, 200);
  34.     frame.setVisible(true);
  35.   }
  36. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment