Transcript Document
Agenda For March 26
1. Reminder: QUIZ #3 (Variables) on Monday.
2. PowerPoint Presentation on Graphical User Interface Components.
http://members.shaw.ca/rzamar/IT11/GUI_Components.ppt
3. PowerPoint Presentation on Variables (Snowboarding Example).
http://members.shaw.ca/rzamar/IT11/Variables3.ppt
http://members.shaw.ca/rzamar/IT11/JavaComponents.java
4. Mastery Questions Section 6.2 (page 47).
5. Mastery Questions Section 6.3 (page 51).
Graphical User Interface Components
What is a graphical user interface?
A graphical user interface (GUI) refers to the various
“pieces” that a program has which allows a user to interact
with the program. Each individual type of “piece” is called a
component. Examples of some commonly used components
are buttons, labels, text fields and text areas.
The three basic steps for creating components in your
program are:
1. Declaring the component.
2. Initializing the component.
3. Adding the component to the applet display
How Components Will Appear To The User
Step 1: Declaring The Component
A component should always be declared in the variable declaration
section (above the init method).
For example:
import java.awt.*;
import java.applet.*;
public class myGui extends
{
Label myLabel;
Button myButton;
TextField myTextField;
TextArea myTextArea;
public void init()
{
}
}
Applet
//
//
//
//
label declarations
button declaration
text field declaration
text area declaration
Step 2: Initializing The Component
Each component should be initialized in the init() function
of an applet. For example, the init() function for the previous
example might contain the following code.
public void init()
{
myLabel = new Label("some label");
myButton = new Button("button");
myTextField = new TextField();
myTextArea = new TextArea();
}
Step 2: Initializing The Component Continued…
In order to complete the initialization of a java component
we must tell Java where to place this component on screen
and specify its size.
The setBounds(int x, int y, int w, int h)
method is used for this.
The following slide shows an example.
Example of the setBounds() Method
public void init()
{
myLabel = new Label("some label");
myButton = new Button("button");
myTextField = new TextField();
myTextArea = new TextArea();
myLabel.setBounds(10,10,100,20);
myButton.setBounds(120,10,100,20);
myTextField.setBounds(10, 50, 100, 20);
myTextArea.setBounds(10, 80, 210,210);
}
Adding the Component To The Applet Display
In order to display a component on the applet display, we must use
the add(Component) method. For example:
public void init()
{
myLabel = new Label("some label");
myButton = new Button("button");
myTextField = new TextField();
myTextArea = new TextArea();
myLabel.setBounds(10,10,100,20);
myButton.setBounds(120,10,100,20);
myTextField.setBounds(10, 50, 100, 20);
myTextArea.setBounds(10, 80, 210,210);
add(myLabel);
add(myButton);
add(myTextField);
add(myTextArea);
}
public class Gui extends Applet
{
Label myLabel;
Button myButton;
TextField myTextField;
TextArea myTextArea;
public void init()
{
setLayout(null);
myLabel = new Label("some label");
myButton = new Button("button");
myTextField = new TextField();
myTextArea = new TextArea();
myLabel.setBounds(10,10,100,20);
myButton.setBounds(120,10,100,20);
myTextField.setBounds(10, 50, 100, 20);
myTextArea.setBounds(10, 80, 210,210);
add(myLabel);
add(myButton);
add(myTextField);
add(myTextArea);
}
}