GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice...

38
GUI Elements Session 17

Transcript of GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice...

Page 1: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

GUI ElementsGUI Elements

Session 17

Page 2: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Memory UploadMemory Upload

• Layout• Components

Button TextField TextArea Label Choice

• Containers Panels The applet itself

Page 3: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Applet MethodsApplet Methods

• init

• start

• end

• destroy

Page 4: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Components / ContainersComponents / Containers

container

Component

Page 5: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

LayoutLayout

• The basic setup for any container FlowLayout BorderLayout GridLayout

Page 6: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

FlowLayoutFlowLayout

• Elements Added From left to right From top to bottom

• Alignment Center

Page 7: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

FlowLayoutFlowLayout

• Create a FlowLayout object.

• The constructor

• Set the Layout

FlowLayout fl = new FlowLayout( );

setLayout(fl);//orContainer.setLayout(fl);

this

Page 8: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ButtonButton

• The constructor

• The parameter a String –the button caption

Button b1 = new Button(“OK”);Button b2 = new Button(“ OK ”);

Page 9: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ButtonButton

import java.applet.*; import java.awt.*; public class FlowButtons extends Applet{ public void init( ) { FlowLayout fl = new FlowLayout( ); setLayout(fl); Button b1 = new Button("OK"); add(b1); } }

Page 10: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ButtonButton

import java.applet.*; import java.awt.*; public class FlowButtons extends

Applet{ public void init( ) { Button b1 = new Button("OK"); add(b1); } }

Page 11: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

FlowLayout with ButtonFlowLayout with Button

OK

Page 12: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

BorderLayoutBorderLayout

• Elements Added In the assigned area

• Areas North South East West Center

Page 13: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

BorderLayoutBorderLayout

• One Component/Container per spot

Page 14: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Buttons BorderLayoutButtons BorderLayout

BorderLayout bl = new BorderLayout( );this.setLayout(bl); Button bN = new Button(“north");Button bS = new Button(“south");Button bC = new Button(“center”);Button bE = new Button(“east");Button bW = new Button(“west");this.add(“Center”, bC);this.add(“North”, bN);this.add(“South”, bS);this.add(“East”, bE);this.add(“West”, bW);

Page 15: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Buttons BorderLayoutButtons BorderLayout

• One Component/Container per spot

north

south

west eastcenter

Page 16: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

GridLayoutGridLayout

• Elements Added From left to right From top to bottom

• Parameters Row Column

Page 17: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Buttons GridLayoutButtons GridLayout

setLayout(new GridLayout(2,2));

Button b1 = new Button("1");Button b2 = new Button("2"); Button b3 = new Button("3"); Button b4 = new Button("4"); add(b1); add(b2); add(b3); add(b4);

this.

this.this.this.this.

Page 18: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Buttons GridLayoutButtons GridLayout

1 2

3 4

Page 19: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

TextFieldTextField

• For user data entry or program information display

• Two constructors:

• They are placed just as any other component/container

TextField tf = new TextField(String);TextField tf = new TextField(int);

Page 20: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

TextAreaTextArea

• For user data entry or program information display

• Two constructors:

• They are placed just as any other component/container

TextArea ta= new TextArea(String);TextArea ta = new TextArea(int, int);

Page 21: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

LabelLabel

• To label GUI elements

• One constructor:

• They are placed like any other component/ container

Label lab = new Label(String);

Page 22: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ChoiceChoice

• Give users controlled choices

• One constructor:

• After creating the Choice, items must be added.

Choice ch = new Choice( );

ch.add(String);

Page 23: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ChoiceChoice

• Java is zero based• Items are added in order• They are placed like any other

component / container

Choice ch = new Choice( );ch.add(“zero”);ch.add(“one”);ch.add(“two”);

zero

Page 24: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

ChoiceChoice

• Java is zero based• Items are added in order• They are placed like any other

component / container

Choice ch = new Choice( );ch.add(“zero”);ch.add(“one”);ch.add(“two”);

zeroonetwo

Page 25: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

• The Panel is a container

• The constructor

• Each Panel can have its own Layout

Panel p = new Panel( );

BorderLayout bl = new BorderLayout( );p.setLayout(bl);

Page 26: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

• There can be many Panels inside an applet

• Panels can be nested

• Follow the rule: One Component/Container per spot

Page 27: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

The applet

BorderLayout bl = new BorderLayout( );this.setLayout(bl);

Page 28: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

Panel pTop = new Panel( );GridLayout gl = new GridLayout(5,1);pTop.setLayout(gl);this.add(“North”, pTop);

Page 29: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

OK NOICS 111

Button b1 = new Button (“OK”);Label num = new Label (“111”);Label area = new Label(“ICS”);Label blank = new Label(“ ”);Button b2 = new Button(“NO”);pTop.add(blank);pTop.add(area);pTop.add(num);pTop.add(b1);pTop.add(b2);

Page 30: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

OK NOICS 111

Panel pCenter = new Panel( );BorderLayout blCtr = new BorderLayout( );this.add(“Center”, pCenter);

Page 31: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

PanelPanel

• You can have as many panels as you want inside one another

• You can have a different layout on each panel

Page 32: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

GUIGUI

• Plan the looks before you code

Page 33: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Applet posting reviewApplet posting review

• Steps Plan your GUI Write your java code in the

public_html directory Compile your code Change the class mode to 644

Page 34: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Applet posting reviewApplet posting review

• Steps Write your html page Change the html page mode to 644 Visit your applet on the web Use the java console for immediate

feedback!

Page 35: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Time to Try it OutTime to Try it Out

AppletsAndGUI

Page 36: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

• Components / Containers

• Different Components

• Panels

• Nesting Panels

• Posting and Viewing Applets

• E-mail any questions to [email protected]

Memory DefragmenterMemory Defragmenter

Page 37: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Task ManagerTask Manager

• Answer 4 questions on WebCT

• Read your e-mail

• Visit WebCT, webct.hawaii.edu

Page 38: GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.

Task ManagerTask Manager

• Answer 4 questions on WebCT

• Read your e-mail

• Visit WebCT, webct.hawaii.edu