CIS 121 February 25, 2000
On Wednesday, we discussed Graphical User Interfaces and saw an example of
adding a Button Component to a Container. Today we will look at other
things that can be added to a Container.
We have discussed part of the hierarchy of the AWT package. Let us
examine some more of the hierarchy.
|
|
|
Component |
|
|
|
|
| Button |
Checkbox |
Choice |
Container |
Label |
|
TextComponent |
|
|
|
|
Window |
|
TextArea |
|
TextField |
|
|
|
Frame |
|
|
|
|
In the Component class we have discussed three important methods:
- public void setSize(int width,int height);
- public void setBackground(Color c);
- public void setVisible(boolean b);
Another important method from the Component class is
- public void setLocation(int x,int y);
In the Container class there are two important methods:
- public Component add(Component comp);
- public void setLayout(LayoutManager mgr);
Components such as Button allow the user to send signals to the computer.
We can interpret those signals and perform appropriate actions.
An event is normally associated with action such as a mouse button
click. When a button is clicked an object called ActionEvent is generated.
In order to capture this object and use it, we use the interface
ActionListener. This interface has one abstract method called
actionPerformed. The parameter to actionPerformed is an ActionEvent. Since
ActionListener is an interface, we must implement actionPerformed.
In order for the interpreter to create an ActionEvent object, it must
listen for events. An event listener acts as a communicator
between the component and the ActionListener. Each component inherits
a method called addActionListener. The parameter list of addActionListener
is an ActionListener object. Recall that any class that implements the
interface ActionListener can be passed into a method whose parameter
is an ActionListener object. Therefore, when we implement the ActionListener interface
and call the method addActionListener, we may use the keyword this as
the parameter.
ActionEvent and ActionListener are both located in the package java.awt.event so this
package must be imported in any class definition where we use these classes.
There are several Listener Intefaces available to us.
ActionListener
public interface ActionListener extends EventListener{
public void actionPerformed(ActionEvent e);
}
WindowListener
public interface WindowListener extends EventListener {
public abstract void windowActivated(WindowEvent e);
public abstract void windowClosed(WindowEvent e);
public abstract void windowClosing(WindowEvent e);
public abstract void windowDeactivated(WindowEvent e);
public abstract void windowDeiconified(WindowEvent e);
public abstract void windowIconified(WindowEvent e);
public abstract void windowOpened(WindowEvent e);
}
ItemListener
public interface ItemListener extends EventListener {
void itemStateChanged(Event e);
}
MouseListener
public interface MouseListener extends EventListener {
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
}
All of these interfaces are located in the java.awt.event package and that package must
be imported if we implement the interfaces.
Now let us examine the components availabe to us and how we can handle
events generated by these components. There are two methods we can use
to tell what triggered the event. One is the getActionCommand, and the other
is getSource. We looked at an example of use getActionCommand to test the
text of a button. We can use getSource to see what object generated the event.
Button
A Button a graphical representation of a push-button in the
container
Constructors:
- public Button();
- public Button(String label);
Event Generated: ActionEvent
Listener Interface: ActionListener
Example:
Source Code:
Gui.java
import java.awt.*;
class Gui extends Frame {
public Gui(String s) {
super(s);
setBackground(Color.yellow);
setLayout(new FlowLayout());
Button pushButton = new Button("press me");
add(pushButton);
}
}
Ex_1.java
class Ex_1 {
public static void main(String[] args) {
Gui screen = new Gui("Example 1");
screen.setSize(500,100);
screen.setVisible(true);
}
}
Label
A label is a text string that may be used to label other components.
Constructors:
- public Label();
- public Label(String label);
- public Label(String label,int alignment);
Event Generated: None
Example:
Source Code:
Gui4.java
import java.awt.*;
class Gui4 extends Frame {
public Gui4(String s) {
super(s);
setLayout(new FlowLayout());
Label l1 = new Label("NAME: ",Label.LEFT);
Label l2 = new Label("AGE: ",Label.CENTER);
Label l3 = new Label("ADDRESS:",Label.RIGHT);
add(l1);
add(l2);
add(l3);
}
}
Ex_4.java
class Ex_4 {
public static void main(String[] args) {
Gui4 screen = new Gui4("Example 4");
screen.setSize(500,100);
screen.setVisible(true);
}
}
Checkboxes
Checkboxes are components that have two states: on or off. A special
case of checkboxes are radio buttons. With radio buttons only one choice
can be on.
Constructors:
- public Checkbox();
- public Checkbox(String label);
- public Checkbox(String label,Checkboxgroup group,boolean state);
Event Generated: ItemEvent
Listener Interface: ItemListener
Example:
Source Code:
Gui5.java
import java.awt.*;
class Gui5 extends Frame {
public Gui5(String s) {
super(s);
setLayout(new FlowLayout());
Checkbox c1 = new Checkbox("Eggs");
Checkbox c2 = new Checkbox("Toast");
Checkbox c3 = new Checkbox("Fries");
Checkbox c4 = new Checkbox("Yogurt");
Checkbox c5 = new Checkbox("Bagels");
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
}
}
Ex_5.java
class Ex_5 {
public static void main(String[] args) {
Gui5 screen = new Gui5("Example 5");
screen.setSize(500,100);
screen.setVisible(true);
}
}
In order to create Radio Buttons we use the class CheckboxGroup. We
create a new instance of a CheckboxGroup and pass this as a parameter
to the third constructor for Checkbox.
Example:
Source Code:
Gui6.java
import java.awt.*;
class Gui6 extends Frame {
public Gui6(String s) {
super(s);
setLayout(new FlowLayout());
CheckboxGroup whatNext = new CheckboxGroup();
Checkbox Continue = new Checkbox("Continue",whatNext,true);
Checkbox Exit = new Checkbox("Exit",whatNext,false);
add(Continue);
add(Exit);
}
}
Ex_6.java
class Ex_6 {
public static void main(String[] args) {
Gui6 screen = new Gui6("Example 6");
screen.setSize(500,100);
screen.setVisible(true);
}
}
TextFields
A TextField allows you to either input or output textual information.
Constructors:
- public TextField();
- public TextField(int columns);
- public TextField(String text);
- public TextField(String text,int columns);
Event Generated: ActionEvent
Listener Interface: ActionListiner
TextAreas
A TextArea is similar to a TextField but it gives more than one line.
Constructors:
- public TextArea();
- public TextArea(String text);
- public TextArea(int rows,int columns);
- public TextArea(String text,int rows,int columns);
- public TextArea(String text,int rows,int columns,int scrollbars);
Scrolling Lists
Constructors:
- public List();
- public List(int rows,boolean multipleSelections);
Event Generated: ActionEvent
Listener Interface: ActionListener
Example:
Source Code:
Gui7.java
import java.awt.*;
class Gui7 extends Frame {
public Gui7(String s) {
super(s);
setLayout(new FlowLayout());
List menu = new List(4,true);
menu.add("Eggs");
menu.add("Toast");
menu.add("Fries");
menu.add("Yogurt");
menu.add("Bagels");
menu.add("Pancakes");
menu.add("Coffee");
add(menu);
}
}
Ex_7.java
class Ex_7 {
public static void main(String[] args) {
Gui7 screen = new Gui7("Example 7");
screen.setSize(500,100);
screen.setVisible(true);
}
}
Drop Down Menus
A Drop Down Menu has two parts - The MenuBar and the Menu.
MenuBar
A MenuBar is a bar containing the name of the menu.
Constructor:
Menu
A Menu is a list of items.
Constructors:
- public Menu();
- public Menu(String label);
Layout Managers
There are five main predefined Layout managers we can use. We have
seen how FlowLayout works. Components are added to the container in rows
and when there is not enough space left on the row to add a new
component, it is placed on the next row.
There are three constructors for the FlowLayout class.
- public FlowLayout();
- public FlowLayout(int align);
- public FlowLayout(int align,int hGap,int vGap);
The other predefined Layout manager we will consider is the GridLayout.
There are two constructors for the GridLayout class.
- public GridLayout(int rows,int columns);
- public GridLayout(int rows,int columns,int hGap,int vGap) throws IllegalArgumentException;
We may also choose to define our own Layout. We can do this by using
the setLocation method from the Component class. We specify in pixels where
the coordinates of the upper left-hand corner of the component should be,
where (0,0) are the coordinates of the upper left-hand corner of the container.
Another important class in the awt is the Graphics class. This is an
abstract class and cannot be instantiated by a constructor. In order
to create a new Graphics object we use the getGraphics() method of a
Component.
Your exercise for the rest of the class period is to take the source
code for a couple of the components and implement Event handling.
Recall the methods in each of the interfaces, and depending on which
interface you decide to implement make sure you implement the appropriate method.
Recall the two methods available to you.
- getSource returns an object
- getActionCommand returns the string data from any text field