ICS3U GUI Lesson 1 Intro
ICS3U GUI Lesson 1 Intro
LG
We will learn and utilize GUI components such as: frames, panels,
buttons, labels and text fields to design and code programs to solve
problems
SC
We will be able to:
Demonstrate the knowledge and understanding of the GUI components listed above
complete the practice exercises, formative and summative tasks
INTRODUCTION
All the programs we have made so far interact with the user via the
console.
The preference is to have the program run in its own window
We would also like to make use of common OS components like text
fields, check boxes, pull down lists, buttons, and scroll bars.
EXAMPLE - GUI
http://www.javaguicodexample.com/javadesktopguimysql2_files/javadesktopmysqlapp001.png
FOCUS
Let’s start by looking at 3 difference GUI components:
Text fields – users can type in here and/or the program can put text in here
http://www.ntu.edu.sg/home/ehchua/programming/java/images/AWT_ContainerComponent
.png
THE CODE
import javax.swing.*;
import java.awt.*; <- These imports are required to use GUI components
<-Class/Instance variables can go here (define your buttons, etc)
public class FrameExample extends JFrame { <- You class name now must extend JFrame
<- This turns your program into a program that
<- describes a frame
public FrameExample() {
setTitle("Hello World!");
setSize(320, 240);
setVisible(true);
}
CLASS VARIABLES
This is the section you can create buttons, text fields, and labels.
Each one requires certain attributes when you declare them
Once these are created you will need to add them to you frame. This
is done in the constructors
We will focus on buttons, labels, and textfields.
BUTTONS
The following code creates a Button:
add(okButton);
add(nameLabel);
add(nameField);
setVisible(true);
}
YOUR TURN