GUI1 Slides
GUI1 Slides
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, …):
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
Button 1
JFRAME
JFrame
Button 1
JFRAME
JFrame
Button 1
Content Pane
JFRAME
JFrame
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
close the app when the window is closed with the red button / x button
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 :(
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
ActionEvent object
ACTIONPERFORMED()
ActionEvent object
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