0% found this document useful (0 votes)
15 views

GUI1 Slides

This document provides an overview of Java GUIs, focusing on Swing components, layout managers, and basic GUI structure. It discusses the evolution of Java GUI libraries including AWT and Swing, and introduces key components like JFrame, JButton, and JTextField. Additionally, it covers the anatomy of a simple GUI application and the functionality of layout managers in organizing components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

GUI1 Slides

This document provides an overview of Java GUIs, focusing on Swing components, layout managers, and basic GUI structure. It discusses the evolution of Java GUI libraries including AWT and Swing, and introduces key components like JFrame, JButton, and JTextField. Additionally, it covers the anatomy of a simple GUI application and the functionality of layout managers in organizing components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
Overview
THIS WEEK’S LECTURE
•Introduction to GUIs in Java
•Overview of Swing
•Layout Managers
•JButton
•JTextField
WHAT IS A GUI
- Graphical User Interface
- A pictorial representation of the computer system that users can interact with through
controller hardware such as; keyboard, pointing devices and touchscreens.
- We are focusing on desktop (windows, icons, mouse pointers)
JAVA AWT (ADVANCED WINDOWING TOOLKIT)
- Came bundled with Java 1.1
- Proved insu cient for creating e cient desktop applications
- Utilises native components (heavyweight)
- A new look and feel for each OS
- Led to interoperability problems
- AWT hasn’t been superseded but complemented by the Swing library since Java1.2
ffi
ffi
SWING OVERVIEW
- Lightweight components. Written in Java on top of AWT
- Use of components is standardised by Java so they operate in an expected way
- Customisable look and feel
JAVA GUI HISTORY
Version AWT Swing Applets JavaFX*
Java 1.1
Java 1.2
Java 1.3
Java 1.4
Java 5
Java 6
Java 7
Java 8
Java 9
Java 10
Java 11
Java 12
Java 13
Java 14
Java 15
* JavaFX not included as part of the JDK since Java
11 but still current and downloadable separately
JAVA GUI HISTORY
Version AWT Swing Applets JavaFX*
Java 1.1
Java 1.2
Java 1.3 Example Applet

Java 1.4
Java 5
Java 6
Java 7
Java 8
Java 9
Java 10
Java 11
Java 12
Java 13
Java 14
Java 15
* JavaFX not included as part of the JDK since Java
11 but still current and downloadable separately
JAVA GUI HISTORY
Version AWT Swing Applets JavaFX*
Java 1.1
Java 1.2
Java 1.3 Java FX Example

Java 1.4
Java 5
Java 6
Java 7
Java 8
Java 9
Java 10
Java 11
Java 12
Java 13
Java 14
Java 15
* JavaFX not included as part of the JDK since Java
11 but still current and downloadable separately
Not included here (SWT, Android, GWT, …):

JAVA GUI HISTORY


Version AWT Swing Applets JavaFX*
Java 1.1
Java 1.2
Java 1.3
Java 1.4
Java 5
Java 6
Java 7
Java 8
Java 9
Java 10
Java 11
Java 12
Java 13
Java 14
Java 15
* JavaFX not included as part of the JDK since Java
11 but still current and downloadable separately
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
Anatomy of a JFrame
COMPONENTS AND CONTAINERS
- Swing GUI’s consist of components and containers
- Containers hold a group components and inner containers
- Components include:
- JLabel
- JTextField
- JButton
- Containers include:
- JFrame
- JPanel
- JOptionPane
- Collection of containers and components for a particular window is called a containment
hierarchy
JFRAME

Hello, I’m a Label

Button 1
JFRAME
JFrame

Hello, I’m a Label

Button 1
JFRAME
JFrame

Hello, I’m a Label

Button 1

Content Pane
JFRAME
JFrame

Hello, I’m a Label JLabel

Button 1 JButton
Content Pane
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
First Example
FIRST GUI EXAMPLE
ANATOMY OF A SIMPLE GUI APP
create a new JFrame, the argument is it’s title
ANATOMY OF A SIMPLE GUI APP

set it’s size on x and y axises


ANATOMY OF A SIMPLE GUI APP

close the app when the window is closed with the red button / x button
ANATOMY OF A SIMPLE GUI APP

Create a new JLabel, the text is provided as a parameter


ANATOMY OF A SIMPLE GUI APP

Add the label to the frame


ANATOMY OF A SIMPLE GUI APP

Set the frame to display


ANATOMY OF A SIMPLE GUI APP

To use the window we need it to run on another thread, swing provides a helper
function called invoke later pass a new runnable object to it that creates a new HelloGUI
object
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
Layout Managers
LAYOUT MANAGERS
- Controls the position of the components within the container.
- Mostly provided by AWT.
- implement the LayoutManager interface
- A tricky topic :(

A simple layout that positions components left-to-right, top-to-


FlowLayout
bottom.
Positions components within the centre or borders of the
BorderLayout
container. This is the default LayoutManager
GridLayout Lays out components within a grid

GridBagLayout Lays out di erent size components within a exible grid

BoxLayout Lays out components vertically or horizontally in a grid


Lays out components using constraints (mostly for interface builder
SpringLayout
tools.)
ff
fl
LAYOUT MANAGERS EXAMPLE
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
JButtons
JBUTTON
- Component for creating a push button control.
- Can contain a string, image or both.
- When the button is pushed it generates an ActionEvent
- We can listen to this event by adding an action listener
- al is an instance of a class that implements ActionListener
- Classes that implement ActionListener need to include the actionPerformed()
method

void addActionListener(ActionListener al)


ACTIONPERFORMED()
ACTIONPERFORMED()

ActionEvent object
ACTIONPERFORMED()

ActionEvent object

Use get ActionCommand to identify which control cause the action


N!
WHAT DAY ISSOIT?
O
DR EDWARD ANSTEAD

EXTENDED JAVA
GUIs Part 1
JTextField
JTEXTFIELD
- Use JTextField to get short input from the users keyboard
- A single line of input such as a user name, search string, URL
JTEXTFIELD
- Use JTextField to get short input from the users keyboard
- A single line of input such as a user name, search string, URL

Set the visible length of the text eld (doesn’t limit


enterable characters.)
fi
JTEXTFIELD
- Use JTextField to get short input from the users keyboard
- A single line of input such as a user name, search string, URL

give the eld an actionCommand for


searching in the actionPerformed method
fi
JTEXTFIELD EXAMPLE

You might also like