Different types of layouts in Java AWT: There are various layouts in Java AWT like border layout, grid layout and flow layout. Few many are there. But this simple AWT program in java is used to talk about these 3 alone. This AWT tutorial is easy to understand. An example source code is given below.
Read: Java AWT color example
DIFFERENT LAYOUTS IN JAVA AWT
This Java AWT program will creates the content to display in all type of layouts. Such as grid, list, flow layout by selecting using buttons and display by using the decision making statements.
[Useful: C Programming Guide] & [IT Companies List]
Various Layouts in Java AWT:
Q: Write a Java program to display grid, flow and border layouts using AWT class.
//Import statements import java.awt.*; import java.awt.event.*; //Class definition public class LayoutDemo extends Frame implements ActionListener { //Variables declaration Button Bolayout,Golayout,Folayout,exit; Button butA; //Main method public LayoutDemo() { setSize(300,300); setLayout(new FlowLayout()); Bolayout=new Button("Border Layout");//Object creation Golayout=new Button("Grid Layout"); Folayout=new Button("Flow Layout"); exit=new Button("Exit"); butA=new Button("1"); Bolayout.addActionListener(this); Golayout.addActionListener(this); Folayout.addActionListener(this); exit.addActionListener(this); addWindowListener(new MyWindowListener()); add(Bolayout); add(Golayout); add(Folayout); add(exit); add(butA); setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==Folayout)//Decision making statement { setLayout(new FlowLayout()); //arranges the components according to the layout validate(); // pack(); // © http://students3k.com – Karthikh Venkat } if(ae.getSource()==Golayout) { setLayout(new GridLayout(2,2)); //pack(); validate(); } if(ae.getSource()==Bolayout) { setLayout(new BorderLayout()); add(Bolayout,BorderLayout.NORTH); add(Golayout,BorderLayout.EAST); add(Folayout,BorderLayout.WEST); add(butA,BorderLayout.SOUTH); add(exit,BorderLayout.CENTER); // pack(); validate(); // © http://students3k.com – Karthikh Venkat } if(ae.getSource()==exit) { System.exit(2); } } public static void main(String[] args) { LayoutDemo obj=new LayoutDemo(); } }