GUI Programming 1
GUI Programming 1
Programming I
Lecture Objectives
• Understand the basic concepts of Graphical User
Interfaces (GUIs)
• Practice Basic GUI Programming in Java
• Develop Basic GUI Applications
• Study Layout Management and Managers for GUI
Applications
Outline
Graphical User Interfaces (GUIs)
-- Overview
-- Essential elements
Containers
-- Overview
-- Examples
Components
-- Overview
-- Examples
Layout Managers
-- Overview
-- Examples
Graphical User Interface (GUI)
• A Graphical User Interface (GUI) is one variety of user
interface.
• User interacts with objects on the screen (icons, JButtons,
scroll-bars, etc.) via mouse clicks or keyboard actions.
AWT Swing
JButton JJButton
Keep in Mind
• We will program graphical elements in source
code.
▪ There are drag and drop systems (graphical integrated
development environments (IDEs).
Containers
-- used to hold items (e.g., the frame)
offset
Components
-- the widgets or interactors (e.g.,
JButtons) CLICK ME
LayoutManagers
-- the hidden algorithm used to offset
organize the widgets inside the
container
GUI Basic Constructs
The three basic constructs used in every GUI are:
1.
Containers
File Edit Help
offset
2.
Components CLICK ME
3.
LayoutManagers
offset
Containers
Examples of Containers:
AWT Swing
• Panel • JPanel
• Frame • JFrame
• Applet • JApplet
• Window • JWindow
import javax.swing.*;
public class HelloGUI {
So, for now, you’ll just have to use Ctrl-C to end the program.
Once the basics of GUI construction are covered, we’ll return
to this problem.
GUI Design Idea
1. Inheritance javax.swing.JFrame
-- our class extends a container
MyGUI
2. Composition MyGUI
-- our class holds a container
java.awt.Frame
GUI Design Idea: Example
We save our
import javax.swing.*; single inheritance
public class HelloComposition {
JFrame f;
public HelloComposition() {
f = new Frame("Composition Test");
f.setSize(200,200);
f.setBackground(Color.red);
f.show();
Check the API
}
public static void main(String[] args) {
HelloComposition h = new HelloComposition();
}
}
Inheritance Composition
• Use up our single inheritance • Saves the single inheritance
myFrame.setBackground(Color.red);
An inherited
A class, also
method
in the API
Container Summary
We may often use "composition" where:
Demo: HelloWidget
Component Examples (Cont’d)
Component - generic widget that you can interact with
• Utilize mouse events and mouse motion events to interact with the
user to accomplish the drawing tasks.
TextField:
• Can generate Key events to indicate that the user has typed a key.
• More typically, it generates an Action event when the user finishes the
data entry and hits Return in the TextField.
Component Examples (Cont’d)
JButton:
• Simply a clickable component.
Label:
• A one-line field of text.
• User cannot change this text directly; program changes text with
setText( ) method.
public HelloComponent(){
f = new JFrame("Component Test");
f.setSize(200,200);
f.setBackground(Color.red);
JPanel p = new JPanel();
JJButton b = new JJButton("Hello
Components");
p.add(b);
f.add(p);
f.show();
}
public static void main (String[] args) {
new HelloComponent();
}
}
Layout Managers
We can now create Components and Containers.
Click
Note: JButton’s
x and y coordinate
starts from top left 25 pixels over
Layout Managers: Motivation (Cont’d)
Problems with specifying (x, y) coordinates for Components:
Solution:
For example:
To specify a BorderLayout:
NORTH
SOUTH
Layout Managers: Constraints (Cont’d)
• BorderLayout then appends constraint information on
all components, e.g.:
public Test() {
super ("BorderLayout Demo");
this.setSize(200,200);
this.setLayout(new BorderLayout());
this.add (new JButton ("North"), BorderLayout.NORTH);
this.add (new JButton ("South"), BorderLayout.SOUTH);
this.add (new JButton ("East"), BorderLayout.EAST);
this.add (new JButton ("West"), BorderLayout.WEST);
this.add (new JButton ("Center"), BorderLayout.CENTER);
}
EAST
CENTER
SOUTH
{ a fresh canvas
for drawing here}
BorderLayout: Simple Example
import javax.swing.*;
public class HelloLayout {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(400,400);
BorderLayout bord = new BorderLayout();
f.setLayout(bord);
Let’s run it
and find out...
BorderLayout: Simple Example (Cont’d)
import javax.swing.*;
public class HelloLayout {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(400,400);
BorderLayout bord = new BorderLayout();
f.setLayout(bord);
• Examples:
▪ GridLayout()
▪ FlowLayout()
public GridLayout();
For example:
generates:
GridLayout (Cont’d)
• To add components (or containers) to a GridLayout, particular
locations are not specified (unlike BorderLayout).
1 2 3
4 5 6
GridLayout (Cont’d)
Optionally, two additional parameters may be used with
GridLayout to specify the horizontal and vertical spacing (in pixels)
between grid elements:
where hspace specifies horizontal size, and vspace specifies vertical size
e.g.,
setLayout (new GridLayout (2, 2, 7, 5));
hspace
vspace
GridLayout: Example 1
import javax.swing.*;
public class CalcPad extends JFrame {
public CalcPad() {
setLayout(new GridLayout(5,3));
add (new JButton ("7"));
public static void main (String[] args){
add (new JButton ("8"));
CalcPad ti = new CalcPad();
add (new JButton ("9"));
ti.setSize(150,150);
add (new JButton ("4"));
ti.show();
add (new JButton ("5"));
}
add (new JButton ("6")); Program Output:
}//CalcPad
add (new JButton ("1"));
add (new JButton ("2"));
add (new JButton ("3"));
add (new JButton ("."));
add (new JButton ("0"));
add (new JButton ("+/-"));
add (new Panel());
}
GridLayout: Example 2
import javax.swing.*;
public class CalcPad extends JFrame {
public CalcPad() {
setLayout(new GridLayout(5,3));
int off[]={-2,2,0};
for (int i=9; i >= 1; i--)
add (new JButton (""+(i+off[i%3])));
add (new JButton ("."));
add (new JButton ("0"));
Program Output:
add (new JButton ("+/-"));
add (new Panel());
}
public static void main (String[] arg){
CalcPad ti = new CalcPad();
ti.setSize(150,150);
ti.show();
} }//CalcPad
BoxLayout: Motivation
Often, it is desirable to place items in horizontal or vertical direction. A
grid layout may not be the best choice, since grid components are
resized to fit the available space--it’s a distorts its contents.
container
component
BoxLayout.X_AXIS;
BoxLayout.Y_AXIS;