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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
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.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Graphical User Interfaces
ITEC220 GUI Lecture – Part 1 References  Java Software Solutions,” by Lewis & Loftus  Chapter 3  Chapter 4  Chapter 5  Chapter 6  Java Foundations-Introduction.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
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 Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
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.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Sadegh Aliakbary Sharif University of Technology Fall 2011.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
1 Lecture 8: User Interface Components with Swing.
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 First Look at GUI Applications
Processing Timer Events
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Timer class and inner classes
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
TA: Nouf Al-Harbi NoufNaief.net :::
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

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

2 Graphical Applications The example programs we've explored thus far have been text-based command-line applications, which interact with the user using text prompts Let's examine some Java applications that have graphical components These components will serve as a foundation to programs with true graphical user interfaces (GUIs) We will start with generating a frame with panels containing text “labels” or images

3 Graphical Applications GUI-related classes are defined primarily in java.awt and javax.swing packages The Abstract Windowing Toolkit (AWT) was the original Java GUI package The Swing package provides additional and more versatile components Sometimes called Java Foundation Classes (mimicking Microsoft Foundation Classes)

4 GUI Containers - Frame A GUI container is a component that is used to hold and organize other components JFrame, JDialog, and JApplet are the three top level containers that are used to display graphics in GUI applications We will work only with JFrame for now A JFrame is displayed as a separate window with a title bar – it can be repositioned and resized on the screen as needed

5 Frame-based Hello World JFrame frame attribute title JFrame frame JLabel label JFrame frame width = 300 JFrame frame height = 200

6 Frame-based Hello World import javax.swing.*; // Get JFrame and JLabel classes public class HelloWorld { public static void main(String[] args) { //Create and set up the window. JFrame frame = new JFrame("HelloWorld Using Swing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.add(label); //Display the window. frame.setSize(300,200); // width and height frame.setVisible(true); }

7 GUI Panels / Components A panel is a container that cannot be displayed on its own It must be added to another container to be displayed It is used to organize other components A GUI component is an object that represents a screen element such as a text field, image, radio buttons, check boxes and so on. A GUI component must be added to a container such as a panel or frame to be displayed

8 GUI Containers A GUI container can be classified as either heavyweight or lightweight A heavyweight container is one that is managed by the underlying operating system A lightweight container is managed by the Java program itself Occasionally this distinction is important A frame is a heavyweight container and a panel is a lightweight container

9 Labels A label is a GUI component that displays a line of text Labels are usually used to display information or identify other components in the display Let's look at a program that organizes two labels in a panel and displays that panel in a frame See Authority.java (page 144)Authority.java This program is not interactive, but the frame can be repositioned and resized

10 Authority JLabel label1 JLabel label2 JPanel primary JFrame frame

11 Authority.java import java.awt.*; import javax.swing.*; public class Authority { public static void main (String[] args) { JFrame frame = new JFrame ("Authority"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel primary = new JPanel(); primary.setBackground (Color.yellow); primary.setPreferredSize (new Dimension(250, 75));

12 Authority.java JLabel label1 = new JLabel ("Question authority,"); JLabel label2 = new JLabel ("but raise your hand first."); primary.add (label1); primary.add (label2); frame.add(primary); frame.pack(); frame.setVisible(true); }

13 Nested Panels Containers that contain other components make up the containment hierarchy of an interface This hierarchy can be as intricate as needed to create the visual effect desired The following example nests two panels inside a third panel – note the effect this has as the frame is resized See NestedPanels.java (page 146)NestedPanels.java

14 Nested Panels JPanel panel1 JLabel label1JLabel label2JLabel label3

15 NestedPanels.java import java.awt.*; import javax.swing.*; public class NestedPanels { public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Set up first subpanel JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); JLabel label1 = new JLabel ("One"); subPanel1.add (label1);

16 NestedPanels.java // Set up second subpanel JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); // Set up primary panel JPanel primary = new JPanel(); primary.setBackground (Color.yellow); primary.add (subPanel1); primary.add (subPanel2); JLabel label3 = new JLabel ("Buckle my shoe..."); primary.add (label3); frame.add(primary); frame.pack(); frame.setVisible(true); }

17 Images Images are often used in a programs with a graphical interface Java can manage images in both JPEG and GIF formats As we've seen, a JLabel object can be used to display a line of text It can also be used to display an image That is, a label can be composed of text, and image, or both at the same time

18 Images The ImageIcon class is used to represent an image that is stored in a label The position of the text relative to the image can be set explicitly The alignment of the text and image within the label can be set as well See LabelDemo.java (page 149)LabelDemo.java

19 LabelDemo

20 LabelDemo.java import java.awt.*; import javax.swing.*; public class LabelDemo { public static void main (String[] args) { JFrame frame = new JFrame ("Label Demo"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); ImageIcon icon = new ImageIcon ("devil.gif"); JLabel label1, label2, label3; label1 = new JLabel ("Devil Left", icon, SwingConstants.CENTER); label2 = new JLabel ("Devil Right", icon, SwingConstants.CENTER); label2.setHorizontalTextPosition (SwingConstants.LEFT); label2.setVerticalTextPosition (SwingConstants.BOTTOM);

21 LabelDemo.java label3 = new JLabel ("Devil Above", icon, SwingConstants.CENTER); label3.setHorizontalTextPosition (SwingConstants.CENTER); label3.setVerticalTextPosition (SwingConstants.BOTTOM); JPanel panel = new JPanel(); panel.setBackground (Color.cyan); panel.setPreferredSize (new Dimension (200, 250)); panel.add (label1); panel.add (label2); panel.add (label3); frame.add(panel); frame.pack(); frame.setVisible(true); }

22 Graphical User Interfaces At least three kinds of objects are needed to create a GUI: –Components – an object that defines a screen element to display information. Containers is a special type of component that is used to hold and organize other components. –Events – an event is an object that represents some occurrence in which we may be interested. For e.g. Press of a mouse button or typing a key on keyboard.

23 Graphical User Interfaces Listeners – a listener is an object that “waits’ for an event to occur and responds in some way when it does. We must carefully establish the relationships among the listener, the event it listens for, and the component that will generate the event.

24 Graphical User Interfaces Every event handler requires three pieces of code: In the declaration for the event handler class, one line of code specifies that the class implements a listener interface. For example: public class MyClass extends JFrame implements ActionListener { Another line of code registers an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(this); The event handler class has code that implements the methods in the listener interface. For example: public void actionPerformed(ActionEvent e) {... //code that reacts to the action... }

25 Buttons Lets write a class for following output. Push Counter Push me Pushes: 2

26 TextFields Lets write a class for following output. Calculator Add 3 2 First Number Second Number SubtractProduct Result: 5Result: 1Result: 6