Pages

Set JFrame layout using Border Layout

Source code below will show you how to add component into JFrame that use Border Layout as it's Layout Manager. If you use Border Layout, you must know 5 region that will be use by Border Layout to arrange component in a JFrame:

NORTH

WEST

CENTER

EAST

SOUTH



You cannot change this setting. If you want to add a component, you also must set location of your component. For example, if you want your component locate at SOUTH, you must use BorderLayout.SOUTH. You can see it in complete source code below. Don't forget to set your location.

********************************************************************************
COMPLETE SOURCE CODE FOR : BorderLayoutForJFrame.java
********************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;

public class BorderLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Border Layout for JFrame layout manager )
JFrame frame=new JFrame("Border Layout for JFrame layout manager");

//Set JFrame layout to Border Layout
frame.setLayout(new BorderLayout());

//Create 5 buttons that we want to add into JFrame
JButton button1=new JButton("NORTH");
JButton button2=new JButton("WEST");
JButton button3=new JButton("CENTER");
JButton button4=new JButton("EAST");
JButton button5=new JButton("SOUTH");

//Add all buttons into JFrame
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.CENTER);
frame.add(button4,BorderLayout.EAST);
frame.add(button5,BorderLayout.SOUTH);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


********************************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************************
Related Posts Plugin for WordPress, Blogger...