Friday, March 19, 2010

Example of Border Layout
import java.awt.*;


public class BorderPanelExample extends Frame
{
public BorderPanelExample(String title)
{
super(title);
addComponent("North", new Button("North"));
addComponent("South", new Button("South"));
addComponent("East", new Button("East"));
addComponent("West", new Button("West"));
addComponent("Center", new Button("Center"));
}
public void addComponent(String region, Component component) {
Panel panel = new Panel();
panel.add(component);
add(region, panel); // make sure you add the panel!
}
public static void main(String[] args)
{
BorderPanelExample bpe = new BorderPanelExample("BorderPanelExample");
bpe.setSize(200, 150);
bpe.show();
}
}


------------------------------------------------------------------------------------------
Card Layout

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

/*



*/

public class Card1 extends Applet implements ActionListener
{
Panel cardPanel; // the container that will hold the various "cards"
Panel firstP, secondP, thirdP; // each of these panels will constitute the "cards"
Panel buttonP; // panel to hold three buttons
Button first, second, third; // the three buttons
CardLayout ourLayout; // the card layout object


public void init()
{
//create cardPanel which is the panel that will contain the three "cards"
cardPanel = new Panel();
//create the CardLayout object
ourLayout = new CardLayout();
//set card Panel's layout to be our Card Layout
cardPanel.setLayout (ourLayout);


//create three dummy panels (the "cards") to show
firstP = new Panel();
firstP.setBackground(Color.blue);


secondP = new Panel();
secondP.setBackground(Color.yellow);


thirdP = new Panel();
thirdP.setBackground(Color.green);


//create three buttons and add ActionListener
first = new Button("First");
first.addActionListener(this);


second = new Button("Second");
second.addActionListener(this);


third = new Button("Third");
third.addActionListener(this);


//create Panel for the buttons and add the buttons to it
buttonP = new Panel(); // Panel's default Layout manager is FlowLayout
buttonP.add(first);
buttonP.add(second);
buttonP.add(third);


//setLayout for applet to be BorderLayout
this.setLayout(new BorderLayout());
//button Panel goes South, card panels go Center
this.add(buttonP, BorderLayout.SOUTH);
this.add(cardPanel, BorderLayout.CENTER);


// add the three card panels to the card panel container
// method takes 1.) an Object (the card) 2.) an identifying String
// first one added is the visible one when applet appears
cardPanel.add(firstP, "First"); //blue
cardPanel.add(secondP, "Second"); //yellow
cardPanel.add(thirdP, "Third"); //green


}


//------------------------------------
// respond to Button clicks by showing the so named Panel
// note use of the CardLayout method show(Container, "identifying string")


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == first)
ourLayout.show(cardPanel, "First");


if (e.getSource() == second)
ourLayout.show(cardPanel, "Second");


if (e.getSource() == third)
ourLayout.show(cardPanel, "Third");
}
}

------------------------------------------------------------------------------------------------
GridBagLayout

import java.awt.*;
import java.awt.event.*;
public class GBCal extends Frame{
Panel pTimes=new Panel();
Panel pAps=new Panel();
TextField txTimes=new TextField("09.00");
TextField txAps=new TextField("Meet the boss");
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public static void main(String argv[]){
GBCal gbc=new GBCal();
gbc.setLayout(new FlowLayout());
}

public GBCal() {
pTimes.add(txTimes);
pAps.add(txAps);
setLayout(gbl);
gbc.gridx=0;
gbc.gridy=0;
pTimes.setBackground(Color.pink);
add(pTimes,gbc);
gbc.gridx=1;
gbc.gridy=0;
pAps.setBackground(Color.lightGray);
add(pAps,gbc);
setSize(300,300);
setVisible(true);
}
}

--------------------------------------------------------------------------------------------
Mixed Layout

import java.awt.*;
import java.applet.Applet;


/*


*/




public class Mixed extends Applet
{
/* This example demonstrates nesting of containers inside other containers.
Each container can have its own Layout Manager. Thus, we can achieve finer
degress of control over component placement. The applet is the main container
and will use a BorderLayout. It will use three Panels at North, Center, and
South. The Center panel itself will contain 2 other panels inside of it.
*/


/* Note how all components are declared as class instance variables. Each method
in this class can have access to them. Do not redeclare such a component inside
of init().
*/


Panel nPanel, sPanel, cPanel, tcPanel, bcPanel;
Button one, two, three, four, five, six;
Label bottom, lbl1, lbl2, lbl3;


public void init()
{
nPanel = new Panel(); // north panel will hold three button
nPanel.setBackground(Color.blue); // give it color so you can see it
one = new Button("One");
two = new Button("Two");
three = new Button("Three");

/* Here is a bad idea. If you declared here, inside of init,
Button three = new Button ("Three"); the class instance Button three would be
"lost". It would LOOK like you had a button three on the applet, but it would
not generate any action events.
*/

// setLayout for North Panel and add the buttons
nPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
nPanel.add(one);
nPanel.add(two);
nPanel.add(three);


sPanel = new Panel(); // south Panel will just hold one Label
sPanel.setBackground(Color.yellow); // give it color so you can see it
bottom = new Label("This is South");

//set Layout and add Label
sPanel.setLayout (new FlowLayout(FlowLayout.CENTER));
sPanel.add (bottom);


cPanel = new Panel(); // center panel holds two other panels
tcPanel = new Panel(); // top panel on center panel holds three labels
tcPanel.setBackground(Color.gray); // give it color so you can see it
bcPanel = new Panel(); // bottom panel on center panel hold three buttons
bcPanel.setBackground(Color.green); // give it color so you can see it


lbl1 = new Label("One"); // the labels
lbl2 = new Label("Two");
lbl3 = new Label("Three");


four = new Button("Four"); // the buttons
five = new Button("Five");
six = new Button("Six");


//set Layout for top center Panel and add labels
tcPanel.setLayout (new GridLayout(1, 3, 5, 5)); // 1 row, 3 columns, 5 pixel gap
tcPanel.add(lbl1);
tcPanel.add(lbl2);
tcPanel.add(lbl3);


//set Layout for bottom center panel and add buttons
bcPanel.setLayout (new GridLayout(3, 1, 5, 5)); // 3 rows, 1 col, 5 pixel gap
bcPanel.add(four);
bcPanel.add(five);
bcPanel.add(six);


//add two center panels (top and bottom) to the center panel
cPanel.setLayout(new GridLayout(2, 1)); // 2 rows, 1 col, no gaps
cPanel.add(tcPanel);
cPanel.add(bcPanel);


//set Layout for the Applet itself and add the panels
this.setLayout (new BorderLayout());
add(nPanel, BorderLayout.NORTH);
add(sPanel, BorderLayout.SOUTH);
add(cPanel, BorderLayout.CENTER);
}
}

-----------------------------------------------------------------------------------------------

Swing Concepts



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import javax.swing.*;



public class layout1 extends JFrame {



JPanel panNorth;

JPanel panWest;

JPanel panCenter;



public layout1() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



setLayout(new BorderLayout());



// set up top panel

panNorth = new JPanel();

panNorth.setLayout(new FlowLayout());

panNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

JLabel lblUpper = new JLabel("Upper Panel");

panNorth.add(lblUpper);

add(panNorth, BorderLayout.NORTH);



// set up left panel

panWest = new JPanel();

panWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

panWest.setLayout(new GridLayout(2, 1));

JButton btnFirst = new JButton("First");

panWest.add(btnFirst);

JButton btnSecond = new JButton("Second");

panWest.add(btnSecond);

add(panWest, BorderLayout.WEST);



// set up center panel

panCenter = new JPanel();

panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

panCenter.setLayout(new BorderLayout());

JLabel lblMiddle = new JLabel("Middle thing");

lblMiddle.setHorizontalAlignment(JLabel.CENTER);
panCenter.add(lblMiddle, BorderLayout.CENTER);

add(panCenter);



setSize(300, 300);

setVisible(true);

}



public static void main(String args[]) {

new layout1();

}

}
-------------------------------------------------------------------------------------------


By Homera A Durani (Lecturer )
MCA Department (AITS)
Rajkot

No comments:

Post a Comment