Window Fundamentals 2 Component encapsulates all of the attributes of a visual component. All user...

75

Transcript of Window Fundamentals 2 Component encapsulates all of the attributes of a visual component. All user...

Page 1: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.
Page 2: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

2

Page 3: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

Component encapsulates all of the attributes of a visual component.

All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component.

A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.

Page 4: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

Container A container is responsible for laying out (that is,

positioning) any components that it contains. It does this through the use of various layout

managers

Page 5: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

Panel A Panel may be thought of as a recursively

nestable, concrete screen component. Panel is the superclass for Applet. When screen

output is directed to an applet, it is drawn on the surface of a Panel object.

In essence, a Panel is a window that does not contain a title bar, menu bar, or border.

Page 6: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

Window The Window class creates a top-level window Generally, Window objects are not created

directly. Instead, a subclass of Window called Frame.

Page 7: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Window Fundamentals

Frame Frame is a subclass of Window It has a title bar, menu bar, borders, and resizing

corners. If you create a Frame object from within an

applet, it will contain a warning message, such as “Java Applet Window,” to the user that an applet window has been created.

This message warns users that the window was started by an applet.

Page 8: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Frames

1. Frame( )

2. Frame(String title)

void setSize(int newWidth, int newHeight) To set the dimensions of the

window

void setVisible(boolean visibleFlag) void setVisible(boolean visibleFlag)

void setTitle(String newTitle) Setting a Window’s Title

Page 9: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Frames

import java.awt.*;

import java.awt.event.*;

class MyFrame extends Frame

{

MyFrame(String s)

{

super(s)

}

public void paint(Graphics g)

{

g.drawString("MCA Department",100,50);

}

Page 10: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Frames

public static void main(String args[])

{

MyFrame f = new MyFrame(“VESIT”);

f.setSize(200,100);

f.setVisible(true);

}

}

Page 11: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Frames// Create a child frame window from within an applet.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="AppletFrame" width=300 height=50>

</applet>

*/

class MyFrame extends Frame

{

MyFrame()

{

addWindowListener(new MyWindowAdapter(this));

}

Page 12: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Framespublic void paint(Graphics g)

{ g.drawString("Hello world",100,50);

}

}

class MyWindowAdapter extends WindowAdapter {

MyFrame f1;

public MyWindowAdapter(MyFrame f1)

{

this.f1 = f1;

}

public void windowClosing(WindowEvent we)

{

f1.setVisible(false);

}

}

Page 13: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Framespublic class AppletFrame1 extends Applet {

MyFrame f;

public void init() {

f = new MyFrame();

f.setSize(250, 250);

f.setVisible(true);

}

public void start() {

f.setVisible(true);

}

public void stop() {

f.setVisible(false);

}

public void paint(Graphics g) {

g.drawString("This is in applet window", 10, 20);

}

Page 14: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Frames

Page 15: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Color classColor(int red, int green, int blue)

1.  void setColor(Color newColor)

2.  Color getColor( )

import java.awt.*;

import java.applet.*;

/* <applet code=“ColorDemo" width=300 height=200>

</applet> */

public class ColorDemo extends Applet

{

public void paint(Graphics g) {

g.setColor(Color.green);

g.drawString(“Hello World”, 50,50);

}

}

Page 16: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Graphics Class

All graphics are drawn relative to a window. This can be the main window of an applet, a child

window of an applet, or a stand-alone application window.

The origin of each window is at the top-left corner and is 0,0.

Coordinates are specified in pixels.

Page 17: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

17

Graphics Primitives For drawing geometric shapes, texts An abstract class

– the extended class must override paint()

Oval

Rectangle

Arc

Line

RoundRectangle

Polygon

Page 18: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

18

Drawing Lines

public void paint(Graphics g){ g.setColor(Color.blue); g.drawLine(0, 0, 100, 100); g.drawLine(0, 100, 100, 0); }

(x1,y1)

(x2,y2)

void drawLine(int startX, int startY, int endX, int endY)

Page 19: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Drawing Rectangles

public void paint(Graphics g) { g.setColor(Color.pink);

g.drawRect(10, 10, 60, 50); g.fillRect(100, 10, 60, 50);}

void drawRect(int top, int left, int width, int height)

void fillRect(int top, int left, int width, int height)

Page 20: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Drawing Rounded Rectangles

void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)

void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)

xDiam: diameter of the rounding arc along the X axis

yDiam: diameter of the rounding arc along the Y axis

public void paint(Graphics g) { g.setColor(Color.yellow);

g.drawRoundRect(190, 10, 60, 50, 15, 15); g.fillRoundRect(70, 90, 140, 100, 30, 40);}

Page 21: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

21

Drawing Ellipses and Circles

public void paint(Graphics g) {

g.setColor(Color.blue);

g.drawOval(10, 10, 75, 50);

g.fillOval(100, 10, 50, 50);

}

void drawOval(int top, int left, int width, int height)

void fillOval(int top, int left, int width, int height)

• ellipse is drawn within a bounding rectangle specified by top,left , width

and height.

•To draw a circle, specify a square as the bounding rectangle

Page 22: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

 Drawing Arcs1. void drawArc(int top, int left, int width, int height,

int startAngle, int sweepAngle)

1. void fillArc(int top, int left, int width, int height,

int startAngle, int sweepAngle)

arc is bounded by the rectangle specified by top, left , width

and height.

The arc is drawn from startAngle through the angular

distance

specified by sweepAngle.

Page 23: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

 Drawing Arcs

public void paint(Graphics g)

{

g.setColor(Color.orange);

g.drawArc(10, 40, 70, 70, 0, 75);

g.fillArc(100, 40, 70, 70, 0, 75);

}

Page 24: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Drawing Polygons

public void paint(Graphics g) { g.setColor(Color.green);

int xs[] = {161,161,185,209,185,161};

int ys[] = {310,334,358,334,310,310};

g.fillPolygon(xs,ys,6); }

void drawPolygon(int x[ ], int y[ ], int numPoints)

void fillPolygon(int x[ ], int y[ ], int numPoints)

Page 25: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Font class

Font(String fontName, int fontStyle, int pointSize)

 

fontStyl: Font.PLAIN, Font.BOLD, and Font.ITALIC.

To combine styles: Font.BOLD | Font.ITALIC

void setFont(Font fontObj)

Page 26: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Font class

public void paint(Graphics g)

{

g.setColor(Color.blue);

Font f = new Font(“TimesRoman”,Font.BOLD,20);

g.setFont(f);

g.drawString(“Hello World”, 50,50);

}

 

 

Page 27: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

27

Using AWT Components Component

CanvasScrollbarButtonCheckboxLabelListChoiceTextComponent

TextArea TextField

Component– Container

• Panel

• Window– Dialog

» FileDialog

– Frame

MenuComponent– MenuItem

• Menu

Page 28: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Using AWT Components

Component add(Component compObj)

void remove(Component obj)

Page 29: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Labels

Label( )

Label(String str)

void setText(String str)

String getText( )

Page 30: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Labels

public void init()

{

Label one = new Label("One");

Label two = new Label("Two");

Label three = new Label("Three");

add(one);

add(two);

add(three);

}

Page 31: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Buttons

Button( )

Button(String str)

void setLabel(String str)

String getLabel( )

31

Page 32: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

32

Buttonspublic void init()

{

Button yes = new Button("Yes");

Button no = new Button("No");

Button maybe = new Button("Undecided");

add(yes);

add(no);

add(maybe);

}

Page 33: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Buttons : Event Handlingimport java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="ButtonDemo" width=250 height=150>

</applet>

*/

public class ButtonDemo extends Applet implements ActionListener

{

String msg = "";

Button yes, no, maybe;

Page 34: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Buttons : Event Handling public void init()

{

yes = new Button("Yes");

no = new Button("No");

maybe = new Button("Undecided");

add(yes);

add(no);

add(maybe);

yes.addActionListener(this);

no.addActionListener(this);

maybe.addActionListener(this);

}

Page 35: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Buttons : Event Handling

public void actionPerformed(ActionEvent ae)

{

msg = ae.getActionCommand();

repaint();

}

public void paint(Graphics g)

{

g.drawString("Pressed: "+msg, 6, 100);

}

}

Page 36: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Buttons : Event Handling

Page 37: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Checkboxes Checkbox( )

Checkbox(String str)

Checkbox(String str, boolean on)

Checkbox(String str, CheckboxGroup cbGroup, boolean on)

boolean getState( )

void setState(boolean on)

String getLabel( )

void setLabel(String str)

Page 38: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Checkboxes: Event Handlingpublic class CheckboxDemo extends Applet implements ItemListener {

String msg = "";

Checkbox c, cpp, java;

public void init()

{

c = new Checkbox("C", null, true);

cpp = new Checkbox("C++");

java = new Checkbox("JAVA");

add(c);

add(cpp);

add(java);

Page 39: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Checkboxes: Event Handling

c.addItemListener(this);

cpp.addItemListener(this);

java.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

Page 40: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Checkboxes: Event Handling

public void paint(Graphics g)

{

msg = c.getLabel()+ " : " + c.getState();

g.drawString(msg, 6, 100);

msg = cpp.getLabel()+ " : " + cpp.getState();

g.drawString(msg, 6, 120);

msg = java.getLabel()+ " : " + java.getState();

g.drawString(msg, 6, 140);

}

}

Page 41: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Checkboxes: Event Handling

Page 42: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CheckboxGroup

Checkbox getSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which)

Page 43: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

43

CheckboxGrouppublic class CBGroup extends Frame { public CBGroup(){

CheckboxGroup cbg = new CheckboxGroup();Checkbox cb1 = new Checkbox(“C”, cbg,false);Checkbox cb2 = new Checkbox(“JAVA”, cbg, true);add(cb1);add(cb2);add(cb3);

}…}

Page 44: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

44

Choices

1. void add(String name) : To add a selection to the list

2. String getSelectedItem( )

3. int getSelectedIndex( )

Page 45: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

45

Choicespublic class ColorChoice extends Applet implements ItemListener { Choice c1; Color color;

public void init() { c1 = new Choice(); c1.addItem("Red"); c1.addItem("Green"); c1.addItem("Blue"); c1.addItemListener(this); add(c1); }

Page 46: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

46

Choices public void itemStateChanged(ItemEvent ie) { if(c1.getSelectedItem().equals("Red")) color = Color.red; else if(c1.getSelectedItem().equals("Green")) color = Color.green; else color = Color.blue; repaint(); } public void paint(Graphics g) { setBackground(color); }}

Page 47: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Lists List( )

List(int numRows)

List(int numRows, boolean multipleSelect)

void add(String name)

void add(String name, int index)

String getSelectedItem( )

int getSelectedIndex( )

Page 48: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Lists

public void init() {

List list = new List();

list.add("Hydrogen");

list.add("Helium");

list.add("Carbon");

list.add("Oxygen");

add(list);

}

Page 49: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

TextField

TextField( )

TextField(int numChars)

TextField(String str)

TextField(String str, int numChars)

String getSelectedText( )

Page 50: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

TextArea

TextArea( )

TextArea(int numLines, int numChars)

TextArea(String str)

TextArea(String str, int numLines, int numChars)

void append(String str)

Page 51: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

MenuBar, Menu, & MenuItem

Menu bar contains one or more Menu objects.

Each Menu object contains a list of MenuItem objects.

Each MenuItem object represents something that can be

selected by the user.

Page 52: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

MenuBar, Menu, & MenuItemMenuBar mb = new MenuBar();

setMenuBar(mb);

// Create menu A

Menu a = new Menu("A");

mb.add(a);

MenuItem a1 = new MenuItem("A1");

MenuItem a2 = new MenuItem("A2");

MenuItem a3 = new MenuItem("A3");

a.add(a1);

a.add(a2);

a.add(a3);

Page 53: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

MenuBar, Menu, & MenuItem// Create menu B

Menu b = new Menu("B");

mb.add(b);

MenuItem b1 = new MenuItem("B1");

MenuItem b2 = new MenuItem("B2");

b.add(b1);

b.add(b2);

Page 54: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

MenuBar, Menu, & MenuItem

// Create sub-menu for B3

Menu b3 = new Menu("B3");

b.add(b3);

MenuItem b31 = new MenuItem("B31");

MenuItem b32 = new MenuItem("B32");

b3.add(b31);

b3.add(b32);

Page 55: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

55

Arranging components

Every Container has a layout manager

The default layout for a Panel is FlowLayout

An Applet is a Panel therefore, the default layout for a Applet

is FlowLayout

The layout manager is set by the setLayout( ) method.

void setLayout(LayoutManager layoutObj)

Page 56: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

Layout of Components

FlowLayout– left to right & top

down

BorderLayout – north, south, west, east

& center

CardLayout– stack of panels

GridLayout– tabular form

– rows & columns

Page 57: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

57

FlowLayout

Use add(component); to add to a component when using

a FlowLayout

Components are added left-to-right

If no room, a new row is started

FlowLayout is convenient but often ugly

Page 58: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

58

FlowLayout1. FlowLayout( )

the default layout, which centers components and leaves five pixels of space between each component.

2. FlowLayout(int how) specify how each line is aligned. Valid values for how are as follows:

1. FlowLayout.LEFT

2. FlowLayout.CENTER

3. FlowLayout.RIGHT

3. FlowLayout(int how, int horz, int vert) horz and vert the horizontal and vertical space left between

components

Page 59: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

59

FlowLayout

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

public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); }}

Page 60: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

BorderLayout BorderLayout( ) BorderLayout(int horz, int vert)

BorderLayout defines the following constants that specify the regions:

1. BorderLayout.CENTER

2. BorderLayout.SOUTH

3. BorderLayout.EAST

4. BorderLayout.WEST

5. BorderLayout.NORTH

Page 61: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

61

BorderLayout At most five components can be added

void add(Component compObj, Object region)

If you want more components, add a Panel, then add components to it.

add (new Button("NORTH"), BorderLayout.NORTH);

Page 62: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

62

BorderLayout

public void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER);}

Page 63: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

63

BorderLayoutimport java.awt.*;import java.applet.*;

public class BorderLayoutExample extends Applet { public void init () { setLayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); }}

Page 64: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

64

Using a Panel

Panel p = new Panel(); add (p, BorderLayout.SOUTH); p.add (new Button ("Button 1")); p.add (new Button ("Button 2"));

Page 65: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

65

GridLayout

The GridLayout manager divides the container up into a

given number of rows and columns:

GridLayout(int rows, int columns)

All sections of the grid are equally sized and as large as

possible

Page 66: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

66

GridLayoutimport java.awt.*;

import java.applet.*;

public class GridLayoutExample extends Applet {

public void init () {

setLayout(new GridLayout(2, 3));

add(new Button("One"));

add(new Button("Two"));

add(new Button("Three"));

add(new Button("Four"));

add(new Button("Five"));

}

}

Page 67: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout

The CardLayout class can store several different layouts.

Each layout can be thought of as being on a separate index card

in a deck that can be shuffled so that any card is on top at a given

time.

Useful for user interfaces that can be dynamically enabled and

disabled upon user input.

User can keep other layouts hidden, ready to be activated when

needed.

Page 68: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout

CardLayout( )

CardLayout(int horz, int vert)

Page 69: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout : How to Use?

The cards are typically held in an object of type Panel.

The cards that form the deck are also typically objects of

type Panel.

This panel must have CardLayout selected as its layout

manager.

Thus, Create a panel that contains the deck and a panel for

each card in the deck.

Page 70: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout : How to Use?

Next, add components to the appropriate panel toform each card.

Add these panels to the Deck panel for which CardLayout is the

layout manager.

Finally, add Deck panel to the window.

Provide some way for the user to select between cards, common

approach is to include one push button for each card in the deck.

Page 71: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout: Example

Page 72: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout: Examplepublic class CardLayoutDemo extends Applet

implements ActionListener {

Checkbox Win98, winNT, solaris, mac;

Panel osCards;

CardLayout cardLO;

Button Win, Other;

public void init() {

Win = new Button("Windows");

Other = new Button("Other");

add(Win);

add(Other);

Page 73: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout: Example

cardLO = new CardLayout();

osCards = new Panel();

osCards.setLayout(cardLO); // set Deck panel layout to card layout

Win98 = new Checkbox("Windows 98", null, true);

winNT = new Checkbox("Windows NT/2000");

solaris = new Checkbox("Solaris");

mac = new Checkbox("MacOS");

// add Windows check boxes to a card panel

Panel winPan = new Panel();

winPan.add(Win98);

winPan.add(winNT);

Page 74: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout: Example // Add other OS check boxes to a card panel

Panel otherPan = new Panel();

otherPan.add(solaris);

otherPan.add(mac);

// add panels to card deck panel

osCards.add(winPan, "Windows");

osCards.add(otherPan, "Other");

// add cards to main applet panel

add(osCards);

// register to receive action events

Win.addActionListener(this);

Other.addActionListener(this);

}

Page 75: Window Fundamentals 2  Component encapsulates all of the attributes of a visual component.  All user interface elements that are displayed on the screen.

CardLayout: Example

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == Win)

cardLO.show(osCards, "Windows");

else

cardLO.show(osCards, "Other");

}

}