Advantage of components over graphics objects is that they are automatically redrawn during repaint. Need not explicitly put in commands to draw them!
Constructor is:
public Button(String label);when executed creates a button with "label" printed on it. Generally large enough to display label.
Responds to variety of messages. One of most useful is
public void setBackground(Color buttonColor);which changes color of button.
Color is predefined class with many constants representing colors:
Add a button to an applet by writing:
add(startButton);This will normally result in button being added to applet in first available space starting from top left and working across from left to right (and then trying next row, etc.)
Button generates an event when user clicks on it.
Method "action" is executed whenever there is an event (and no other routine executing)
public boolean action(Event evt, Object obj);Event has a number of fields which can be used to identify the kind of event. One of simplest things to do is to identify the object involved with the action by examining the "target" field.
E.g., if Button startButton = new Button("start");
then can write
if (evt.target = startButton) doStart();See program buttons for simple example.
Look at program bouncing ball to see use of button and graphics objects from library.
The constructors are
public Label() // creates label with no text public Label(String s) //create label with text sMethods
public String getText() // return label text public void setText(String s) // sets the label text
The constructors are
public TextField () // creates text field with no text public TextField (int columns) // create text field with appropriate number of columns public TextField (String s) // create text field with s displayed public TextField (String s, int columns) // create text field with s displayed & appropriate widthMethods
public void setEditable(boolean s) // if false the TextField is not user editable public String getText() // return label text public void setText(String s) // sets the label textWhen the user types into a text field and then hits the return or enter key, it generates an event which can be handled using the action routine.
Recall the action routine has declaration:
public boolean action (Event evt, Object obj)As with buttons, test to see if evt.target == myTextField. If so, can get contents from obj, which has type Object.
To be able to do anything with it, you must "downcast" obj to String.
Alternatively, you can get the contents of the field using getText().
The constructors are
public TextArea (int rows, int columns) // create text field with appropriate number of rows & columns public TextArea (String s, int rows, int columns) // create text field with rows, columns, & displaying sMethods
public void setEditable(boolean s) // if false the TextField is not user editable public String getText() // return label text public void setText(String s) // sets the label textUnlike TextField, hitting return or enter does not generate an event.
See example Book (but ignore layout).
The constructor is:
public Choice() // create new choice buttonThe most useful methods are:
public void addItem(String s) // add s to list of choices public int countItems() // return # choices in list public String getItem(int index) // return item at index public String getSelectedItem() // return selected item public int getSelectedIndex() // return index of selected itemThe user selecting an item is an event which is handled as usual with action.
BorderLayout has two constructors:
public BorderLayout() public BorderLayout(int horizGap, int vertGap) // leaves given gap size between components.The layout manager of an applet can be changed to BorderLayout by executing:
setLayout(new BorderLayout())Components can be added by using a more refined version of add:
add(String direction, Component comp)Thus to add a button on the north, write
add("North",startButton)Note that all directions must begin with a capital letter or an error will occur.
Components added to "North" and "South" extend to the left and right edges of the Applet, while those added "East" and "West" extend up and down to the components to the "North" and "South". The component in the "Center" fills the remaining space.
If one component is omitted, other components stretch to fill that space.
With the BorderLayout, components stretch to fill all of the Applet. As a result using this by itself can often make layouts look distorted.
public GridLayout(int rows, int cols) // Construct layout w/ given rows and columns public GridLayout(int rows, int cols, int h, int v) // Construct layout w/ given rows and columns, and // horiz and vertical gaps of h and v between componentsExecuting
setLayout(new GridLayout(2,3))divides the applet into 2 rows and 3 columns. When "add" method is executed, components are added from left to right first across the first row, then second row, etc.
The only somewhat unfortunate thing about GridLayout is that all components added are resized to be exactly the same size.
The constructor for a panel is:
public Panel()
Its constructor is:
public Canvas()Both Panel and Canvas have method:
public void resize(int width, int height)Canvases must be resized or they will be set to have size (0,0) (and hence be invisible). You can try to resize panels, but the layout manager may stretch them to a somewhat different size.
The example program Book provides a good example of using panels and both FlowLayout and BorderLayout to lay out an applet nicely.
We'll see other examples later.