CIS 121 February 23, 2000
There are a few things that need to be mentioned.
Remember that tomorrow is the last day to officially drop a course.
The School of CIS is implementing a new policy that all of its students
must have an e-mail address. To enter your e-mail address(es), please go
to the URL http://www.cis.usouthal.edu/email. This
will improve communications between faculty and students.
In an effort to provide more assistance to students in this course, we
have set up an on-line FAQ system. This system will give you the
opportunity to ask any questions you might have regarding the material.
There will be no way to trace the origins of the questions so you may
feel free to ask any questions you have. There is a link to the FAQ on both
this page and the general page for CIS 120 and 121.
We will continue today talking about Graphical User Interfaces.
Recall that the package we are using is AWT (Abstract Windowing Toolkit).
Part of the hierarchy is
|
Within Component there are three methods that are important.
- public void setSize(int width,int height);
- public void setBackground(Color c);
- public void setVisible(boolean b);
width and height are measured in pixels, picture elements. 500 pixels
is approximately 6 inches on a 17 inch monitor.
Color is an object. Color is implemented by a series of 6 hexadecimal digits. This is known
as RGB. The first two hexadecimal digits represents the amount of red in the color, the second
two hexadecimal digits represents the amount of green in the color, and the
last two hexadecimal digits represents the amount of blue in the color.
setVisible determines whether or not the Component is displayed.
Within the Container subclass there are two important methods.
- public Component add(Component comp);
- public void setLayout(LayoutManager mgr);
There are several different components that we can consider. Some of
these are buttons, labels, checkboxes, radio buttons, etc.
Recall that Window is a subclass of Container and Frame is a subclass
of Window. Since they are all decendants of Component, they inherit all
of the methods of Component. It is very simple to set up a Frame which
contains a component. Consider Example 10.1 on page 362.
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);
}
}
Type this example in and run it.
Note that even though we have the button on the frame, nothing happens
if we click it. However, when we click it, signals are being sent to the
computer. So what we must do in order to make something happen is
intercept those signals and interpret them. To accomplish this, we must
use what is called a listener.
In contrast to sequential programming languages, object oriented programs
are event driven. In particular for the example, what we want is to execute
some code when a user clicks the button. Therefore, what we want to do is
simulate the actions of the Windows desktop.
An event is normally associated with an action such as clicking
a button. When we click a button an object called ActionListener is generated.
This is an interface that contains a single method, ActionPerformed(ActionEvent e). This
is automatically invoked when a button is clicked. Since it is an interface
we must implement the ActionPerformed method. This is another example
of why we use abstraction. The ActionListener can be used for different
components but based on that component we need to take a different action.
An event listener acts as a communicator between the button object
and the ActionListener object. A method called addActionListener is used
to set up a new ActionListener object.
The source of the event can be determined through the getActionCommand() from
the ActionEvent class. This way if there are several buttons, we can
determine which was pushed.
The procedure for detecting a button being pressed is
- Add an actio listener for the button. The formal parameter list
of addActionListener is addActionListener(ActionListener l)
- We must implement the methods of ActionListener since it is an
interface
- How we implement actionPerformed determines what happens when the
event is detected.
We must also import the java.awt.event package because ActionEvent and
ActionListener are contained in it.
Consider Example 10.2.
Gui1.java
import java.awt.event.*;
import java.awt.*;
class Gui1 extends Frame implements ActionListener {
public Gui1(String s) {
super(s);
setBackground(Color.yellow);
setLayout(new FlowLayout());
Button pushButton = new Button("press me");
add(pushButton);
pushButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
final char BELL = '\u0007';
if (event.getActionCommand().equals("press me"))
System.out.print(BELL);
}
}
Ex_2.java
class Ex_2 {
public static void main(String[] args) {
Gui1 screen = new Gui1("Example 2");
screen.setSize(500,100);
screen.setVisible(true);
}
}
Note that we can't kill the frame by clicking the X button or choosing close.
This is because we are not listening for those events. In order to do
that we must implement the WindowListener interface.
The WindowListener interface has the following methods.
public abstract 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);
}
In order for us to detect when we click the X button or the Close,
we need to implement the method windowClosing. We implement it as
follows.
public void windowClosing(WindowEvent event) {
System.exit(0);
}
Consider Example 10.3.
Gui2.java
import java.awt.event.*;
import java.awt.*;
class Gui2 extends Frame implements ActionListener,WindowListener {
public Gui2(String s) {
super(s);
setBackground(Color.yellow);
setLayout(new FlowLayout());
addWindowListener(this);
Button pushButton = new Button("press me");
add(pushButton);
pushButton.addActionListener(this);
}
public void windowClosed(WindowEvent event) { }
public void windowDeiconified(WindowEvent event) { }
public void windowIconified(WindowEvent event) { }
public void windowActivated(WindowEvent event) { }
public void windowDeactivated(WindowEvent event) { }
public void windowOpened(WindowEvent event) { }
public void windowClosing(WindowEvent event) {
System.exit(0);
}
public void actionPerformed(ActionEvent event) {
final char BELL = '\u0007';
if (event.getActionCommand().equals("press me"))
System.out.print(BELL);
}
}
Ex_3.java
class Ex_3 {
public static void main(String[] args) {
Gui2 screen = new Gui2("Example 3");
screen.setSize(500,100);
screen.setVisible(true);
}
}
We will discuss these ideas more in class Friday. You may experiment with
using these ideas by looking at the exercises on page 422.