Components - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Components

Description:

Components are just graphical 'widgets' used to build GUIs (Graphical ... public class Xmas extends java.applet.Applet { Button b; TextField t; public void init ... – PowerPoint PPT presentation

Number of Views:242
Avg rating:3.0/5.0
Slides: 16
Provided by: jeffch8
Category:
Tags: components | xmas

less

Transcript and Presenter's Notes

Title: Components


1
Components
  • A Light Overview of the A.W.T.

2
Overview
  • Definition of components
  • A look at two common components
  • Buttons
  • Textfields
  • Methods of all components

3
What is a Component
  • The A.W.T. (Abstract Windowing Toolkit) is where
    graphical things live
  • Buttons
  • TextFields
  • TextAreas
  • Graphics
  • Scrollbars
  • Checkboxes

4
What is a Component
  • The A.W.T. (Abstract Windowing Toolkit) is where
    graphical things live
  • Buttons
  • TextFields
  • TextAreas
  • Graphics
  • Scrollbars
  • Checkboxes


All of these things are components, and inherit
from class Component
5
SO
  • Components are just graphical widgets used to
    build GUIs (Graphical User Interfaces)
  • Most of the things in windows are components
  • If you know one, you know them all
  • Well focus on two common ones
  • Button
  • TextField
  • Components have tons of accessors and modifier
    method (for querying)

6
Getting Components into Your Applet
  • Steps
  • Declare the component as an attribute of the
    applet
  • Bring the component to life (using new)
  • add( ) the component
  • // Note 2 and 3 are usually done in init ( )

7
The Button
  • There is a Button class
  • Push Buttons for some kind of action to occur
  • Its constructor expects a String (a label for
    the Button)
  • Example
  • Button b
  • b new Button (Push me and DIE)
  • Size of Button depends on the text on it (and
    some other things)

8
TextFields
  • There is a TextField class
  • Used to type in a single line of text
  • Constructor expects an int representing the
    number of characters
  • Example
  • TextField t
  • t new TextField (30)
  • Important methods
  • getText ( )
  • setText ( )

9
Example
  • import java.awt.
  • public class Worthless extends java.applet.Applet
  • Button b
  • TextField t
  • public void init ( )
  • b new Button (Press me and DIE)
  • t new TextField (30)

10
Bad Example!
  • import java.awt.
  • public class Worthless extends java.applet.Applet
  • Button b
  • TextField t
  • public void init ( )
  • b new Button (Press me and DIE)
  • t new TextField (30)
  • // The user wont see the button or textfield!
  • // forgot to add the components!

11
Adding the Components
  • import java.awt.
  • public class RockOn extends java.applet.Applet
  • Button b
  • Textfield t
  • public void init ( )
  • b new Button (Press me and DIE)
  • t new Textfield (30)
  • add ( b )
  • add ( t )

12
Methods all Components Have
  • setBackground ( Color c )
  • setForeground ( Color c )
  • setEnabled ( boolean b )
  • setVisible ( boolean b )
  • // Check the API for Component to see other
  • // functionality

13
Example
  • import java.awt.
  • public class Xmas extends java.applet.Applet
  • Button b
  • TextField t
  • public void init ( )
  • b new Button (Press)
  • t new TextField (30)
  • b.setBackground (Color.green)
  • b.setForeground (Color.red) // red text label
  • t.setBackground (Color.blue)
  • add ( b )
  • add ( t )

14
Output
15
Summary
  • Components are widgets
  • Including components into applets
  • Declare the component
  • Bring component to life
  • Add the component
  • Components have accessors and modifiers to get
    their state (later)
  • You should now explore other components
Write a Comment
User Comments (0)
About PowerShow.com