Lecture 12 - Graphical User Interface
Lecture 12 - Graphical User Interface
A user interface is the medium that a user uses while interacting with a system. The
programs we have done so far are called Console Applications as Console is the
interface that the user is interacting with. User is giving input in the console and also
getting the output in the console.
A user interface that uses different graphical components for users to interact with
the system is known as a Graphical User Interface. Example:
If GUI was not here, using a computer would have become boring and irritating.
Graphical User Interface
Components of GUI
Label
TextField
PasswordFiel
d
Button
Frame
Graphical User Interface
Illustration of GUI Components
Radio Buttons
Check Boxes
Graphical User Interface
Java library classes for making a GUI
All these GUI components can be Name of Component Java Library Class Name
created using java library classes. Label JLabel
These classes are inside javax.swing TextField JTextField
package. PasswordField JPasswordField
Button JButton
Radio Button JRadioButton
Check Box JCheckBox
Button Group ButtonGroup
Drop Down List JComboBox
Text Area JTextArea
Image ImageIcon
Table JTable
Panel JPanel
Frame JFrame
Graphical User Interface
Customizing Color and Font
Color and Font of GUI components Both of these classes are in java.awt package.
can be customized/changed by These classes have constructors to create color
using the following library classes: and font according to user preference. These
user defined colors and fonts can be used for
• Color GUI components by calling library methods of
• Font their respective classes.
Constructors and Methods Description
Color(int r, int g, int b) Creates a color having the RGB value as per the parameters
Changes the background color as per the Color object passed in the
setBackground(Color c)
parameter
setForeground(Color c) Changes the font color as per the Color object passed in the parameter
Creates a font having the name, style and size as per the parameter
Font(String name, int style, int size)
values
setFont(Font f) Changes the font as per the Font object passed in the parameter
Graphical User Interface
Different Event Listeners
• ActonListener All of these listeners are java library interfaces. They are
• MouseListener inside the java.awt.event package. Each type of listener
• KeyListener handles their own type of event.
• FocusListener
The followings are the abstract methods that these interfaces have:
MouseListener KeyListener
mouseClicked(MouseEvent me) keyTyped(KeyEvent ke)
mousePressed(MouseEvent me) keyPressed(KeyEvent ke)
mouseReleased(MouseEvent me) keyReleased(KeyEvent ke)
mouseEntered(MouseEvent me)
mouseExited(MouseEvent me) FocusListener
focusGained(FocusEvent fe)
ActionListener focusLost(FocusEvent fe)
actionPerformed(ActionEvent ae)
Graphical User Interface
Steps to Design a GUI
We need to write a class to develop a GUI. The object of our class will be the GUI. It will
be easier to develop a GUI, if we do the followings first:
• Draw a sketch of the GUI in a paper.
• Make a list of the components of the GUI.
• Make a list of color and fonts that the components will have (optional).
• Make a list of events that might happen in the GUI.
The followings steps should be followed while writing the class:
Step 0: Import Necessary library classes.
Step 1: Our class needs to extend the JFrame class and implement necessary listeners.
Step 2: The components used in the GUI will be the attributes of our class.
Step 3: The colors and fonts used in the GUI should also be in the attributes (optional).
Step 4: Write a constructor for the class. The body of the constructor can be divided
into several parts:
Graphical User Interface
Steps to Design a GUI
Example
In Code
Books