JAVA ASTRACT WINDOW TOOLKIT- II
Layout Managers
The AWT provides 4 basic layout managers:
vFlowLayout
vGridLayout
vBorderLayout
vCardLayout
FlowLayout
The FlowLayout
class is the most basic of layout Using the flow layout,components are added to
the panel one at a time ,row by row.
Constructor |
Action |
FlowLayout() |
Constructs
a new flow layout with left alignment,leaving a vertical and horizontal gap
of 5 pixels. |
FlowLayout(int
align) |
Constructs
a new flow layout with the alignment,specified leaving a vertical and
horizontal gap of 5 pixels. |
FlowLayout(int
align),constructs a new flow layout with the alignment specified,int vgap,int
hgap) |
constructs
a new flow layout with the alignment specified,int vgap,int hgap)leaving a
vertical and horizontal gap as specified |
Method |
Action |
getAlignment() |
Gets the alignment for the layout |
getHgap() |
Gets the horizontal
gapbetweencomponents |
getVgap() |
Gets the verticalgap betweencomponents |
setAlignment(int align) |
Sets the alignment for the
specified layout |
setHgap(int hgap) |
Sets the horizontal gap for the
specified layout |
setVgap(int vgap) |
Sets the vertical gap for the
specified layout |
Example
File name: FlowLayoutDemo.java
import
java.applet.*;
import
java.awt.*;
/*
<applet
code="FlowLayoutDemo" width=300 height=200>
</applet>
*/
public class
FlowLayoutDemo extends Applet
{
public void
init()
{
setLayout(new
FlowLayout(FlowLayout.CENTER,5,5));
for(int
i=1;i<=10;i++)
{
add(new
Button("BUTTON"+i));
}
}
}
Output
Grid Layout
The
Grid Layout class lays out components in a way very similar to a spreadsheet-in
rows and column.
The
grid layout can be constructed using the constructors listed in the table
below:
Constructor |
Action |
Gridlayout() |
Creates a grid layout with a default of
one columnpercomponent in a single row |
Gridlayout(int rows,int cols) |
Creates a grid layout with he specified
rows&columns |
Gridlayout(int rows,int cols,int hgap,int
vgap) |
Creates a grid layout with he specified
rows&columns and specified horizontal and vertical gaps |
Method |
Action |
getColumns() |
Getsthenumber ofcolumns in corresponding
layout |
getRows() |
Gets thenumber ofrows in corresponding
layout |
getHgap() |
Gets thehorizontalgap for the in
corresponding layout |
getVgap() |
Gets the verticalgap for the in corresponding
layout |
setColumns(int cols) |
Sets the number of columns to the
specified number in the corresponding layout |
setHgap(int
hgap) |
Sets the horizontal gap to the value
specified in the corresponding layout |
setVgap(int
vgap) |
Sets the vertical gap to the value
specified in the corresponding layout |
setRows(int
rows) |
Sets the number of rows in the
corresponding layout to the specified value |
Example
File name: GridLayoutDemo.java
import
java.awt.*;
import
java.applet.*;
/*
<applet
code="GridLayoutDemo" width=300 height=200>
</applet>
*/
public
class GridLayoutDemo extends Applet{
static
final int n=4;
public
void init(){
setLayout(new
GridLayout(n,n));
setFont(new
Font("SansSerif",Font.BOLD,24));
for(int
i=0;i<n;i++){
for(int
j=0;j<n;j++){
int
k=i*n+j;
if(k>0){
add(new
Button(""+k));
}
}
}
}
}
Output
Border Layouts
Borders layouts behave differently
from flow and grid layouts. When you add a component to a panel that uses a
border layout, you indicate its placement as a geographic direction: north,
south, east, west, and center.
Constructor |
Action |
BorderLayout() |
Creates
a new border layout with no gap between the components |
BorderLayout(int hgap, int vgap) |
Creates
a border layout with the specified horizontal and vertical gap between
components. |
The Various methods that can be used in
conjunction with the border layout is given below:
Method |
Action |
getHgap() |
Returns
the horizontal gap between components |
getVgap() |
Returns
the vertical gap between components |
setHgap(int hgap) |
Sets
the horizontal gap between components to the value specified. |
Border Layout
File name: BorderLayoutDemo.java
import
java.awt.*;
import
java.applet.*;
import
java.util.*;
/*
<applet
code="BorderLayoutDemo" width=400 height=200>
</applet>
*/
public
class BorderLayoutDemo extends Applet{
public
void init(){
setLayout(new
BorderLayout());
add(new
Button("This is across the top."),BorderLayout.NORTH);
add(new
Label("The footer message might go here."),BorderLayout.SOUTH);
add(new
Button("Right"),BorderLayout.EAST);
add(new
Button("Left"),BorderLayout.WEST);
String
msg="The reasonable man adapts"+"himself to the
world;\n"+"the unreasonable one persist in"+"trying to
adapt the world to himself.\n"+"Therefore all progress
depends"+"On the unreasonable man.\n\n"+" -George Bernard Shaw\n\n";
add(new
TextArea(msg),BorderLayout.CENTER);
}
}
Output
Card Layouts
Card layouts are different from the other layouts. Unlike
with the other three layouts, when you add components to a card layout,. they
are not all displayed on the screen at once. Card layouts are used to produce slide shows of
components, one at a time.
Card
Layout provides these two constructors:
Card
Layout()
Card
Layout (int horz, int vert)
The first form creates a default card layout. The second
form allows you to specify the horizontal and vertical space left between
components in horz and vert, respectively.
For
example, here's how to create a card layout containing three cards:
setLayout(new
Card layout());
Panel
one = new Panel()
add("first",one);
Panel
two = new Panel()
add
("second",two);
Panel
three = new Panel()
add("third",three);
show
(this,"second");
Your
program activates a card by calling one of the following methods defined by
Card Layout:
void
first(Container deck)
void
last(Container deck)
void
next(Container deck)
void
previous(Container deck)
void
show(Container deck,String card Name)
Here,
deck is a reference to the container (usually a panel)that holds the cards, and
card Name is the name of a card
File name: BorderText.java
import
java.awt.*;
/*<applet
code="BorderText" width=400 height=300>
</applet>
*/
public
class BorderText extends java.applet.Applet{
public
void init(){
setLayout(new
BorderLayout(5,5));
add("South",new
Button("Bottom of the Applet"));
add("North",new
Button("Top of the Applet"));
add("East",new
Button("Right"));
add("West",new
Button("Left"));
add("Center",new
TextArea("Appears at the centre"));
}
public
Insets getInsets(){
return
new Insets(20,20,10,10);
}
}
Output
Creating a Frame Window is an Applet
Insets
public
Insets insets(){
return
new Insets(10,10,10,10);
}
Panels
set
Layout(new Grid Layout(1,2,10,10));
Panel
panel1 = new Panel();
panel
panel2 = new Panel();
add(panel1);
add (panel2);
File name: Frame2.java
class
Frame2 extends Frame
{
Frame2(String
title)
{
super(title);
addWindowListener(new
WindowAdapter()){
public
void windowClosing(WindowEvent we){
dispose();
}
});
}
}
File name: FrameApplet.java
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
/*
<applet
code="FrameApplet" width=250 height=200>
</applet>
*/
public
class FrameApplet extends Applet implements ActionListener
{
public
void init(){
Button
b=new Button("Create Frame");
b.addActionListener(this);
add(b);
}
public
void actionPerformed(ActionEvent ae){
Frame2
f2=new Frame2("My new Window");
f2.show();
f2.setSize(300,300);
}
}
Output
Menus
File name: MenuFrame.java
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
//Create
a subclass of Frame
class
MenuFrame extends Frame
{
String
msg ="";
CheckboxMenuItem
debug,test;
MenuFrame(String
title){
super(title);
//
create menu bar and addd it to frame
MenuBar
mbar = new MenuBar();
setMenuBar(mbar);
//create
the menu items
Menu
file = new Menu("File");
MenuItem
item1,item2,item3,item4,item5;
file.add(item1=new
MenuItem("New"));
file.add(item2=new
MenuItem("Open"));
file.add(item3=new
MenuItem("Close"));
file.add(item4=new
MenuItem("-"));
file.add(item5=new
MenuItem("Quit"));
mbar.add(file);
Menu
edit = new Menu("Edit");
MenuItem
item6,item7,item8,item9;
edit.add(item6=
new MenuItem("Cut"));
edit.add(item7=
new MenuItem("Copy"));
edit.add(item8=
new MenuItem("Paste"));
edit.add(item9=
new MenuItem("-"));
Menu
sub = new Menu("Special");
MenuItem
item10,item11,item12;
sub.add(item10
= new MenuItem("First"));
sub.add(item11
= new MenuItem("Second"));
sub.add(item12
= new MenuItem("Third"));
edit.add(sub);
//these
are checkable Menu items
debug
= new CheckboxMenuItem("Debug");
edit.add(debug);
test
= new CheckboxMenuItem("Testing");
edit.add(test);
mbar.add(edit);
//create
an object to handle action and item events
MyMenuHandler
handler = new MyMenuHandler(this);
//register it to receive those events
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addItemListener(handler);
test.addItemListener(handler);
//create
an object to handle window events
MyWindowAdapter
adapter = new MyWindowAdapter(this);
//register
it to receive those events
addWindowListener(adapter);
}
public
void paint(Graphics g){
g.drawString(msg,10,220);
if(debug.getState())
g.drawString("Debug
is on.",10,220);
else
g.drawString("Debug
is off.",10,220);
if(test.getState())
g.drawString("Testing
is on.",10,240);
else
g.drawString("Testing
is off.",10,240);
}
}
File name: MyWindowAdapter.java
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
class
MyWindowAdapter extends WindowAdapter{
MenuFrame
menuFrame;
public
MyWindowAdapter(MenuFrame menuFrame){
this.menuFrame=menuFrame;
}
public
void windowClosing(WindowEvent we){
menuFrame.setVisible(false);
}
}
File name: MyMenuHandler.java
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
class
MyMenuHandler implements ActionListener, ItemListener{
MenuFrame
menuFrame;
public MyMenuHandler(MenuFrame menuFrame){
this.menuFrame = menuFrame;
}
// Handle action events
public void actionPerformed(ActionEvent ae){
String
msg = "You selected";
String
arg= (String)ae.getActionCommand();
if(arg.equals("New"))
msg+="New.";
else
if(arg.equals("Open"))
msg+="open.";
else
if(arg.equals("Close"))
msg+="Close.";
else
if(arg.equals("Quit"))
msg+="Quit.";
else
if(arg.equals("Edit"))
msg+="Edit.";
else
if(arg.equals("Cut"))
msg+="Cut.";
else
if(arg.equals("Copy"))
msg+="Copy.";
else
if(arg.equals("Paste"))
msg+="Paste.";
else
if(arg.equals("First"))
msg+="First.";
else
if(arg.equals("Second"))
msg+="Second.";
else
if(arg.equals("Third"))
msg+="Third.";
else
if(arg.equals("Debug"))
msg+="Debug.";
else
if(arg.equals("Testing"))
msg+="Testing.";
menuFrame.msg
= msg;
menuFrame.repaint();
}
// Handle item events
public
void itemStateChanged(ItemEvent ie){
menuFrame.repaint();
}
}
File name :
MenuDemo.java
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*
<applet
code = "MenuDemo" width = 250 height=250>
</applet>
*/
public
class MenuDemo extends Applet{
Frame
f;
public
void init(){
f=new
MenuFrame("Menu Demo");
int
width=Integer.parseInt(getParameter("width"));
int
height=Integer.parseInt(getParameter("height"));
setSize(new
Dimension(width,height));
f.setSize(width,height);
f.setVisible(true);
}
public
void start(){
f.setVisible(true);
}
public
void stop(){
f.setVisible(false);
}
}
Output
Dialog
Boxes
File name: MessageBox.java
import
java.awt.*;
import
java.awt.event.*;
class
MessageBox extends Dialog{
MessageBox(Frame
fm,String lab){
super(fm,"Message",true);
setLayout(new
GridLayout(2,1,0,0));
Panel p1=new
Panel();
Panel p2=new
Panel();
Button b1,b2;
p1.setFont(new
Font("TimesRoman",Font.BOLD,18));
p1.setLayout(new
FlowLayout(FlowLayout.CENTER,20,15));
p2.setLayout(new
FlowLayout(FlowLayout.CENTER,20,20));
p1.add(new
Label(lab));
b1=new
Button("ok");
b1.addActionListener(new
B1());
p2.add(b1);
b2=new
Button("cancel");
b2.addActionListener(new
B1());
p1.add(b2);
add(p1);
add(p2);
setSize(350,125);
setTitle("Message
Box");
addWindowListener(new
WindowAdapter(){
public void
windowClosing(WindowEvent w){
System.exit(0);
}
});
}
}
File name: B1.java
import
java.awt.*;
import
java.awt.event.*;
class B1
implements ActionListener{
public void
actionPerformed(ActionEvent e){
try{
Button
ok=(Button)e.getSource();
String
s=ok.getLabel();
if(s.equals("ok")||s.equals("cancel")){
//dispose();
System.exit(0);
}
}
catch(Exception
n){}
}
}
File name:
MessageApplication.java
import
java.awt.*;
import
java.awt.event.*;
public class
MessageApplication extends Frame{
boolean a;
MessageApplication(){
MessageBox
mb=new MessageBox(this,"JAVA Alert: This is a Message Box");
mb.setLocation(200,200);
mb.setVisible(true);
a=false;
}
public static
void main(String args[]){
MessageApplication
fm=new MessageApplication();
System.out.println("Popping
out Message Box");
fm.setVisible(true);
}
}
Output
File
Dialogs
File name: FileDialogDemo.java
import
java.awt.*;
import
java.awt.event.*;
/*
<applet
code="FileDialogDemo" width=400 height=400>
</applet>
*/
class
FileDialogDemo extends Frame implements ActionListener
{
Frame t;
Button b1;
Label l1;
FileDialogDemo()
{
t=new
Frame("Sample Frame");
t.setLayout(new
FlowLayout());
b1=new
Button("Open");
l1=new
Label("File selected:");
t.add(b1);
t.add(l1);
b1.addActionListener(this);
t.setSize(300,300);
t.setVisible(true);
}
public static
void main(String args[])
{
FileDialogDemo
fd=new FileDialogDemo();
}
public void
actionPerformed(ActionEvent a){
FileDialog
fd=new FileDialog(t,"File Dialog");
fd.setVisible(true);
l1.setText(l1.getText()+fd.getFile());
}
}
Output
0 comments:
Post a Comment