TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Swing

Java Swing BorderLayout

Overview

In this tutorial, we will learn about Java Swing BorderLayout with an example demo program. A border layout lays out a container, arranging and resizing its components to fit in five regions:

  • NORTH
  • SOUTH
  • EAST
  • WEST
  • CENTER

BorderLayout also supports relative positioning in the container:

  • PAGE_START
  • PAGE_END
  • LINE_START
  • CENTER
  • LINE_END

Java Demo Program

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

/****************************************
 * Filename: BorderLayoutDemo.java
 * 
 * Java Tutorials - www.TestingDocs.com
 *****************************************/


//BorderLayout Demo Program
public class BorderLayoutDemo {
 public static void main(String[] args) {

 JFrame frame = new JFrame("BorderLayout - www.TestingDocs.com");
 JButton btn1 = new JButton("Button [NORTH]");
 JButton btn2 = new JButton("Button [CENTER]");
 JButton btn3 = new JButton("Button [WEST]");
 JButton btn4 = new JButton("Button [SOUTH]");
 JButton btn5 = new JButton("Button [EAST]");

 JPanel panel = new JPanel(new BorderLayout());
 panel.add(btn1, BorderLayout.NORTH);
 panel.add(btn2, BorderLayout.CENTER);
 panel.add(btn3, BorderLayout.WEST);
 panel.add(btn4, BorderLayout.SOUTH);
 panel.add(btn5, BorderLayout.EAST);
 

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(600,300);
 frame.getContentPane().add(panel);
 frame.setVisible(true);
 }
}

Output

Run the Java application to view the output.

Java Swing BorderLayout

 

We can use the constants of the BorderLayout class to indicate the area we want to place a component in the container.

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

 

Related Posts

Add GUI Components Swing

Java Swing /

Add GUI Components to a Swing Container

JPanel Container Class

Java Swing /

Swing JPanel Container Class

Swing CardLayout

Java Swing /

Java Swing CardLayout

Java Swing GridLayout

Java Swing /

Java Swing GridLayout

Java Swing FlowLayout

Java Swing /

Java Swing FlowLayout

‹ Java Swing JFileChooser› Java Swing FlowLayout

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version