Hi All,
/* I had created a JFrame with Jpanel having five JButtons on JPanel and their layout is set by choosing layout from menubar using of Actionlistener but problem is that effect is show after minize the window not instantly after selecting..*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LayoutManagersShow implements ActionListener
{
JFrame jf;
JMenuBar jb;
JMenu jm1,jm2,jm3;
JMenuItem m1,m2,m3,m4,m6,m7;
JMenuItem First,Next,Last,Previous,Desire;
JPanel jp;
JButton b1,b2,b3,b4,b5;
String str1;
LayoutManagersShow()
{
jf=new JFrame("LayOut Appearence");
jb=new JMenuBar();
jm1=new JMenu("Layout");
jm2=new JMenu("Quit");
jm3=new JMenu("CardLayout");
jb.add(jm1);
jb.add(jm2);
jf.setJMenuBar(jb);
m1=new JMenuItem("FlowLayout");
m2=new JMenuItem("GridLayout");
m3=new JMenuItem("BorderLayout");
m4=new JMenuItem("CardLayout");
m6=new JMenuItem("Exit");
jm1.add(m1);
jm1.add(m2);
jm1.add(m3);
jm1.addSeparator();
jm1.add(m4);
jm2.add(m6);
First=new JMenuItem("First");
Next=new JMenuItem("Next");
Last=new JMenuItem("Last");
Previous=new JMenuItem("Previous");
Desire=new JMenuItem("Desire");
jf.setLayout(new FlowLayout());
jf.setVisible(true);
jf.setSize(400,400);
jp=new JPanel(new FlowLayout());
b1=new JButton("FirstButton");
b2=new JButton("SecondButton");
b3=new JButton("ThirdButton");
b4=new JButton("FourthButton");
b5=new JButton("FifthButton");
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(b4);
jp.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
jp.setBackground(new Color((float)Math.random(),(float)Math.random(),(f loat)Math.random()));
jp.setPreferredSize(new Dimension(250,250));
jp.setVisible(true);
jf.add(jp);
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
m4.addActionListener(this);
m6.addActionListener(this);
First.addActionListener(this);
Next.addActionListener(this);
Last.addActionListener(this);
Desire.addActionListener(this);
Previous.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m1)
{
jp.setLayout(new FlowLayout());
}
if(e.getSource()==m2)
{
jp.setLayout(new GridLayout(3,2));
}
if(e.getSource()==m3)
{
jp.setLayout(new BorderLayout());
jp.add(b1,"North");
jp.add(b2,"South");
jp.add(b3,"East");
jp.add(b4,"West");
jp.add(b5,"Center");
}
if(e.getSource()==m4)
{
jb.add(jm3);
jm3.add(First);
jm3.add(Next);
jm3.add(Previous);
jm3.add(Desire);
jm3.add(Last);
CardLayout c=new CardLayout(20,20);
jp.setLayout(c);
jp.add(b1,"t1");
jp.add(b2,"t2");
jp.add(b3,"t3");
jp.add(b4,"t4");
jp.add(b5,"t5");
if(e.getSource()==First)
{
c.first(jp);
}
if(e.getSource()==Next){
c.next(jp);
}
if(e.getSource()==Last){
c.last(jp);
}
if(e.getSource()==Previous){
c.previous(jp);
}
if(e.getSource()==Desire){
c.show(jp,"t3");
}
}
if(e.getSource()==m6)
{
System.exit(0);
}
}
public static void main(String...s)
{
LayoutManagersShow lms=new LayoutManagersShow();
}
}