Chapter 3: Widgets for a GUI - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 3: Widgets for a GUI

Description:

Graphical User Interfaces composed of widgets (buttons, text ... box erased, repainted), useful to avoid flicker, should not be called by user (overridden) ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 23
Provided by: vinod6
Learn more at: https://www.d.umn.edu
Category:
Tags: gui | chapter | flicker | widgets

less

Transcript and Presenter's Notes

Title: Chapter 3: Widgets for a GUI


1
Chapter 3 Widgets for a GUI
  • General Component methods
  • Useful widgets classes
  • Text classes
  • Label
  • TextField
  • TextArea
  • Active widgets
  • Button
  • Checkbox
  • Choice
  • List

2
Building a GUI
  • Graphical User Interfaces composed of widgets
    (buttons, text fields, etc.)
  • Various useful classes defined in AWT
  • Part of Component class (abstract class)

3
  • Component methods (inherited by all)
  • Point location() - x,y position
  • Dimension size() - width,height of object
  • Rectangle bounds() - both location, size info
  • void move (int x, int y) - set x,y position
  • void resize (int w, int h) - set w,h of object
  • void resize (Dimension d) - similar
  • void reshape (int x, int y, int w, int h) - set
    rectangle
  • Dimension minimumSize() - determined by system,
    smallest size needed
  • Dimension preferredSize() - similar to minimum

4
  • Component methods (cont)
  • Color getBackground() - return background color
  • Color getForeground() - return foreground color
  • void setBackground(Color c) - set background
    color
  • void setForeground(Color c) - set foreground
    color
  • Font getFont() - return current font
  • void setFont(Font f) - set current font
  • boolean isShowing() - true if Component on screen
  • boolean isVisible() - true if object should be
    visible
  • void hide() - turns component invisible
  • void show() - makes component visible

5
  • Component methods (cont)
  • Graphics getGraphics() - returns the Graphics
    object associated with the current component,
    useful for doing graphical operations on this
    component
  • void paint(Graphics g) - method that shows
    component, should not be called by user
    (overridden)
  • void repaint() - informs the computer that this
    component needs to be repainted
  • void repaint(int x, int y, int width, int height)
    - similar to repaint() but indicates only the
    portion falling in the rectangle needs to be
    redone
  • void update(Graphics g) - how component redone
    (generally box erased, repainted), useful to
    avoid flicker, should not be called by user
    (overridden)

6
Text Classes
  • Label - simple text statements, generally one
    line with no editing expected
  • TextComponent - abstract class with methods used
    by
  • TextField - one line text, with some editing
  • TextArea - generally for more than one line of
    text with some editing

7
  • Label class
  • constructors
  • Label() - no text, center alignment
  • Label(String label) - center-aligned label with
    text
  • Label(String label, int alignment) - label with
    text
  • aligned based on arg, vals Label.LEFT, CENTER,
    RIGHT
  • methods
  • String getText() - return string associated with
    Label
  • void setText(String label) - set string
  • int getAlignment() - return Labels current
    alignment
  • void setAlignment(int alignment) - set alignment

8
  • TextComponent class
  • abstract class (parent of TextField, TextArea)
  • methods
  • String getText() - return the text associated
    with object
  • void setText(String text) - set text of object
  • boolean isEditable() - flag indicating text can
    be edited
  • void setEditable(boolean canEdit) - set editable
    flag
  • String getSelectedText() - return highlighted
    text
  • int getSelectionStart() - location where
    selection starts
  • int getSelectionEnd() - location where selection
    ends
  • void select (int start, int end) - select text
    start-end
  • void selectAll() - select all of text

9
  • TextField class - one line, editing
  • constructors
  • TextField() - empty (no text), default (system)
    width
  • TextField(int columns) - no text, columns wide
  • TextField(String text) - set text, use it for
    width
  • TextField(String text, int columns) - set text,
    width
  • methods
  • int getColumns() - return column width of object
  • void setColumns(int c) - set column width
  • char getEchoChar() - return echo char (if any)
  • void setEchoChar(char c) - set echo char
  • boolean echoCharIsSet() - check if echo char set
  • Dimension minimumSize() - minimum needed size
  • Dimension preferredSize() - preferred size

10
  • TextArea class - gt 1 line, editing
  • look and feel depends on system (may include
    scrollbars)
  • constructors
  • TextArea() - empty (no text), default (system)
    size
  • TextArea(int rows, int cols) - no text, rows,
    cols size
  • TextArea(String text) - set text, default sized
  • TextField(String text, int rows, int cols) - set
    text, size
  • methods
  • int getColumns() - column width of object
  • int getRows() - row height of object

11
  • TextArea class (cont)
  • methods
  • void appendText(String str) - add string to the
    end of the current text
  • void insertText(String str, int pos) - insert
    string at position pos
  • void replaceText(String str, int start, int end)
    - insert string replacing chars from start to end
  • Dimension minimumSize() - minimum required size
  • Dimension preferredSize() - preferred size

12
import java.applet. import java.awt. public
class TestClass extends Applet Label myLabel1
new Label("Test label 1",Label.LEFT) Label
myLabel2 new Label() TextField myTextField1
new TextField("Init text",20) TextField
myTextField2 new TextField("Start text")
TextArea myTextArea new TextArea("Initial
text",3,10) public void init ()
resize(500,400) myLabel1.setText("TLab1")
myLabel2.setText("TLab2") myTextField1.setFore
ground(Color.green) myTextField2.setEchoCharac
ter('') myTextArea.insertText("silly ",8)
add(myLabel1) add(myTextField1)
add(myTextArea) add(myLabel2)
add(myTextField2)
13
Active Widgets
  • Various classes provide mechanisms for users to
    indicate info
  • Button - labeled object that can be clicked
  • Checkbox - labeled object, can be checked/un
  • Choice - object providing multiple choices with
    one showing
  • List - object providing multiple choices with all
    showing

14
  • Button class
  • constructors
  • Button() - unlabeled button
  • Button(String label) - button with label
  • methods
  • String getLabel() - return label of object
  • void setLabel(String label) - set label of Button
  • We will learn later how to check if the mouse has
    been used to access button

15
  • Checkbox class
  • constructors
  • Checkbox() - no label, initially unchecked
  • Checkbox(String label) - labeled, initially
    unchecked
  • Checkbox(String label, CheckboxGroup group,
    boolean state)
  • methods
  • String getLabel() - return label of object
  • void setLabel(String label) - set label of
    Checkbox
  • boolean getState() - determine if
    checked/unchecked
  • void setState(boolean state) - set the state of
    the Checkbox
  • CheckboxGroup getCheckboxGroup() - returns group

16
  • CheckboxGroup class
  • Checkbox objects added using Checkbox methods
  • constructors
  • CheckBoxGroup() - empty group
  • methods
  • Checkbox getCurrent() - Checkbox of group that is
    checked
  • void setCurrent(Checkbox c) - set c as checked
    member

17
import java.applet. import java.awt. public
class TestButton extends Applet Button
myButton new Button("AButton") Checkbox
myCheckBox1 new Checkbox("CB1") Checkbox
myCheckBox2 new Checkbox("CB2") CheckboxGroup
myCheckBoxGroup new CheckboxGroup() Checkbox
myCBG1 new Checkbox("Big") Checkbox myCBG2
new Checkbox("Bigger") Checkbox myCBG3 new
Checkbox("Biggest") public void init ()
resize(500,400) myCheckBox2.setState(true)
myCBG1.setCheckboxGroup(myCheckBoxGroup)
myCBG2.setCheckboxGroup(myCheckBoxGroup)
myCBG3.setCheckboxGroup(myCheckBoxGroup)
myCheckBoxGroup.setCurrent(myCBG1)
add(myCheckBox1) add(myButton)
add(myCBG1) add(myCBG2) add(myCBG3)
add(myCheckBox2)
18
  • Choice class
  • constructor
  • Choice() - initially empty set of choices
  • methods
  • void addItem(String item) - add the named item to
    the choices
  • int getSelectedIndex() - return num of selected
    item
  • String getSelectedItem() - return string
    associated with item
  • String getItem(int index) - returns string of
    index item
  • void select(int index) - select the index item
    (starting at 0)
  • void select(String item) - select item with
    string name
  • int countItems() - return number of items

19
  • List class
  • constructors
  • List() - default-size list in single-selection
    mode
  • List(int rows, boolean multSel) - list with rows,
    if multSel true can make multiple selections
  • methods
  • void addItem(String item) - add the named item to
    the choices
  • void addItem(String item, int index) - add item
    at loc index
  • void delItem(int index) - remove the index item
  • void delItems(int start, int end) - remove items
    from start to end
  • void clear() - remove all items
  • void replaceItem(String item, int index) -
    replace item at index
  • int countItems() - count of the number of items
    in List

20
  • methods
  • void getSelectedIndex() - num of selected index
  • -1 if multiple or none
  • boolean isSelected(int index) - true if index
    selected
  • void select(int index) - set index as selected
  • void deselect(int index) - unselect index
  • void makeVisible(int index) - scroll so index
    visible
  • int getVisibleIndex() - index of last makeVisible
    call
  • boolean allowsMultipleSelections() - true if list
    allows multiple selections
  • void setMultipleSelections(boolean m) - set
    multiple selection flag to m

21
  • methods
  • int getRows() - number of rows in object
  • Dimension minimumSize() - min size needed
  • Dimension minimumSize(int rows) - min size for
    rows
  • Dimension preferredSize() - preferred size
  • Dimension preferredSize(int rows) - preferred by
    rows

22
import java.applet. import java.awt. public
class TestLists extends Applet Choice myChoice
new Choice() List myList new
List(4,true) public void init ()
resize(500,400) myChoice.addItem("Small")
myChoice.addItem("Medium") myChoice.addItem("L
arge") myChoice.select("Medium")
myList.addItem("Left") myList.addItem("Right")
myList.addItem("Straight")
myList.select(0) myList.select(2)
add(myChoice) add(myList)
Write a Comment
User Comments (0)
About PowerShow.com