Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.

Slides:



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

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Lecture 19 Graphics User Interfaces (GUIs)
Graphical User Interfaces
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
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.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
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.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
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.
 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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Swing One of the most important features of Java is its ability to draw graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Graphical User Interfaces
Introduction to Graphics
A First Look at GUI Applications
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Java GUI

Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Graphical User Interfaces GUI’s, AWT, and Swing GUI’s, AWT, and Swing –AWT (Abstract Window Toolkit) is the original Java classes used for GUI development. –Java 2 includes an improved toolkit, named Swing, to improve on AWT. Swing components are easier to use, more portable, and provide more functionality than older AWT components. Swing components are easier to use, more portable, and provide more functionality than older AWT components.

Inheritance Helps Since Swing was designed as an improvement of AWT, it made sense not to start from scratch when designing the Swing components. Since Swing was designed as an improvement of AWT, it made sense not to start from scratch when designing the Swing components. To differentiate them, Swing components have names that begin with the letter “J”. To differentiate them, Swing components have names that begin with the letter “J”.

Graphical User Interfaces GUI’s and the Java Swing components GUI’s and the Java Swing components –JComponent is the ancestor of many Swing classes. Here are some descendants of the Jcomponent class. JButton JButton JPanel JPanel JLabel JLabel JTextArea JTextArea

Windows We will use the class JFrame to create GUI windows. We will use the class JFrame to create GUI windows. –javax.swing.JFrame inherits from java.awt.Frame –Every JFrame object has a “content pane” associated with it. Content panes are of type java.awt.Container Content panes are of type java.awt.Container

What to Import When we design GUI programs, there are three packages we will need to import: When we design GUI programs, there are three packages we will need to import: –java.awt.* –java.awt.event.* –javax.swing.* The java.awt.event package provides us with the capability to respond to user interface “events”, such as the pushing of a button. The java.awt.event package provides us with the capability to respond to user interface “events”, such as the pushing of a button.

Displaying a Window The most common form of a window displayed via Swing is a JFrame. The most common form of a window displayed via Swing is a JFrame. –Next, we will see several example programs that will: display JFrames display JFrames place Swing objects in frames place Swing objects in frames Exhibit event-driven programming Exhibit event-driven programming –“Listeners” will be used to respond to mouse events.

Example GUIone Creating a JFrame Creating a JFrame –new JFrame() Setting the size of our frame. Setting the size of our frame. –setSize(…); Displaying the frame Displaying the frame –setVisible(true)

FirstFrame.java import javax.swing.*; class FirstFrame extends JFrame { public FirstFrame() { super("FirstFrame"); setSize(300, 200); setVisible(true); } public static void main(String[] args) { JFrame frame = new FirstFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

Putting content in a GUI Frames constitute GUI real estate Frames constitute GUI real estate Organization of space: Organization of space: –Via other containers Each container defines and manages an area within the GUI Each container defines and manages an area within the GUI –e.g. menu bars, scroll bars, tool bars, panels, etc. Containers can be nested Containers can be nested Individual components added into those Containers Individual components added into those Containers Containers may decide on their layout (will be discussed later) Containers may decide on their layout (will be discussed later)

LabelTest import javax.swing.*; import java.awt.*; public class LabelTest extends JFrame { public LabelTest() { setTitle("Label Test"); { setTitle("Label Test"); JLabel helloLabel = new JLabel("Hello"); JLabel helloLabel = new JLabel("Hello"); add( helloLabel); add( helloLabel); setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } public static void main(String[] args) public static void main(String[] args) { LabelTest t = new LabelTest(); { LabelTest t = new LabelTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Event Handling Perform some functionalities when the button is press Perform some functionalities when the button is press Need to have a class that implements ActionListener Need to have a class that implements ActionListener The functionalities needed to perform will be put in the actionPerformed method The functionalities needed to perform will be put in the actionPerformed method Create an instance of this type of ActionListener Create an instance of this type of ActionListener Register the handler with the component Register the handler with the component

ButtonTest public class ButtonTest extends JFrame { public ButtonTest() { setTitle("Button Test"); { setTitle("Button Test"); JButton helloButton = new JButton("Hello"); JButton helloButton = new JButton("Hello"); add( helloButton); add( helloButton); // create an instance of inner class ButtonHandler to use for button event handling // create an instance of inner class ButtonHandler to use for button event handling ButtonHandler handler = new ButtonHandler(); ButtonHandler handler = new ButtonHandler(); //register the handler //register the handler helloButton.addActionListener( handler ); helloButton.addActionListener( handler ); setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } // inner class for button event handling // inner class for button event handling private class ButtonHandler implements ActionListener { private class ButtonHandler implements ActionListener { // handle button event // handle button event public void actionPerformed( ActionEvent event ) public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() ); "You pressed: " + event.getActionCommand() ); } } // end private inner class ButtonHandler } // end private inner class ButtonHandler public static void main(String[] args) public static void main(String[] args) { ButtonTest t = new ButtonTest(); { ButtonTest t = new ButtonTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

No inner class public class ButtonTest2 extends JFrame implements ActionListener { public ButtonTest2() public ButtonTest2() { setTitle("Button Test"); setTitle("Button Test"); JButton helloButton = new JButton("Hello"); JButton helloButton = new JButton("Hello"); add( helloButton); add( helloButton); helloButton.addActionListener( this ); //register the handler (the frame itself) helloButton.addActionListener( this ); //register the handler (the frame itself) setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } // handle button event // handle button event public void actionPerformed( ActionEvent event ) public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() ); "You pressed: " + event.getActionCommand() ); } public static void main(String[] args) public static void main(String[] args) { ButtonTest2 t = new ButtonTest2(); ButtonTest2 t = new ButtonTest2(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Example JTextfield JTextfield Get Length of square in a Jtextfield and output the area Get Length of square in a Jtextfield and output the area