Java GUI.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Graphic User Interfaces Layout Managers Event Handling.
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
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
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
Java Programming Chapter 10 Graphical User Interfaces.
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,
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.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Review_6 AWT, Swing, ActionListener, and Graphics.
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.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Developing GUIs With the Eclipse Visual Editor, Swing/AWT Edition David Gallardo.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Savings Account From Learn Java GUI.
Chapter 9: Graphical User Interfaces
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Provision for GUIs in Java
Graphical User Interface (pronounced "gooey")
Graphical user interface with Swing by jose maria gonzalez pinto
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
GUIS: Graphical User Interfaces
CiS 260: App Dev I Chapter 6: GUI and OOD.
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java GUI

Structure of a GUI App A GUI app consists of a frame controls code This app is made of: Frame – window Controls – such as buttons and labels Properties – such as size, color Methods – built in procedures related to properties Event Methods – such as clicking General Methods – code not related to objects

Steps in Building a GUI Create the frame. Create the user interface by placing controls on the frame. Write code for control event methods, and perhaps other methods. We will use the java Swing and AWT components, so import javax.swing.*; import java.awt.*;

Testing The event-driven nature of Java allows you to build your app in stages and test it at each stage. Build one method, or part of a method, and get it working before moving on. This minimizes errors and increases your confidence. Build a little, test a little, modify a little, and test again!

Swing Controls These all have names beginning with J JFrame JButton basic container for other controls title property establishes caption JButton used to start some action text property establishes the caption JLabel allows placement of formatted text on a frame text property establishes the caption

Swing Controls, continued JTextField accepts a single line of typed information JTextArea accepts multiple lines of scrollable typed info JCheckBox provides yes or no answer JRadioButton allows selection from a mutually exclusive group JComboBox users can choose from a drop down list

Swing Controls, continued JList like a combo box with the list always visible allows multiple selections JScroll a scroll bar control used to select from a range of values always buddied with another related control

Creating a Frame This is the first step in building a GUI app The following is an example from the Stopwatch project

Extending the JFrame means that this Stopwatch object takes on all characteristics of such a frame. This is the constructor. It has the same name as the class. It is called from the main method. These are methods.

User Interface Once the frame is created, we ‘place’ controls in the frame. The layout manager determines how controls are arranged. There are several to choose from. The GridBagLayout offers a nice interface appearance by placing controls into a rectangular frame like a table or a 2D array.

User Interface, cont. A frame contains several panes. Controls are placed into the content pane. The GridBagLayout manager divides the content pane into rows and columns.

Placing a Control into a GridBag Layout Declare the control Create the control Set the desired control properties Add the control to the desired position We will declare all the controls with class level scope so that any method in the class may use them.

JButton startButton = new JButton(); Adding Controls Declare the control ControlType controlName; For ex, JButton startButton; Create the control controlName = new ControlType(); For ex, startButton = new JButton(); You may combine steps 1 & 2: JButton startButton = new JButton();

startButton.setText(“Start Timing”); Adding Controls, cont. Set the desired control properties controlName.setPropertyName(PropertyValue); For ex, startButton.setText(“Start Timing”);

Adding Controls, cont. Add the control to the desired position Declare a GridBagConstraints object to allow position GridBagConstraints gridConstraints = new GridBagConstraints(); Choose x and y location. gridConstraints.gridx =0; gridConstraints.gridy=0; Add the control. getContentPane().add(controlName,gridConstraints); For ex, getContentPane().add(startButton,gridConstraints);

Adding Controls, cont. To finalize placement of controls in the frame, execute a pack method. pack(); This “packs” the grid layout onto the frame and makes the controls visible.

Stopwatch Project Complete the Stopwatch project GUI from Learn Java (GUI Applications), Chapter 1. pages 1-29 to 1-35 pages 1-44 to 1-50

Event Methods The next step is to add code in event methods and their corresponding listeners. There are two ways…one for AWT and one for Swing objects. Listeners are added into the frame constructor code.

AWT Event Listeners Event listeners for AWT objects are implemented using adapters. Every project needs to “listen” for the event when the user closes the window. Add this code to the frame constructor.

Swing Event Listeners actionPerformed event methods are added using ActionListener.

controlNameActionPerformed We could add the code for the action to the listener, but it is cleaner to add it in an external method as shown here: This makes editing, modifying, and testing easier.

Stopwatch, cont. Complete the Stopwatch project GUI from Learn Java (GUI Applications), Chapter 1. pages 1-62 to 1-64 Try the options on page 1-69 Savings Account Project, Learn Java (GUI App) p 2-28 to 2-35