Java Program to implement Layout Manager

Write a java program to implement Layout Manager:-

public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ActionListener
{
Panel mainPanel, p1, p2, p3, p4, p5, topPanel;
CardLayout cl;
TextField txt[];
Button btn[], b1, b2, b3, b4, b5;
TextArea ta[];
Label lbl[];
Choice choice[];
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(400,300);
addControls();
setVisible(true);
}
private void addControls()
{
cl = new CardLayout();
mainPanel = new Panel();
mainPanel.setLayout(cl);
int i;
GridLayout gl = new GridLayout(5,10,5,5);
p1 = new Panel();
p1.setLayout(gl);
txt = new TextField[50];
for (i=0;i<txt.length;i++)
{
txt[i] = new TextField("Text " + (i+1));
p1.add(txt[i]);
}
p2 = new Panel();
p2.setLayout(gl);
btn = new Button[50];
for (i=0;i<btn.length;i++)
{
btn[i] = new Button("Button " + (i+1));
btn[i].addActionListener(this);
p2.add(btn[i]);
}
p3 = new Panel();
p3.setLayout(gl);
ta = new TextArea[50];
for (i=0;i<ta.length;i++)
{
ta[i] = new TextArea("Text " + (i+3));
p3.add(ta[i]);
}
p4 = new Panel();
p4.setLayout(gl);
lbl = new Label[50];
for (i=0;i<lbl.length;i++)
{
lbl[i] = new Label("Label " + (i+4));
p4.add(lbl[i]);
}
p5 = new Panel();
p5.setLayout(gl);
choice = new Choice[50];
for (i=0;i<choice.length;i++)
{
choice[i] = new Choice();
p5.add(choice[i]);
}
mainPanel.add(p1,"panel1");
mainPanel.add(p2,"panel2");
mainPanel.add(p3,"panel3");
mainPanel.add(p4,"panel4");
mainPanel.add(p5,"panel5");
add(mainPanel);
b1 = new Button("Card 1");
b2 = new Button("Card 2");
b3 = new Button("Card 3");
b4 = new Button("Card 4");
b5 = new Button("Card 5");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
topPanel = new Panel();
topPanel.setLayout(new FlowLayout());
topPanel.add(b1);
topPanel.add(b2);
topPanel.add(b3);
topPanel.add(b4);
topPanel.add(b5);
add(topPanel, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == b1)
cl.show(mainPanel,"panel1");
else if (ae.getSource() == b2)
cl.show(mainPanel,"panel2");
else if (ae.getSource() == b3)
cl.show(mainPanel,"panel3");
else if (ae.getSource() == b4)
cl.show(mainPanel,"panel4");
else if (ae.getSource() == b5)
cl.show(mainPanel,"panel5");
else
{
int i;
boolean found = false;
for (i=0;i<btn.length;i++)
{
if (ae.getSource() == btn[i])
{
found = true;
break;
}
}
if (found)
{
System.out.println("Button Clicked from panel : " +
btn[i].getLabel());
}
}
}
}
import java.awt.event.*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

Related Posts