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 Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
User Interfaces II GUI – Awt, Swing, Web
14 Copyright © 2005, Oracle. All rights reserved. User Interface Design: Swing Basics Planning the Application Layout.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 22 GUI Programming I.
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 program.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Copyright 2010 by Pearson Education Building Java Programs More Graphics reading: Supplement 3G.
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Graphical User Interfaces
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
Addition 1’s to 20.
Import javax.swing.*; public class FrameTest { public static void main(String args[]) { JFrame f = new JFrame("Frame Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLO.
Web Design & Development Lecture 19. Java Graphics 2.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
CS18000: Problem Solving and Object-Oriented Programming.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Corresponds with Chapter 12
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”
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
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 
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
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.
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.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Dale Roberts GUI Programming using Java - Windowing Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Java Swing.
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
Introduction to Graphical Interface Programming in Java
תכנות מכוון עצמים בשפת JAVA
Steps to Creating a GUI Interface
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 based on objects These components will serve as a foundation for programs with graphical user interfaces (GUIs) The important point to learn is that an object in your program corresponds with a real world object We will start by 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 with a frame object JFrame frame = new JFrame("HelloWorld Using Swing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label to the frame object JLabel label = new JLabel("Hello World"); frame.add(label); //Display the window using frame methods 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 a top level container or another panel 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 or an image A GUI component must be added to another container such as a frame or panel to be displayed

8 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 145)Authority.java This program is not interactive, but the frame can be repositioned and resized

9 Authority JLabel label1 JLabel label2 JPanel primary JFrame frame

10 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));

11 Authority.java JLabel label1 = new JLabel ("Question authority,"); JLabel label2 = new JLabel ("but raise your hand first."); primary.add (label1);// add the labels to the panel primary.add (label2); frame.add(primary); // add the panel to the frame frame.pack(); // set frame size based on contents frame.setVisible(true); }

12 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 )NestedPanels.java

13 Nested Panels JPanel panel1 JLabel label1JLabel label2JLabel label3

14 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);

15 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); }

16 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

17 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 151)LabelDemo.java

18 LabelDemo

19 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);

20 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); }