Week 10 - GUI Part I
Week 10 - GUI Part I
GUI (Part 1)
Objectives
– To define a subclass of JFrame to implement a customized frame
window.
– To be able to arrange GUI component objects on a window using
basic layout managers and nested panels
– To write GUI application programs using JButton, JLabel,
JTextField and other common GUI objects from the javax.swing
package
– To write GUI application programs using Netbeans Swing GUI Tool
Graphical User Interface
Swing:
•Swing is part of the java foundation classes.
•Swing components are platform-independent.
•Swing components are lightweight components because swing sits on
the top of AWT.
•Swing components is uses the screen resource of an ancestor
instead of having their own and that's why called lightweight or
lighter component.
AWT:
•AWT is called the abstract window tool.
•AWT components are platform-dependent.
•AWT components are heavyweight components
•AWT component are associated with native screen resource and
called heavyweight component.
Various Java GUI Components
from the javax.swing package
Button
Combo Box
Sample GUI Objects
• Various GUI components
from the javax.swing
package.
AWT Class Hierarchy (java.awt package)
Swing Class Hierarchy (javax.swing)
GUI Classes
■ Can be classified into three groups
■ Container classes
■ Ex: JFrame, JPanel, JApplet
■ To contain other components
■ Component classes
■ JButton, JTextField, etc are subclasses of JComponent
■ Helper classes
■ Graphics, Color, Font, etc
■ Used by components and containers to draw and place objects
Container Classes
■ JFrame
■ Is a window not contained inside another window. It is the container
that holds other swing UI components
■ JPanel
■ An invisible container that holds UI components
■ Panel can be nested
■ Can place panels inside a container that includes a panel
■ JDialog
■ A pop-up windows or message box to receive additional information
from the user or provide notification that an event has occurred
■ JApplet – a subclass of Applet. Must extend JApplet to create a
Swing-based applet
Swing GUI Components
• First approach
– Declare & Create object of type JFrame
– Use various methods to manipulate window
• Second approach
– Create class containing application program by extending
definition of class JFrame
– Utilizes mechanism of inheritance
Creating Frames
1) First approach:
import javax.swing.*;
public class MyFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame(“MyFrame");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
Creating Frames
import javax.swing.*;
public class MyFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame(“MyFrame");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
setDefaultCloseOperation
(EXIT_ON_CLOSE)
- terminate when the frame is
closed
300
Creating Frames
2) Second approach :
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setTitle(“MyFrame");
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
contentPane.setLayout(
new FlowLayout());
JButton okButton
= new JButton("OK");
JButton cancelButton
= new JButton("CANCEL");
contentPane.add(okButton);
contentPane.add(cancelButton);
Layout Managers
This shows
the placement
of five buttons
by using
FlowLayout.
FlowLayout
• public FlowLayout()
Constructs a new FlowLayout with a default center alignment and a default
gap of five pixels for both horizontal and vertical.
import java.awt.*;
import javax.swing.*;
//import java.awt.event.*;
}
BorderLayout
e.g To add button 1 in the north region & button 2 in the south region :
cPane.add(new JButton("button 1"), BorderLayout.NORTH);
cPane.add(new JButton("button 2"), BorderLayout.SOUTH);
GridLayout
example :
Container cpane = getContentPane();
JPanel pan = new JPanel(); // create a panel
pan.add(new JButton(“Click”)); // add a button in the
panel
cpane.add(pan) // add the panel in the container
label TextField Top
panel
Middle
Text Area panel
public GUIwithPanels() {
super("Using Panels in JFrame");
Container ctr = getContentPane();
ctr.setLayout(new BorderLayout());
} // constructor
}
Step in Creating Panel
JTextField
JTextArea
• We use a JTextArea object to display or allow the user to
enter multiple lines of text.
• The setText method assigns the text to a JTextArea,
replacing the current content.
• The append method appends the text to the current text.
JTextArea textArea
= new Hello
JTextArea( ); the lost
. . . world
textArea.setText("Hello\n");
textArea.append("the lost ");
textArea.append("world"); JTextArea
• TextArea containing six words.
Designing a Swing GUI in NetBeans
IDE
• In real world, you will create your GUI using tool such as the
NetBeans IDE GUI Builder
• Here we will design the GUI for an application that can calculate
the area and perimeter of a rectangle given its length & width as
the input
• The GUI is shown below:
Designing a Swing GUI in NetBeans
IDE
Free Design
•In the IDE's GUI Builder, you can build your forms by simply
putting components where you want them as if you were using
absolute positioning.
•The GUI Builder figures out which layout attributes are required
and then generates the code for you automatically
Palette
Design
Area
Properties
Navigator
Steps for designing the GUI
4. Adding Components
To add a JLabel to the form:
-In the Palette window, select (click your mouse) the Label component
from the Swing Controls category.
Steps for designing the GUI
4. Adding Components
-Move your mouse into the Design Area.
. When the guidelines appear indicating that the JLabel is
positioned in the top left corner with a small
margin at the top and left edges, click again your mouse to place the Label.
-Double-click the JTextField to select its display text & press Delete.
- The width of the JTextField will be reduced. You need to resize (make it
wider) the JTextField by dragging the JTextField's right edge resize handle
toward the right edge of the enclosing JFrame
Steps for designing the GUI
4. Adding Components
To add the remaining three JLabels & JTextFields
- Repeat all the previous steps to add Jlabels & JTextFields
- Ensure that all the texts for the labels & textfields have been changed
correctly (e.g. second JLabel’s text is ‘Width :’ etc.)
- Once finished, you should have your GUI as shown below:
Steps for designing the GUI
4. Adding Components
To add JButton to the form:
- In the Palette window, select the Button component from the Swing Controls
category.
- Move the JButton below of the Perimeter: JTextField
Generated
codes
Steps for designing the GUI
6. Running your App
To run your RectangleAppGUI program:
-Right Click your mouse in the generated codes area & select “Run File”
Steps for designing the GUI
6. Running your App
You can see your GUI when the program runs: