Null layout manager and absolute positioning - Java Swing

Java examples for Swing:Null Layout

Introduction

null layout manager means no layout manager.

null layout manager is known as absolute positioning.

The following code uses a null layout manager for the content pane of a JFrame.

Demo Code

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Null Layout Manager");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(null);/*  ww  w . j a  v a2s .  co m*/

    JButton b1 = new JButton("Small Button 1");
    JButton b2 = new JButton("this is a test 2...");
    contentPane.add(b1);
    contentPane.add(b2);

    b1.setBounds(10, 10, 100, 20);
    b2.setBounds(120, 10, 150, 20);

    frame.setBounds(0, 0, 350, 100);
    frame.setVisible(true);
  }
}

Related Tutorials