CIS 121 February 21, 2000


Today we will begin discussing Module 6.

Module 6 deals with the topic of Graphical User Interfaces. A Graphical User Interface usually consists of a Container, such as a Window or Frame, into which components are placed.

The Java package we will use in AWT (Abstract Windowing Toolkit). Part of the hierarchy in this package begins at the Component. A subclass of Component is Container. A subclass of Container is Window. A subclass of Window is Frame. The normal rules of inheritance apply.

From the Component class there are three useful methods.

public void setSize(int width, int height); public void setBackground(Color c); public void setVisible(boolean b);

From the Container class there are two useful methods.

public Component add(Component comp); public void setLayout(LayoutManager mgr);

In the Component class, setSize determines how large our frame will be. setBackground determines the background color, and setVisible determines whether or not to display the frame.

width and height are measured in pixels, picture elements. Typically 500 pixels is approximately 6 inches on a 17 inch monitor.

Colors come from the class Color. Here there are defined some standard colors and the ability to make others. The RGB code is used to define colors. This is a six hexadecimal digit code. The first two hexadecimal digits range from 00 (0) to FF (255). This determines how much red will be in the color. We have 256 choices. The second two hexadecimal digits determine how much green is in the color. The last two hexadecimal digits determine how much blue is in the color. If all six hexadecimal digits are 0, the color is black. If all six hexadecimal digits are F, the color is white. Since there are 24 different bits, there are 224possible combinations.

LayoutManager is a class we will talk about more in detail later. But a flow layout refers to how components are placed in a frame. A typical one is a flow layout which places components on rows.

There are several components we can place in a frame. We will consider one of them now. On page 362 in your book, you will find two small sample programs. Type these files in and execute Ex_1. The component we will consider is the button. The Button class has two different constructors. The default constructor will create a button with no label. The second constructor has a String parameter which will be the label of the button. 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);
   }

}
Experiment with different labels for the button, different titles for the frame, and different background colors.

On Wednesday we will continue with this discussion. In preparation read Chapter 10 in your book.