Java AWT Insets

Introduction

To leave space between the container and the window that contains it, use "Insets".

To do this, override the getInsets() method that is defined by Container.

The getInsets() method has this general form:

Insets getInsets() 

The constructor for Insets is shown here:

Insets(int top, int left, int bottom, int right) 

import java.awt.BorderLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

class Demo extends JPanel { 
  public Demo() {
    setLayout(new BorderLayout());
    add(new JButton("This is across the top."), BorderLayout.NORTH);
    add(new JLabel("The footer message might go here."), BorderLayout.SOUTH);
    add(new JButton("Right"), BorderLayout.EAST);
    add(new JButton("Left"), BorderLayout.WEST);

    add(new JTextArea("demo from demo2s.com"), BorderLayout.CENTER);
  }/*w w  w  .  j av a 2  s  .  c  o m*/
  @Override  
  public Insets getInsets() {  
    return new Insets(10, 10, 10, 10);  
  }  
}

public class Main {
  public static void main(String[] args) {
    Demo panel = new Demo();

    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related