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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
GUI and event-driven programming An introduction.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Applets Yong Choi School of Business CSU, Bakersfield.
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.
 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.
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)
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.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Basics of GUI Programming Chapter 11 and Chapter 22.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
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 Swing One of the most important features of Java is its ability to draw graphics.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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)
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.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
A First Look at GUI Applications
Ellen Walker Hiram College
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Graphics Programming - Frames
Steps to Creating a GUI Interface
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

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 applications)? GUI – Graphical User Interface GUI – Graphical User Interface Java has used the following since its creation: Java has used the following since its creation: AWT – Abstract Windowing Toolkit AWT – Abstract Windowing Toolkit There is an AWT package that is part of the standard Java API. There is an AWT package that is part of the standard Java API.

However, the AWT package had some problems. However, the AWT package had some problems. Remember, the nice thing about Java is that it works on multiple Operating Systems. Remember, the nice thing about Java is that it works on multiple Operating Systems. But some Operating Systems have different ways of interacting with their graphic interface system. But some Operating Systems have different ways of interacting with their graphic interface system.

For example: For example: Some Operating Systems have slider bars for their windows that behave differently than other operating systems. Some Operating Systems have slider bars for their windows that behave differently than other operating systems. Microsoft Windows usually use a mouse with 2 click buttons. Microsoft Windows usually use a mouse with 2 click buttons. Apple’s Operating Systems usually use a mouse with 1 click button. Apple’s Operating Systems usually use a mouse with 1 click button.

The AWT package couldn’t handle these differences across multiple platforms. The AWT package couldn’t handle these differences across multiple platforms. Why? Why? Because the AWT classes rely on an Operatig System’s peer classes. Because the AWT classes rely on an Operatig System’s peer classes. What are peer classes? Read up on them are if you are really curious What are peer classes? Read up on them are if you are really curious To solve these problems, Java introduced the following package (library of classes)…. To solve these problems, Java introduced the following package (library of classes)….

Swing: Swing: This package of classes doesn’t replace AWT. This package of classes doesn’t replace AWT. It provides an improved alternative for creating GUI applications. It provides an improved alternative for creating GUI applications. Very few of the Swing classes rely on an Operating Systems peer classes. Very few of the Swing classes rely on an Operating Systems peer classes. This means that the Swing components have a consistent feel and look across different Operating Systems. This means that the Swing components have a consistent feel and look across different Operating Systems. Today we used the JFrame class, which is part of the Swing package. Today we used the JFrame class, which is part of the Swing package.

Some Terminology: Some Terminology: A container is something that can hold multiple things, or components: A container is something that can hold multiple things, or components: Likewise, a GUI window is considered a container because it can hold multiple graphical components. Likewise, a GUI window is considered a container because it can hold multiple graphical components. Buttons Text Boxes Labels

In Java, a container that can be displayed as a window is known as a…. frame. In Java, a container that can be displayed as a window is known as a…. frame. Frame: Frame: A basic window that has: A basic window that has: A Title Bar. A Title Bar. A Border. A Border. Buttons for minimizing Buttons for minimizing, maximizing, and closing., maximizing, and closing.

A frame like this one is an object, or an INSTANCE of a class. A frame like this one is an object, or an INSTANCE of a class. We can create a class and name it something like SimpleWindow. We can create a class and name it something like SimpleWindow. public class SimpleWindow {}

JFrame class: JFrame class: This class contains code from the Swing package that allows us to create a frame. This class contains code from the Swing package that allows us to create a frame. We must give our SimpleWindow class access to the JFrame class. We must give our SimpleWindow class access to the JFrame class. To do that we must : To do that we must : 1. Import the Swing package. 1. Import the Swing package. 2. Write “extends” at the end of our class declaration. (We will learn more about “extends” in the future. 2. Write “extends” at the end of our class declaration. (We will learn more about “extends” in the future.

Ex: Ex: import javax.swing.*; //import Swing package public class SimpleWindow extends JFrame { }

To create the frame we now need to do the following: To create the frame we now need to do the following: Use the no-argument constructor. Use the no-argument constructor. Create an instance of the JFrame object. Create an instance of the JFrame object. Set the window title. Set the window title. Set the size of the window. Set the size of the window. Tell the frame what to do when the exit button is clicked. Tell the frame what to do when the exit button is clicked. Display the window. Display the window.

Ex: Ex: public class SimpleWindow extends JFrame { //No-Arg Constructor public SimpleWindow( ) { //Create an instance of the window in memory. JFrame window = new JFrame(); //Set the title window.setTitle(“I’m a Simple Window!"); //Set the size of the window – this is in pixels window.setSize(350, 250); //Specify what happens when the close button is clicked. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Display the window. window.setVisible(true); }

Our creation: Our creation:

Great. An empty window. Great. An empty window. How do I add a label to it? How do I add a label to it?

1. Create a panel object. 1. Create a panel object. 2. Create a label object. 2. Create a label object. 3. Add the label to the panel. 3. Add the label to the panel. 4. Add the panel to the frame’s content label. 4. Add the panel to the frame’s content label. Let’s break these steps down….

Each frame object has a content pane. Each frame object has a content pane. To add things to our frame we have to add them to the content pane. To add things to our frame we have to add them to the content pane. Content Pane

How do we add things to the content pane? How do we add things to the content pane? We use a panel. We use a panel. Panel Panel A panel is also a container that holds GUI objects. A panel is also a container that holds GUI objects. However, panels cannot be displayed by themselves. They have to be added to the content pane. However, panels cannot be displayed by themselves. They have to be added to the content pane. To create a panel: To create a panel: Define the panel name in the class’ fields. Define the panel name in the class’ fields. Create the panel object: Create the panel object: panel = new JPanel;

Now we can create a label: Now we can create a label: Define the label name in the class’ fields. Define the label name in the class’ fields. Create the label object: Create the label object: lblMessage = new JLabel(“I’m on a boat!”) lblMessage = new JLabel(“I’m on a boat!”) Here’s our label: Here’s our label: I’m on a boat!

Add the label to the panel. Add the label to the panel. Ex: Ex: panel.add(lblMessage); panel.add(lblMessage); Add the panel to the content pane. Add the panel to the content pane. Ex: Ex: add(panel); add(panel); I’m on a boat!

Here is our completed window: Here is our completed window:

Code for the Main class: public class Main { public static void main (String[]args) { //Creating a CrazyWindow object SimipleWindow myWindow = new SimpleWindow(); }

Code for the SimpleWindow class: import javax.swing.*; public class SimpleWindow extends JFrame { private JPanel panel; private JLabel lblMessage; //Constructor public SimpleWindow( ) { //Set the window title. setTitle("I'm a Simple Window!"); //Set the size of the window setSize(350, 250); //Specify what happens when the close button is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create a JPanel object and let the panel field reference it panel = new JPanel(); //Create another label to display craziness lblMessage = new JLabel("I'm on a boat!"); //Add the labels to the panel //panel.add(lblMessage); //Add the panel to the frame's content pane add(panel); //Display the window setVisible(true); }