Friday, 14 March 2014

LAYOUT MANAGERS:FlowLayout

FlowLayout places component in rows from left to right. Components towards the end of row are written on next row............

FlowLayout

FlowLayout places component in rows from left to right. Components towards the end of row are written on next row, if there is not enough space in the current row. The FlowLayout honors the specified size of a component. The size of a component never changes regardless of size of container.

 constructors of FlowLayout


Typically the constructor is called in the call to the container's setLayout method (see example code). The parameterless FlowLayout() constructor is probably most common, but there are some good places to use the alignment.

  • FlowLayout();
  • FlowLayout(int alignment);
  • FlowLayout(int alignment, int hor_gap, int ver_gap);

Alignment can take values of constants - LEFT, CENTER and RIGHT. The default alignment for the components in a row is center. Default horizontal and vertical gaps are 5 pixels.


  • FlowLayout.LEFT
  • FlowLayout.CENTER
  • FlowLayout.RIGHT


simple program for FlowLayout


import java.awt.*;
import java.awt.event.*;
public class AwtLayoutDemo 
{
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;
public AwtLayoutDemo()
{
prepareGUI();
}
public static void main(String[] args)
{
AwtLayoutDemo  awtLayoutDemo = new AwtLayoutDemo();  
awtLayoutDemo.showFlowLayoutDemo();       
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() 
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}        
});    
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();        
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);  
}
private void showFlowLayoutDemo(){
headerLabel.setText("Layout in action: FlowLayout");      
Panel panel = new Panel();
panel.setBackground(Color.darkGray);
panel.setSize(200,200);
FlowLayout layout = new FlowLayout();
layout.setHgap(10);              
layout.setVgap(10);
panel.setLayout(layout);        
panel.add(new Button("OK"));
panel.add(new Button("Cancel"));       
controlPanel.add(panel);
 mainFrame.setVisible(true);  
}
}

output




No comments :

Post a Comment