Bitcoin

Bitcoin
Bitcoin

BorderLayout

BorderLayout is the default layout manager for the content pane of a JFrame, JWindow, JDialog, JInternalFrame, and JApplet. Place components against any of the four borders of the container and in the center. The component in the center fills the available space. You do not need specify all five areas of the container.
  1. Place components against any of the four borders of the container and in the center.
  2. The component in the center fills the available space.
  3. You do not need specify all five areas of the container.
  4. The component in the north region takes up the entire width of the container along its top.
  5. South does the same along the bottom.
  6. The heights of north and south will be the preferred heights of the added component.
  7. The east and west areas are given the widths of the component each contains, where the height is whatever is left in the container after satisfying north's and south's height requirements.
  8. Any remaining space is given to the component in the center region.
  9. Constants used to specify areas: CENTER, EAST, NORTH, SOUTH, WEST

Field

Following are the fields for java.awt.BorderLayout class −
  • static String AFTER_LAST_LINE − Synonym for PAGE_END.
  • static String AFTER_LINE_ENDS − Synonym for LINE_END.
  • static String BEFORE_FIRST_LINE − Synonym for PAGE_START.
  • static String BEFORE_LINE_BEGINS − Synonym for LINE_START.
  • static String CENTER − The center layout constraint (middle of the container).
  • static String EAST − The east layout constraint (right side of the container).
  • static String LINE_END − The component goes at the end of the line direction for the layout.
  • static String LINE_START − The component goes at the beginning of the line direction for the layout.
  • static String NORTH − The north layout constraint (top of the container).
  • static String PAGE_END − The component comes after the last line of the layout's content.
  • static String PAGE_START − The component comes before the first line of the layout's content.
  • static String SOUTH − The south layout constraint (bottom of the container).
  • static String WEST − The west layout constraint (left side of the container).
Its constructors:
public BorderLayout()
public BorderLayout(int hgap, int vgap)
Example of Code Written in Java for BorderLayout
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;

public class BorderLayoutExample extends JFrame {

  public static void main(String[] args) {
    BorderSample bs = new BorderSample();
    bs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = bs.getContentPane();
    pane.setLayout(new BorderLayout());
    JLabel label = new JLabel("North", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 36));
    label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    pane.add(label, BorderLayout.NORTH);
    label = new JLabel("South", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 36));
    label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    pane.add(label, BorderLayout.SOUTH);
    label = new JLabel("East", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 36));
    label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    pane.add(label, BorderLayout.EAST);
    label = new JLabel("West", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 36));
    label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    pane.add(label, BorderLayout.WEST);
    label = new JLabel("Center", JLabel.CENTER);
    label.setFont(new Font("Courier", Font.BOLD, 36));
    label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    pane.add(label, BorderLayout.CENTER);
    bs.setSize(400, 300);
    bs.setVisible(true);
  }

}
THE OUTPUT

ANOTHER CODE FOR BORDERLAYOUT
import java.awt.*;  
import javax.swing.*;  
  
public class BorderLayoutTute {  
   JFrame f;  
   BorderLayoutTute(){  
        f=new JFrame();  
      
        JButton b1=new JButton("NORTH");;  
        JButton b2=new JButton("SOUTH");;  
        JButton b3=new JButton("EAST");;  
        JButton b4=new JButton("WEST");;  
        JButton b5=new JButton("CENTER");;  
      
        f.add(b1,BorderLayout.NORTH);  
        f.add(b2,BorderLayout.SOUTH);  
        f.add(b3,BorderLayout.EAST);  
        f.add(b4,BorderLayout.WEST);  
        f.add(b5,BorderLayout.CENTER);  
      
        f.setSize(300,300);  
        f.setVisible(true);  
    }  
    public static void main(String[] args) {  
        new BorderLayoutTute();  
    }  
}  
The Output:
ANOTHER UNINTERESTING CODE FOR BORDERLAYOUT
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BorderLayoutDemo {
    public static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return;
        }

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button = new JButton("Button 1 (PAGE_START)");
        pane.add(button, BorderLayout.PAGE_START);

        //Make the center component big, since that's the
        //typical usage of BorderLayout.
        button = new JButton("Button 2 (CENTER)");
        button.setPreferredSize(new Dimension(200, 100));
        pane.add(button, BorderLayout.CENTER);

        button = new JButton("Button 3 (LINE_START)");
        pane.add(button, BorderLayout.LINE_START);

        button = new JButton("Long-Named Button 4 (PAGE_END)");
        pane.add(button, BorderLayout.PAGE_END);

        button = new JButton("5 (LINE_END)");
        pane.add(button, BorderLayout.LINE_END);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("BorderLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());
        //Use the content pane's default BorderLayout. No need for
        //setLayout(new BorderLayout());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
THE OUTPUT:

THE USELESS CODE FOR BORDERLAYOUT
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingLayoutDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingLayoutDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();  
      swingLayoutDemo.showBorderLayoutDemo();       
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showBorderLayoutDemo(){
      headerLabel.setText("Layout in action: BorderLayout");      

      JPanel panel = new JPanel();
      panel.setBackground(Color.darkGray);
      panel.setSize(300,300);
      BorderLayout layout = new BorderLayout();
      layout.setHgap(10);
      layout.setVgap(10);
      
      panel.setLayout(layout);        
      panel.add(new JButton("Center"),BorderLayout.CENTER);
      panel.add(new JButton("Line Start"),BorderLayout.LINE_START); 
      panel.add(new JButton("Line End"),BorderLayout.LINE_END);
      panel.add(new JButton("East"),BorderLayout.EAST);   
      panel.add(new JButton("West"),BorderLayout.WEST); 
      panel.add(new JButton("North"),BorderLayout.NORTH); 
      panel.add(new JButton("South"),BorderLayout.SOUTH); 

      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }
}




No comments:

Post a Comment

Facebook