Getting Started with GUI Programming Chapter 10 CSCI 1302.

Slides:



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

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.
Corresponds with Chapter 12
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 13 GUI Basics.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
1 GUI Basics First three slides are repeated from previous lecture notes! You will probably (in fact should) never write Java codes like the program examples.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
Implementing GUIs in Java ● The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs: – Abstract Window Toolkit (AWT): native.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 11 Getting Started.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 11 Getting Started with GUI Programming 百闻不如一见.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and.
1 Chapter 10 Getting Started with Graphics Programming F Graphics Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
GUI Basics.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 2 Getting Started with GUI Programming.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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:
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Chapter 7 A First Look at GUI Applications Layout Managers.
Chapter 8 Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames  Creating & centering frames, adding.
Abstract Window Toolkit (AWT): native GUI components
Reference: COS240 Syllabus
Swing JComponents.
Modern Programming Language Java
Tim McKenna Layout Mangers in Java Tim McKenna
Chapter 13: Advanced GUIs and Graphics
Chapter 10 Getting Started with Graphics Programming
Chapter 12 GUI Basics.
Chapter 12 GUI Basics.
Advanced GUIs and Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Getting Started with GUI Programming Chapter 10 CSCI 1302

CSCI 1302 – Getting Started with GUI Programming2 Outline Introduction GUI Components –Swing vs. AWT The Java GUI API –Swing GUI Components –Container Classes –GUI Helper Classes Frames –Creating a Frame

CSCI 1302 – Getting Started with GUI Programming3 Outline Frames (cont.) –Adding Components to a Frame –Centering a Frame Layout Managers – FlowLayout – GridLayout – BorderLayout The Color Class The Font Class

CSCI 1302 – Getting Started with GUI Programming4 Outline Using Panels as Subcontainers Drawing Graphics on Panels –Drawing Lines –Drawing Rectangles –Drawing Ovals –Drawing Arcs –The Polygon class and Drawing Polygons and Polylines Centering a Display Using the FontMetrics Class

CSCI 1302 – Getting Started with GUI Programming5 Outline Case Studies –The MessagePanel Class –The StillClock Class

CSCI 1302 – Getting Started with GUI Programming6 Introduction Dialog boxes and command window for input and output Learn to create custom graphical user interfaces (GUI) to obtain and display input and output Learn the basics of GUI programming

CSCI 1302 – Getting Started with GUI Programming7 GUI Components Each GUI component is defined in a class that provides several constructors See DisplayGUI.java

CSCI 1302 – Getting Started with GUI Programming8 Swing vs. AWT All GUI classes were bundled in a library called the Abstract Windows Toolkit (AWT) Automatically maps AWT components to platform-specific components Java 2 added Swing components which are painted directly on canvases Less dependent on the target platform, known as lightweight, always prefixed with a J

CSCI 1302 – Getting Started with GUI Programming9 The Java GUI API

CSCI 1302 – Getting Started with GUI Programming10 The Java GUI API

CSCI 1302 – Getting Started with GUI Programming11 The Java GUI API GUI classes can be classified into three groups: –Component classes – actual GUI components, subclasses of JComponent –Helper classes – used to draw and place objects –Container classes – contain other components

CSCI 1302 – Getting Started with GUI Programming12 Swing GUI Components All user-interface classes have a superclass of Component All lightweight components descend from JComponent Remember that all subclasses can invoke methods of their superclasses Methods like getHeight and getWidth are defined in Component but available to all subclasses

CSCI 1302 – Getting Started with GUI Programming13 Container Classes Used as containers to hold other GUI components Container – Used to group components JFrame – Window not contained in another window, holds Swing components JDialog – Popup window or message box temporarily used to deliver information JApplet – Used to create applets

CSCI 1302 – Getting Started with GUI Programming14 Container Classes JPanel – Invisible container that holds UI components –Can be nested –Can be placed inside other panels –Can be used as a canvas to draw graphics

CSCI 1302 – Getting Started with GUI Programming15 GUI Helper Classes Used to describe properties of GUI components such as graphics context, colors, fonts, and dimension Graphics – Abstract class that can draw strings, lines, and simple shapes Color – Provides color to components Font – Specifies fonts for text and drawings on components FontMetrics – Properties of fonts

CSCI 1302 – Getting Started with GUI Programming16 GUI Helper Classes Dimension – Encapsulates the width and height of a component in an object LayoutManager – Interface, specifies how components are arranged in a container

CSCI 1302 – Getting Started with GUI Programming17 Frames When creating GUI applications, something must be used to hold components Either frames or applets can be used to hold these components

CSCI 1302 – Getting Started with GUI Programming18 Creating a Frame import javax.swing.*; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("MyFrame"); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } See MyFrame.java

CSCI 1302 – Getting Started with GUI Programming19 Creating a Frame

CSCI 1302 – Getting Started with GUI Programming20 Creating a Frame Must import javax.swing.* so that JFrame will be available Create a JFrame with a set title Specify a pixel width and height for the frame Display the frame with setVisible(true) Last line sets the operation to be performed on closing the frame, must be done to terminate the program

CSCI 1302 – Getting Started with GUI Programming21 Adding Components import javax.swing.*; public class MyFrameWithComponents { public static void main(String[] args) { JFrame frame = new JFrame("Adding Components into the Frame"); // Add a button java.awt.Container container = frame.getContentPane(); JButton jbtOK = new JButton("OK"); container.add(jbtOK); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

CSCI 1302 – Getting Started with GUI Programming22 Adding Components

CSCI 1302 – Getting Started with GUI Programming23 Adding Components GUI components are placed in the content pane, returned by getContentPane Content panes are created when JFrame objects are created Components are added with the add method, removed with the remove method

CSCI 1302 – Getting Started with GUI Programming24 Layout Managers In many systems, each component is absolutely placed Java components are placed in containers which have layout managers Use the setLayout(LayoutManager) method to set a layout manager for a container LayoutManager lm = new XLayout(); container.setLayout(lm); FlowLayout, GridLayout, BorderLayout

CSCI 1302 – Getting Started with GUI Programming25 FlowLayout Simplest layout manager Components are arranged from left to right in the order that they are added When a row fills up, a new one is started Can set overall alignment with three constants: –FlowLayout.RIGHT –FlowLayout.CENTER –FlowLayout.LEFT

CSCI 1302 – Getting Started with GUI Programming26 FlowLayout Three constructors –public FlowLayout(int align, int hGap, int vGap) –public FlowLayout(int alignment) –public FlowLayout Default alignment is FlowLayout.CENTER and default vGap and hGap is 5 pixels

CSCI 1302 – Getting Started with GUI Programming27 FlowLayout See ShowFlowLayout.java

CSCI 1302 – Getting Started with GUI Programming28 GridLayout Arranges components in a grid Components are placed from left to right in the order that they are added User can specify number of rows and columns –Both cannot be zero –Rows has precedence over columns, will dynamically add columns to keep the row requirement fixed

CSCI 1302 – Getting Started with GUI Programming29 GridLayout Three constructors – public GridLayout(int rows, int cols, int hGap, int vGap) – public GridLayout(int rows, int cols) – public GridLayout() Default gaps of zero

CSCI 1302 – Getting Started with GUI Programming30 GridLayout See ShowGridLayout.java

CSCI 1302 – Getting Started with GUI Programming31 BorderLayout Divides the window into five areas, East, South, West, North, and Center Components are added using constants like BorderLayout.CENTER Two constructors – public BorderLayout(int hGap, int vGap) – public BorderLayout() North and South can stretch horizontally, East and West vertically

CSCI 1302 – Getting Started with GUI Programming32 BorderLayout See BorderLayout.java

CSCI 1302 – Getting Started with GUI Programming33 The Color class Java uses RGB (red, green, blue) model 3-tuple, 0 (darkest) – 255 (lightest) 255, 0, 0 is red; 0, 255, 0 is green public Color(int r, int g, int b) Use setForeground(Color c) and setBackground(Color c) to set a component’s colors Constants for thirteen standard colors See ShowColor.java

CSCI 1302 – Getting Started with GUI Programming34 The Font class Create fonts with java.awt.Font Set the fonts for components using the setFont method public Font(String name, int style, int size) name can be SansSerif, Serif, Monospaced, Dialog, or DialogInput style can be Font.PLAIN (0), Font.BOLD (2), Font.ITALIC (2), or Font.BOLD + Font.ITALIC (3)

CSCI 1302 – Getting Started with GUI Programming35 Using Panels as Subcontainers Single containers are not conducive to good UI Panels can act as subcontainers to apply different layouts to different sections of the application Panels hold groups of components, then the panels are placed on frames Default layout manager for a JPanel is FlowLayout

CSCI 1302 – Getting Started with GUI Programming36 Using Panels as Subcontainers Create a panel and add a button JPanel p = new JPanel(); p.add(new JButton(“OK”)); Add the panel to a frame JFrame f = new JFrame(); f.getContentPane().add(p); Use this method to arrange components into logical groups with different layouts

CSCI 1302 – Getting Started with GUI Programming37 Microwave Using panels as subcontainers, emulate the front of a microwave Design with layout managers in mind