Monday, July 29, 2013

FlowLayout

FlowLayout is one of layout managers that arranges and resizes the components placed on the window in a directional flow. The components flow  in the same way as the flow of characters in a line of text in a paragraph.

FlowLayout class has three constructors as shown below.
-FlowLayout()
creates a new flow layout with center alignment and a 5-pixcel space between the components.
-FlowLayout(int alignment)
creates a new flow layout with specified alignment. The value of the alignment can be FlowLayout.LEFT, FlowLayout.RIGHT, or FlowLayout.CENTER.
-FlowLayout(int alignment, int hor, int ver)
creates a new flow layout with specified alignment, horizontal and vertical spaces between components.

Example:
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setTitle(title);
setLayout(new FlowLayout(FlowLayout.LEFT));
setSize(new Dimension(500,300));

setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

for(int i='A';i<='Z';i++){
char c=(char)i;
add(new Button(c+""));
}

validate();
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using FlowLayout");

}
}


flowlayout layoutmanager

2 comments:

  1. Hi, i need a layout manager that can manager different sizes of components on Frame or Frame window. For example, the first row, i will place one text box. The second row contains list, text area, and two buttons. However, i don't want to want the two buttons to be resized automatically to become big as the list and text area. How can i do? Please suggest a good solution. Thanks

    ReplyDelete
  2. To solve your problem, you will use the GridBagLayout layout manager. It is more complex than GridLayout. However, it more flexible. http://javatheprogram.blogspot.com/2013/11/gridbaglayout.html

    ReplyDelete