Top Banner

of 22

Ch 15 Aw t Components

Jun 03, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/12/2019 Ch 15 Aw t Components

    1/22

    Objectives

    The objectives of this chapter are:To discuss the classes present in the java.awt package

    To understand the inheritance hierarchy of the AWT

    To outline the basic structure of GUIs

    To show how to add components to containersTo understand how to use Layout Managers

    To understand basic graphics processing under the AWT

  • 8/12/2019 Ch 15 Aw t Components

    2/22

    AWT (Abstract Windowing Toolkit)

    The AWT is roughly broken into three categories

    Components

    Layout Managers

    Graphics

    Many AWT components have been replaced by Swingcomponents

    It is generally not considered a good idea to mix Swingcomponents and AWT components. Choose to use

    one or the other.

  • 8/12/2019 Ch 15 Aw t Components

    3/22

    AWT Class Hierarchy

    PanelButton

    Checkbox

    Choice

    Label

    List

    Component

    Container FrameWindow

    TextArea

    TextFieldTextComponent

    Note: There are more classes, howeer,

    these are what are coered in this chapter

  • 8/12/2019 Ch 15 Aw t Components

    4/22

    Component

    Component is the superclass of most of the displayable

    classes defined within the AWT. Note: it is abstract.MenuComponent is another class which is similar toComponent except it is the superclass for all GUI itemswhich can be displayed within a drop-down menu.

    The Component class defines data and methods whichare relevant to all Components

    setBoundssetSizesetLocation

    setFontsetEnabledsetVisiblesetForeground -- coloursetBackground -- colour

  • 8/12/2019 Ch 15 Aw t Components

    5/22

    Container

    Container is a subclass of Component. (ie. All containers

    are themselves, Components)Containers contain components

    For a component to be placed on the screen, it must beplaced within a Container

    The Container class defined all the data and methodsnecessary for managing groups of ComponentsaddgetComponentgetMaximumSize

    getMinimumSizegetPreferredSizeremoveremoveAll

  • 8/12/2019 Ch 15 Aw t Components

    6/22

    Windows and Frames

    The Window class defines a top-level Window with no

    Borders or Menu bar.Usually used for application splash screens

    ! Frame defines a top-level Window with Borders and aMenu Bar

    ! Frames are more commonly used than WindowsOnce defined, a Frame is a Container which can containComponents

    Frame aFrame = new Frame(Hello World);aFrame.setSize(100,100);aFrame.setLocation(10,10);aFrame.setVisible(true);

  • 8/12/2019 Ch 15 Aw t Components

    7/22

    anels

    When writing a GUI application, the GUI portion can

    become quite complex.To manage the complexity, GUIs are broken down intogroups of components. Each group generally provides aunit of functionality.

    A Panel is a rectangular Container whose sole purpose is

    to hold and manage components within a GUI.

    anel aanel = new anel();

    aanel.add(new !utton("#$"));

    aanel.add(new !utton("%ancel"));

    Frame aFrame = new Frame("!utton &est");

    aFrame.setSize(100,100);

    aFrame.setLocation(10,10);

    aFrame.add(aanel);

  • 8/12/2019 Ch 15 Aw t Components

    8/22

    !"ttons

    This class represents a push-button which displays some

    specified text.When a button is pressed, it notifies its Listeners. (Moreabout Listeners in the next chapter).

    To be a Listener for a button, an object must implement theActionListener Interface.

    anel aanel = new anel();

    !utton o$!utton = new !utton("#$");

    !utton cancel!utton = new !utton("%ancel");

    aanel.add(o$!utton));aanel.add(cancel!utton));

    o$!utton.add'ctionListener(controller);

    cancel!utton.add'ctionListener(controller1);

  • 8/12/2019 Ch 15 Aw t Components

    9/22

    #abels

    This class is a Component which displays a single line of

    text.Labels are read-only. That is, the user cannot click on alabel to edit the text it displays.

    Text can be aligned within the label

    Label aLabel = new Label("nter *assword+");

    aLabel.set'linment(Label.-/H&);

    aanel.add(aLabel);

  • 8/12/2019 Ch 15 Aw t Components

    10/22

    #ist

    This class is a Component which displays a list of Strings.

    The list is scrollable, if necessary.

    Sometimes called Listbox in other languages.

    Lists can be set up to allow single or multiple selections.

    The list will return an array indicating which Strings are

    selectedList aList = new List();aList.add("%alar");aList.add("dmonton");aList.add("-eina");aList.add("Vancouer");

    aList.set2ulti*le2ode(true);

  • 8/12/2019 Ch 15 Aw t Components

    11/22

    Checkbo$

    This class represents a GUI checkbox with a textual label.

    The Checkbox maintains a boolean state indicating whetherit is checked or not.

    If a Checkbox is added to a CheckBoxGroup, it will behavelike a radio button.

    %3ec$bo4 cream%3ec$bo4 = new %3ec$!o4("%ream");

    %3ec$bo4 suar%3ec$bo4 = new %3ec$!o4("Suar");

    56

    i7 (cream%3ec$bo4.etState())

    8

    co77ee.add%ream();

    9

  • 8/12/2019 Ch 15 Aw t Components

    12/22

    Choice

    This class represents a dropdown list of Strings.

    Similar to a list in terms of functionality, but displayeddifferently.

    Only one item from the list can be selected at one time andthe currently selected element is displayed.

    %3oice a%3oice = new %3oice();

    a%3oice.add("%alar");

    a%3oice.add("dmonton");

    a%3oice.add("'lert !a");

    56

    Strin selected:estination= a%3oice.etSelectedtem();

  • 8/12/2019 Ch 15 Aw t Components

    13/22

    Te$tField

    This class displays a single line of optionally editable text.

    This class inherits several methods from TextComponent.This is one of the most commonly used Components in theAWT

    TextField emailTextField = new TextField();

    TextField passwordTextField = new TextField();

    passwordTextField.setEchoChar("*");

    []

    String userEmail = emailTextField.getText();

    String userpassword = passwordTextField.getText();

  • 8/12/2019 Ch 15 Aw t Components

    14/22

    Te$tArea

    This class displays multiple lines of optionally editable text.

    This class inherits several methods from TextComponent.TextArea also provides the methods: appendText(),insertText() and replaceText()

    < rows, 0 columns

    &e4t'rea 7ull'ddress&e4t'rea = new &e4t'rea(

  • 8/12/2019 Ch 15 Aw t Components

    15/22

    #ayo"t %anagers

    Since the Component class defines the setSize() and

    setLocation() methods, all Components can be sized andpositioned with those methods.

    Problem: the parameters provided to those methods aredefined in terms of pixels. Pixel sizes may be different

    (depending on the platform) so the use of those methodstends to produce GUIs which will not display properly onall platforms.

    Solution: Layout Managers. Layout managers are

    assigned to Containers. When a Component is added toa Container, its Layout Manager is consulted in order todetermine the size and placement of the Component.

    NOTE: If you use a Layout Manager, you can no longerchange the size and location of a Component through the

    setSize and setLocation methods.

  • 8/12/2019 Ch 15 Aw t Components

    16/22

    #ayo"t %anagers (cont)

    There are several different LayoutManagers, each of whichsizes and positions its Components based on an algorithm:

    FlowLayout

    BorderLayout

    GridLayout

    For Windows and Frames, the default LayoutManager isBorderLayout. For Panels, the default LayoutManager isFlowLayout.

  • 8/12/2019 Ch 15 Aw t Components

    17/22

    Flow #ayo"t

    The algorithm used by the FlowLayout is to lay outComponents like words on a page: Left to right, top tobottom.

    It fits as many Components into a given row before movingto the next row.

    anel aanel = new anel();

    aanel.add(new !utton("#$"));

    aanel.add(new !utton("'dd"));

    aanel.add(new !utton(":elete"));aanel.add(new !utton("%ancel"));

  • 8/12/2019 Ch 15 Aw t Components

    18/22

    !order #ayo"t

    The BorderLayout Manager breaks the Container up into 5regions (North, South, East, West, and Center).

    When Components are added, their region is alsospecified:

    Frame aFrame = new Frame();

    aFrame.add(">ort3", new !utton("#$"));

    aFrame.add("Sout3", new !utton("'dd"));

    aFrame.add("ast", new !utton(":elete"));

    aFrame.add("West", new !utton("%ancel"));

    aFrame.add("%enter", new !utton("-ecalculate"));

  • 8/12/2019 Ch 15 Aw t Components

    19/22

    !order #ayo"t (cont)

    The regions of the BorderLayout are defined as follows:

    %enter

    >ort3

    Sout3

    West ast

  • 8/12/2019 Ch 15 Aw t Components

    20/22

    &rid #ayo"t

    The GridLayout class divides the region into a grid ofequally sized rows and columns.

    Components are added left-to-right, top-to-bottom.

    The number of rows and columns is specified in the

    constructor for the LayoutManager.

    anel aanel = new anel();

    /ridLaout t3eLaout = new /ridLaout(,);

    aanel.setLaout(t3eLaout);

    aanel.add(new !utton("#$"));

    aanel.add(new !utton("'dd"));

    aanel.add(new !utton(":elete"));

    aanel.add(new !utton("%ancel"));

  • 8/12/2019 Ch 15 Aw t Components

    21/22

    What i' dont want a #ayo"t%anager

    Ch. VIII - 21

    LayoutManagers have proved to be difficult and frustratingto deal with.

    The LayoutManager can be removed from a Container byinvoking its setLayout method with a null parameter.

    anel aanel = new anel();

    aanel.setLaout(null);

  • 8/12/2019 Ch 15 Aw t Components

    22/22