BorderLayout as Layout Manager in Java

This tutorial is about the BorderLayout as Layout Manager in Java. A BorderLayout is a layout where it has a rectangular screen area divided into five regions - North, South, East, West, and Center position regions. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of borderLayout.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the BorderLayout class
  2. import javax.swing.*; //used to access the JFrame and JButton class
3. Initialize your variable in your Main, variable frame for JFrame, and 4 buttons namely btnNorth, btnSouth, btnEast, and btnWest as JButton.
  1. JFrame frame = new JFrame("BorderLayout as Layout Manager");
  2. JButton btnNorth, btnSouth, btnEast, btnWest;
  3.  
  4. btnNorth = new JButton("North");
  5. btnSouth = new JButton("South");
  6. btnEast = new JButton("East");
  7. btnWest = new JButton("West");
4. To set your program layout to BorderLayout, we will use the setLayout method of the JFrame and put inside the BorderLayout class on it.
  1. frame.getContentPane().setLayout(new BorderLayout());
Then, we will add the four buttons into the frame with a different positions in a BorderLayout.
  1. frame.getContentPane().add(btnNorth, BorderLayout.NORTH);
  2. frame.getContentPane().add(btnSouth, BorderLayout.SOUTH);
  3. frame.getContentPane().add(btnEast, BorderLayout.EAST);
  4. frame.getContentPane().add(btnWest, BorderLayout.WEST);
The BorderLayout.NORTH assumes that the button is in the top position, BorderLayout.SOUTH in the bottom, BorderLayout.EAST in the left, and BorderLayout.WEST is in the right position, respectively. 5. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  2. frame.setSize(200, 100);
  3. frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the BorderLayout class
  2. import javax.swing.*; //used to access the JFrame and JButton class
  3.  
  4. public class borderLayout{
  5.  
  6. public static void main(String[] args) {
  7. JFrame frame = new JFrame("BorderLayout as Layout Manager");
  8. JButton btnNorth, btnSouth, btnEast, btnWest;
  9.  
  10. btnNorth = new JButton("North");
  11. btnSouth = new JButton("South");
  12. btnEast = new JButton("East");
  13. btnWest = new JButton("West");
  14.  
  15. frame.getContentPane().setLayout(new BorderLayout());
  16. frame.getContentPane().add(btnNorth, BorderLayout.NORTH);
  17. frame.getContentPane().add(btnSouth, BorderLayout.SOUTH);
  18. frame.getContentPane().add(btnEast, BorderLayout.EAST);
  19. frame.getContentPane().add(btnWest, BorderLayout.WEST);
  20. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. frame.setSize(200, 100);
  22. frame.setVisible(true);
  23.  
  24. }
  25. }
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