Graphical User Interface (GUI)

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
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
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
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.
GUI programming Graphical user interface-based programming.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
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.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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.
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.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Java GUI.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Timer class and inner classes
Graphical user interface-based programming
Steps to Creating a GUI Interface
CiS 260: App Dev I Chapter 6: GUI and OOD.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Graphical User Interface (GUI) Java API

GUI Contains 1 label, 1 text box and 2 buttons Contains 3 buttons, 3 image icons , and 3 labels with text.

Window Objects A GUI contains many objects in a window. Classes for creating window objects are found in javax.swing package. JButton—clickable buttons JLabel—to display text and images JTextField—to input and output text JTextArea—for I/O of multiple lines of text JScrollBar—to implement scrollbar JCheckBox—for multiple selections of choices JRadioButton—for one selection out a set of choices

Container Class The swing objects—labels, buttons, etc.—are arranged within a container object. Panel (JPanel)—an intermediate container in which objects are arranged The intermediate container is then inserted into a top-level container Frame (JFrame)—a window with border, title, and button for closing Applet (JApplet)—container for a program to be embedded in a Web page

Developing GUI Application Developing an application that involves a GUI requires the following steps. Create a Frame by extending JFrame and “implementing” ActionListener Create GUI objects Choose objects to cause events and associate them with actionListener Create a panel Add objects to the panel Define actionPerformed method to be executed for each event* In the main() method: Instantiate a frame by extending JFrame Call SetdefaultCloseOperation Add panel to the frame Make the frame visible

Developing GUI Applet Developing an applet that involves a GUI requires the following steps. Create an Applet by extending JApplet and “implementing” ActionListener In the init() method: Create GUI objects Decide which objects will be responsible for events and associate them to actionListener Create a panel Add objects to the panel Insert panel into the applet Make applet visible Define actionPerformed method to be executed for each event*

Event Event—an action initiated outside the program, e.g., by the user, that can trigger the program to execute a particular code segment. Event Examples Clicking a button Hovering over a text box Sliding a scroll bar Striking a particular key on the keyboard Back

Completing Event Handlers Java Interface Like a class, but has no instance variables Contains abstract method—an outline of a method whose contents must be provided by the interface client To use Interface ActionListener import java.awt.event.*; class MyApplet extends Japplet implements ActionListener {

Completing Event Handlers (cont.) // Event handler for action events on buttons public void actionPerformed(ActionEvent event) Object object = event.getSource(); if (object == btnPush) { count++; lblResult.setText("Number of Pushes: " + count); } else if (object == btnReset) { count = 0; lblResult.setText("Number of Pushes: " + count); } }

Creating a Simple GUI Create the GUI above, which contains one label, one text box, and two buttons in a frame.

Developing a GUI Create a frame class by extending JFrame class. Create GUI’s individual elements. In the constructor Call the parent class’s constructor. Arrange GUI elements in a panel. Insert the panel into the frame. In the main() method, instantiate the frame.

Developing a GUI frame panel component txt fld 1 Label 2 txt fld 2

Java Code for a Simple GUI Here is the source code for creating the GUI. Note: public class GUIDemo extends JFrame declares GUIDemo as a subclass of JFrame Instantiation of GUI elements (c.f., Java API) super("GUI Demo"); calls the parent class’s constructor, which displays title in frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tells the program to end when window is closed.

Code (cont.) JPanel pane = new JPanel(); All elements must be arranged in an intermediate container before they can be inserted into the frame. setContentPane(pane); Insert container into the frame. setVisible(true); Make the frame visible.

Exercise Write a program that displays the interface shown below. Skip code

High-level Procedure import javax.swing.*; class MyInterface extends JFrame { // declare objects // constructor public MyInterface() { // prepare frame // instantiate components // create intermediate container // add components to container // insert pane into frame // make frame visible } public static void main(String[] args) { MyInterface demo = new MyInterface1(); } }

Code import javax.swing.*; class MyInterface extends JFrame { // declare objects JLabel lbFirstName; JLabel lbLastName; JLabel lbFirstTime; JTextField txFirstName; JTextField txLastName; JRadioButton rbFirstTime; JButton btStart; JButton btClear; // constructor public MyInterface1() { // prepare frame super("INTERFACE DEMO");

setSize(400, 200); setDefaultCloseOperation(JFrame setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // prepare components lbFirstName = new JLabel("Enter your first name.") lbLastName = new JLabel("Enter your last name."); lbFirstTime = new JLabel("Check if this your first time?"); txFirstName = new JTextField(20); txLastName = new JTextField(20); rbFirstTime = new JRadioButton("First Time?"); btStart = new JButton("Start"); btClear = new JButton("Clear"); // intermediate container JPanel pane = new JPanel(); // add components to container pane.add(lbFirstName); pane.add(txFirstName);

pane. add(lbLastName); pane. add(txLastName); pane pane.add(lbLastName); pane.add(txLastName); pane.add(lbFirstTime); pane.add(rbFirstTime); pane.add(btStart); pane.add(btClear); // insert pane into frame setContentPane(pane); // make frame visible setVisible(true); } // Interface public static void main(String[] arguments) { MyInterface1 demo = new MyInterface1(); } // main // Interface To GUI

Layout Managers Java uses layout manager classes to control arrangement of GUI elements. FlowLayout (default) GridLayout BorderLayout GridBagLayout CardLayout

Flow Layout This is the default scheme. Elements are inserted in a container in order, moving to the next row as needed. Click image to see source code

Flow Layout (cont.) In the source code, note: Declarations of objects are at the class level, but instantiations are local, inside the constructor. This makes the objects visible to all methods, but details of object formatting are deferred until the constructor is called. gui.show(); in the main() makes the frame visible. Same as setVisible(true) inside the constructor.

Grid Layout Elements are inserted in particular cells an N x M matrix Click image to see source code

Grid Layout (cont.) In the source code note that a GridLayout object grid is created, and the it is associated with the JPanel object panel. JPanel panel = new JPanel(); GridLayout grid = new GridLayout(3, 3); // create layout manager panel.setLayout(grid);

Border Layout Elements are inserted at North, South, East, West, and Center positions Click image to see source code