FlowLayout in Java AWT with Example code: This simple program is a type of various Java Layouts. The type of java layouts are,
- Grid Layout,
- Border Layout,
- Card Layout,
- & Flow Layout.
We have already discussed about GridLayout and CardLayouts here. If you want to know the various java layouts with program means just visit a link.
FLOW LAYOUT IN JAVA
This java program is used to arrange the objects in a direction of flow method and it is just like the texts are available in a paragraph. It uses a button to arrange the view by left or right and button will only available in horizontal position. It will not be the sequential and uses align property to align the lines and buttons.
[Useful: C Programming Guide] & [IT Companies Details]
FlowLayout Example Tutorial:
Q: Write a Java program to design a FlowLayout using AWT.
//Import statements import java.awt.*; import java.awt.event.*; //Class definition public class Fl extends Frame implements ActionListener { Variable declaration Button bu1,bu2,bu3; String mg="Demonstrating the layouts"; public Fl(String title) { super(title); setLayout(new FlowLayout());//Object creation bu1=new Button("Left Aligned"); bu2=new Button("Right Aligned"); bu3=new Button("Center Aligned"); bu1.addActionListener(this); bu2.addActionListener(this); bu3.addActionListener(this); add(bu1); add(bu2); add(bu3); } public void actionPerformed(ActionEvent e) { int val; if(e.getSource()==bu1)//Decision making statement { val=FlowLayout.LEFT; mg="Layout - Left aligned"; } else if(e.getSource()==b2) { val=FlowLayout.RIGHT; mg="Layout - Right aligned"; } else { val=FlowLayout.CENTER; mg="Layout - Center aligned"; } setLayout(new FlowLayout(val)); validate(); //screen is updated repaint(); } public void paint(Graphics g) { g.drawString(mg,100,150); //Graphics method } public static void main(String args[]) { Fl f=new Fl("FlowLayout"); f.setSize(300,300); f.show(); } }
SEE: All Examination Results & News