JAVA - How To Use BorderLayout In Java NetBeans

BorderLayout Example In Java NetBeans

                                                                                                                                                            

In this java Code we will see How To Use The BorderLayout LayoutManager In Java NetBeans

Source Code:

package javaapp;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingLayout {

    public static void main(String[] args){
        
      JFrame frame = new JFrame("BorderLayout");
      
      JPanel panel1 = new JPanel();
      
      panel1.setLayout(new BorderLayout());
      
      // set the button in the top of the container
      panel1.add(new JButton("NORTH"), BorderLayout.NORTH);
      
      // set the button in the right of the container
      panel1.add(new JButton("EAST"), BorderLayout.EAST);
      
      // set the button in the middle of the container
      panel1.add(new JButton("CENTER"), BorderLayout.CENTER);
      
      // set the button in the top of left container
      panel1.add(new JButton("WEST"), BorderLayout.WEST);
      
      // set the button in the bottom of the container
      panel1.add(new JButton("SOUTH"), BorderLayout.SOUTH);
     
      // create an instance of borderlayout with gaps
      BorderLayout bl = new BorderLayout(50,50);
      
      JPanel panel2 = new JPanel(bl);
      
      panel2.add(new JButton("NORTH"), BorderLayout.NORTH);
      panel2.add(new JButton("EAST"), BorderLayout.EAST);
      panel2.add(new JButton("CENTER"), BorderLayout.CENTER);
      panel2.add(new JButton("WEST"), BorderLayout.WEST);
      panel2.add(new JButton("SOUTH"), BorderLayout.SOUTH);
      
        bl.removeLayoutComponent(new JButton("NORTH"));
      
      // return the state of the borderlayout Hgap and Vgap
      System.out.println(bl.toString());
      
      // return the borderlayout Hgap and Vgap
      System.out.println(bl.getHgap()+" - "+bl.getVgap());     
      
      frame.add(panel2);     
       
      frame.setSize(400, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      
    }
}


//OUTPUT:

borderlayout in java
using the panel1

java borderlayout
using the panel2

java.awt.BorderLayout[hgap=50,vgap=50]
50 - 50



Share this

Related Posts

Previous
Next Post »