Using FlowLayout in our Application
We all (may be) know that in java,Layout Manager is used to lay the components in the containers like Frame,panel. There are many layout managers available in java among which FlowLayout manager is also the one.
To apply a particular Layout to a container.
- First we need to create the particular Layout Object which we would like to apply in our application.
- Next pass the Layout Object as an argument to the setLayout() method.
We can create the FlowLayout in two ways
1. Using the default FlowLayout constructor like as follows,
Example:
FlowLayout f=new FlowLayout();
setLayout(f);
2.This method involves using the parameterized constructor of the FlowLayout class which provides some flexibility in laying the components in the container.
Example:
FlowLayout f=new FlowLayout(int align);
setLayout(f);
FlowLayout f=new FlowLayout(int align,int hori-dist,int vert-dist);
setLayout(f);
align-Specifies some of the predefined constants like FlowLayout.CENTER , Flow Layout.LEFT,FlowLayout.RIGHT.
hori-dist- Specifes the horizontal distance between the components as well as between the edges of the container and the components.
Veri-dist- Specifies the vertical distance between the components as well as between the edges of the container and the components.
Program:
FlowLayoutdemo.java
import java.util.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
class Flowlayoutdemo extends JFrame
{
public Flowlayoutdemo()
{
setTitle("FlowLayoutDemo");
setLocationByPlatform(true);
setSize(400,400);
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
class Flowlayoutdemo extends JFrame
{
public Flowlayoutdemo()
{
setTitle("FlowLayoutDemo");
setLocationByPlatform(true);
setSize(400,400);
// creation of base panel
JPanel jp=new JPanel();//By default JPanel has FlowLayout Manager
jp.setBorder(new LineBorder(Color.RED,10,false));
//creation of the first panel for having components
JPanel jp1=new JPanel();
jp1.setBorder(new LineBorder(Color.ORANGE,10,true));
JButton button1=new JButton("NEXT");
//creatin of the second panel for having components
JPanel jp2=new JPanel();
jp2.setBorder(new LineBorder(Color.GREEN,10,true));
JButton button2=new JButton("BACK");
jp1.add(button1);
jp2.add(button2);
jp.add(jp1);
jp.add(jp2);
add(jp);
}
}
Flowlayoutmain.java
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
class Flowlayoutmain
{
public static void main(String args[])throws IOException
{
Flowlayoutdemo f1=new Flowlayoutdemo();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import java.io.*;
class Flowlayoutmain
{
public static void main(String args[])throws IOException
{
Flowlayoutdemo f1=new Flowlayoutdemo();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}
As you see the first program which i have shown above i haven't used the default constructor for creating flow layout because JPanel has FlowLayout by default.
If you use the default constructor for creating flowlayout then all the components that you would add will be placed at the top initially at CENTER until you specify the alignment.
The output of the above program would be as follows.
If you see the
output you can notice that there is no considerable distance between the
components and also if you keep on adding components to the container
then the components will be positioned in the second row and so on.
If you use the parameterized constructor for creating flowlayout it enables you to position the components at the specified location if the pixel coordinates of the top-left corner of the component is given.
For example, if you set the basepanel( object jp used in the first program) with the following flowlayout configuration,
JPanel jp=new JPanel(new FlowLayout(FlowLayout.CENTER,20,100));
Here we have used CENTER alignement,hori-dist=20,vert-dist=100.These values would modify the output as follows.
Here i would like to highlight you one thing that the "position of the components will not be affected by the alignment you use because they are positioned according to the horizontal distance and vertical distance that you have specified".
0 comments:
Post a Comment