import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SelectLayout extends JFrame implements ItemListener{
public static void main (String[] args) {
SelectLayout sl=new SelectLayout();
}
JMenuBar menuBar;
JMenu lmenu,cmenu;
JRadioButtonMenuItem lrbmenuItem,crbmenuItem;
JPanel menuPanel,bPanel;
JButton button;
public SelectLayout() {
super("Select Layout in swing");
menuPanel=new JPanel();
menuBar=new JMenuBar();
lmenu=new JMenu("layout");
ButtonGroup gp=new ButtonGroup();
lrbmenuItem =new JRadioButtonMenuItem("BorderLayout");
lrbmenuItem.setMnemonic(KeyEvent.VK_B);
lrbmenuItem.addItemListener(this);
gp.add(lrbmenuItem);
lmenu.add(lrbmenuItem);
lrbmenuItem =new JRadioButtonMenuItem("FlowLayout");
lrbmenuItem.setMnemonic(KeyEvent.VK_F);
lrbmenuItem.addItemListener(this);
lrbmenuItem.setSelected(true);
gp.add(lrbmenuItem);
lmenu.add(lrbmenuItem);
lrbmenuItem =new JRadioButtonMenuItem("GridLayout");
lrbmenuItem.setMnemonic(KeyEvent.VK_G);
lrbmenuItem.addItemListener(this);
gp.add(lrbmenuItem);
lmenu.add(lrbmenuItem);
lrbmenuItem =new JRadioButtonMenuItem("CardLayout");
lrbmenuItem.setMnemonic(KeyEvent.VK_C);
lrbmenuItem.addItemListener(this);
gp.add(lrbmenuItem);
lmenu.add(lrbmenuItem);
menuBar.add(lmenu);
//second menu
cmenu=new JMenu("CardLayout");
crbmenuItem =new JRadioButtonMenuItem("First");
crbmenuItem.setMnemonic(KeyEvent.VK_F);
crbmenuItem.setSelected(true);
crbmenuItem.addItemListener(this);
gp.add(crbmenuItem);
cmenu.add(crbmenuItem);
crbmenuItem =new JRadioButtonMenuItem("last");
crbmenuItem.setMnemonic(KeyEvent.VK_L);
crbmenuItem.addItemListener(this);
gp.add(crbmenuItem);
cmenu.add(crbmenuItem);
crbmenuItem =new JRadioButtonMenuItem("next");
crbmenuItem.setMnemonic(KeyEvent.VK_N);
crbmenuItem.addItemListener(this);
gp.add(crbmenuItem);
cmenu.add(crbmenuItem);
crbmenuItem =new JRadioButtonMenuItem("previous");
crbmenuItem.setMnemonic(KeyEvent.VK_P);
crbmenuItem.addItemListener(this);
gp.add(crbmenuItem);
cmenu.add(crbmenuItem);
cmenu.setEnabled(false);
menuBar.add(cmenu);
menuPanel.add(menuBar);
add(menuPanel,BorderLayout.NORTH);
bPanel=new JPanel();
for (int i = 0; i<4; i++) {
button=new JButton("button");
bPanel.add(button);
}
bPanel.setLayout(new GridLayout());
add(bPanel,BorderLayout.CENTER);
//setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if(e.paramString().equals("BorderLayout"))
{
bPanel.setLayout(new BorderLayout());}
if(e.paramString().equals("FlowLayout"))
button.setLayout(new FlowLayout());
if(e.paramString().equals("GridLayout"))
bPanel.setLayout(new GridLayout(2,2));
if(e.paramString().equals("CardLayout"))
{
cmenu.setEnabled(true);
setLayout(new CardLayout());}
}
}
syed aamiruddin 0 Newbie Poster
cbarton.a 0 Newbie Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
stultuske 1,116 Posting Maven Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.