Basics of GUI Programming Chapter 11 and Chapter 22.

Slides:



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

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.
Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
Corresponds with Chapter 12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
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.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
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,
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
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.
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.
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.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
MIT-AITI 2004 – Lecture 16 Introduction to Swing.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
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.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Graphical User Interfaces
Java Swing.
Chapter 13: Advanced GUIs and Graphics
Timer class and inner classes
IFS410: Advanced Analysis and Design
GUI and Event-Handling Programming
Chapter 12 GUI Basics.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Basics of GUI Programming Chapter 11 and Chapter 22

GUI Components GUIs are built from GUI components. GUI component is an object with which the user interacts via--- – Mouse – Keyboard – Voice – Etc;

Simple GUI Input/Output Java’s JOptionPane in javax.swing – showMessageDialog static method – showInputDialog static method – Used for very simple input and output

GUI Components Two sets of GUI components in Java – Swing (in package javax.swing) – AWT ( in package java.awt) AWT – components are automatically mapped to the platform- specific components – Tied to the local platform – Prone to platform-specific bugs – Referred to as heavyweight components – Eventually fade away

GUI Components Swing – More robust, versatile, and flexible than AWT components – Less dependent on the target platform – Referred to as lightweight components

Classification of GUI classes Container classes Component classes Helper classes

Container Classes Component classes that are used as containers to contain other GUI components – JFrame Window not contained inside another window Container that holds other Swing components JFrame object is a window – has a border, sizing buttons – JPanel Invisible container that holds user-interface components Can be used as a canvas to draw graphics Can be nested – JApplet

Java GUI API Object Component Color Graphics Container Window Panel JComponent Frame JFrame Applet JApplet Font JPanel

GUI Component Classes JButton JTextField JTextArea JComboBox JList JRadioButton JMenu

GUI Helper Classes Used by components and containers to draw and place objects – Graphics Abstract class for drawing strings, lines and simple shapes – Color Specifies colors of GUI components – Font Specifies fonts for the text and drawings on GUI components

GUI Helper Classes LayoutManager – Interface that specifies how components are arranged in a container

Layout Managers Java GUI components are placed in containers, where they are arranged by the container’s layout manager. Layout manager places components in the correct locations in the containers

Layout Managers - Examples FlowLayout – simplest layout manager – Components are arranged in the container from left to right, top to bottom in the order in which they were added – Default manager BorderLayout – Has 5 regions NORTH (top), SOUTH (bottom), EAST (right side), WEST (left side) and CENTER

JFrame methods To create a user interface, you need to create either a frame or an applet to hold user- interface components. JFrame() – constructs an untitled JFrame object JFrame(String ) – constructs a JFrame object with the specified title

JFrame methods void setDefaultCloseOperation (int operation) Operation tells the program what to do when the frame is closed. Possible values are: DO_NOTHING_ON_CLOSE HIDE_ON_CLOSE DISPOSE_ON_CLOSE EXIT_ON_CLOSE

JFrame methods setSize (int width, int height) – Specified in pixels – Defined in Component class setTitle (String title) dispose() – eliminates calling frame and all subcomponents setVisible (boolean value) – Defined in Component class

JFrame Concepts JFrame will display only when setVisible is set to true If setSize() is not used, frame will be 0x0; nothing will be seen but the title bar. If setDefaultCloseOperation is not used, the program does not terminate and user must break at the DOS prompt (windows)

JFrame Example import javax.swing.JFrame; public class MyFrame { public static void main (String args[]) { JFrame frame = new JFrame (“My Frame”); frame.setSize (400,300); frame.setVisible (true); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }

MyFrame output

JFrame Concepts JFrame object can have components added – Components must be added to the content pane of the JFrame – Think of a content pane as the “inside” of the JFrame – In Java, you can add components directly to the frame and it automatically adds them to the content pane. Example: add(component); – Older versions: getContentPane().add (component);

Adding Components - JLabel JLabel – uneditable text 1. Constructors public JLabel() public JLabel (String text) public JLabel (Icon icon) public JLabel (String text, Icon icon, int horizontalAlignment) 2. setIcon(Icon b) Image b = new ImageIcon (“cat.gif”); – Label1.setIcon(b);

Adding Components - JLabel 1..setHorizontalPosition (SwingConstants.CENTER); 2..setVerticalTextPosition (SwingConstants.BOTTOM); 3..setToolTipText(“This is a label”);

JTextField Enables user to enter data from the keyboard Also can be used to display editable or un- editable text

Example Design the GUI for a “guessing” game. User should be asked to guess a number between 1 and 10. Points are deducted each time the user guesses the wrong number.

Java application for GuessGame import javax.swing.JFrame; public class TestGuessGame { public static void main (String args[]) { GuessGame aGame = new GuessGame(); aGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aGame.setSize (200,200); aGame.setVisible (true); }

Output (using FlowLayout)

The GuessGame file GuessGame.java

End (Part 1) JFrame FlowLayout BorderLayout