1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
GUI Programming in Java
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Java Programming Chapter 10 Graphical User Interfaces.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Basics of GUI Programming Chapter 11 and Chapter 22.
Java Swing One of the most important features of Java is its ability to draw graphics.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
A Quick Java Swing Tutorial
A First Look at GUI Applications
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Chapter 12 GUI Basics.
Graphical User Interface
Presentation transcript:

1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers Programming buttons Other GUI Components

2 Introduction We want to communicate with the user through a Graphical User Interface (GUI), rather than using a text-based interface.

3 GUIs written in Java Nothing to do with Java interface keyword! Java provides two sets of facilities for writing GUIs: –the Abstract Window Toolkit (AWT) –Swing AWT is part of the core Java classes ( java.awt ), whereas Swing is part of the Java Foundation Classes ( javax.swing ). From a programmer’s perspective, Swing is an alternative to using the Java AWT, although technically Swing extends (but does not replace the AWT).

4 GUIs written in Java Java GUIs are platform independent as well. The graphical components (like windows) of different operating systems look and operate differently. However, the Java AWT and Swing facilities are the same across all platforms. Java maps its components onto components provided by the operating system it is running on. Same Java GUI can run on many platforms.

5 Understanding Events An event is defined as a: “happening, occurrence” Something happening somewhere. In GUIs, an event (usually) results from a user’s action. Eg. The user clicks the mouse on a button..

6 Understanding Events An event is defined as a: “happening, occurrence” Something happening somewhere. In GUIs, an event (usually) results from a user’s action. Eg. The user clicks the mouse on a button..

7 Understanding Events Text interface There is a set sequence of actions. The program stops when it expects input from the user and continues on the same set path after it receives the input. Graphical Interface The user can do any number of things (type in a text box, resize window, press a button). These actions by the user are called events. An event loop constantly checks for any new event and responds accordingly. Java’s event loop is hidden from the programmer. We say the program is event- driven. Note: Pressing the “Cancel” button... Note: Pressing the “Cancel” button...

8 How to write GUIs We write GUI programs in Java by creating instances of components or deriving new classes from the existing component classes. Eg. JButton b1 = new JButton("Button 1"); We construct one or more listeners which “listen” for an event and respond accordingly. To program a GUI we: 1. Decide on GUI components (buttons etc.) and their layout. [Today] 2. Write listeners to handle the events. [Tomorrow]

9 The Swing class that implements an independent window on the screen A first Swing Application import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame frame = new JFrame(“Swing Application”); frame.setVisible(true); }

10 The Swing class that implements an independent window on the screen A first Swing Application import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame frame = new JFrame(“Swing Application”); frame.setVisible(true); } Displays the JFrame and enters the event loop, waiting for events

11 A first Swing Application import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame frame = new JFrame(“Swing Application”); frame.setVisible(true); }

12 import java.awt.*; import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { Dimension d = new Dimension(300, 200); JFrame frame = new JFrame(“Swing Application”); frame.setSize( d ); frame.setVisible(true); } A first Swing Application

13 import java.awt.*; import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame frame = new JFrame(“Swing Application”); frame.setSize(new Dimension(300,200)); frame.setVisible(true); } A first Swing Application

14 Problem:. –When we click to close the window, the Java program keeps running! –Default behaviour is just to close window - nothing more. Solution: –When we click on, we need to exit our program. –Call setDefaultCloseOperation method with the static value of JFrame.EXIT_ON_CLOSE. A first Swing Application

15 Shutting down properly import java.awt.*; import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame frame = new JFrame(“Swing Application”); frame.setSize(new Dimension(300,200)); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible(true); }

16 The Top-Level Container Every GUI program contains at least one top-level Swing container (usually JFrame, JDialog, or JApplet ) Every top-level container indirectly contains an intermediate container known as a content pane. Components (such as buttons or text fields) are added to this content pane. To add a button to our frame: –create an instance of JButton –add it to the content pane of the frame –before you set the frame to be visible, call the pack() method.

17 Adding Components import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame f = new JFrame(“Swing Application”); f.getContentPane().add(new JButton(“Button 1”) ); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); f.pack(); f.setVisible(true); }

18 The Containment Hierarchy There are 3 main types of components: –top-level containers (JFrame, JDialog, JApplet) –intermediate containers (The purpose is to contain other components. Examples: JPanel, JTabbedPane) –atomic components (They do not contain other components. Examples: JButton, JLabel, JTextField, JComboBox, JList, JTable). For every Swing application, we can draw a containment hierarchy which shows how the components fit together.

19 Component Hierarchy The above GUI contains 4 components: –A frame, or main window (JFrame) –A panel (JPanel), also called a pane. –A button (JButton), which has “I’m a Swing button” on it. –A label (JLabel), showing the Number of button clicks. The frame is a top-level container, which provides a place for the components to paint themselves. The panel is an intermediate container, used to simplify the position of the button and label (which are atomic components). JFrame (with its own content pane) JPanel JButtonJLabel

20 Component Hierarchy import javax.swing.*; public class SimpleSwing { public static void main(String[] args) { JFrame f = new JFrame("Swing Application"); JPanel p = new JPanel(); p.add(new JLabel("Label 1") ); p.add(new JButton("Button 1") ); f.getContentPane().add( p ); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); f.pack(); f.setVisible(true); }

21 Component Hierarchy: Exercise JLabelJTextField JSlider ColorBlock (extends JLabel) JButton

22 Component Hierarchy: One Solution JSlider JButton (Reset) JPanel JFrame (with its own content pane) JPanelColorBlock JPanel (Red)JPanel (Blue)JPanel (Green) JLabelJTextFieldJSliderJLabelJTextField JSliderJLabelJTextField