Lesson 28: More on the GUI button, frame and actions.

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Drawing in a frame – Java GUI
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Java Programming Chapter 10 Graphical User Interfaces.
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,
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
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.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
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.
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,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
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.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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,
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 33: Layout management and drawing – Java GUI.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
GUIs & Event-Driven Programming Chapter 11 Review.
©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.
Lecture 15 Basic GUI programming
Multiple buttons and action calls
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
Web Design & Development Lecture 11
A First Look at GUI Applications
Ellen Walker Hiram College
Steps to Creating a GUI Interface
The calculator – Java GUI
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Lesson 28: More on the GUI button, frame and actions

// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); // for wrapping the frame we could use this command // frame.pack(); frame.setSize(300,200); frame.show(); } The Java Button RECAP

The new result The source fileCompiling and running the program The output RECAP

More on the Swing library Any program that uses Swing to create a window enters a separate thread of execution that enters an infinite loop, looking for events such as mouse movements, button clicks or key presses. This is like having to workers executing the program: –One for the main() –One for watching for events This results in an event driven program When main() is finished, the program still is executing the “listening” part of the library object. As a result the program in Lesson 27 does not stop executing but “hangs”. And we have to exit using the program commands in Windows

More on the Swing library All Swing components are event sources that can be observed or listened to. To use the event, we need to tell the source which object to notify when an event occurs. By default the JButton reacts to a mouse click by changing its appearance. JButtons are also the source of java.awt.event.ActionEvent objects. An ActionEvent is generated by a button when you click the button with a mouse. Objects that can receive events are called listeners. Different type of events have different listeners.

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } Importing the abstract windows toolkit and the Swing library Creating a class called HelloGoodBye this has to be stored in a file named HelloGoodBye.java (case sensitive).

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } The main section. This means we can execute this program Creating a java frame named frame with the label HelloGoodBye The keyword (new) to identify a constructor statement where objects are created

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } A container is a component to which we can add other components. A JFrame is actually a container, but we should not add components directly to a JFrame. Instead, we should add all components to a special container in the JFrame. We use the method getContentPane() to get a reference to JFrame’s special container.

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } Here we create a Javax.swing.JButton object and give it a string which is used as a label. The object is named hello and is a Java button (Jbutton)

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } Creating an object named listener based on the class GoodBye (which we have not created yet). This class will contain the events we want to execute when the button is clicked. Linking the listener object (we could have called this something more descriptive), to our button through the method addActionListener which was inherited from JButton to our button called hello.

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } The method add in the class Container is used to add a component to the JFrame’s content pane (later we will add many components to a frame). Note: For now, we do not tell the container how to arrange this component, nor where this component is to be displayed (we will do this later).

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } The show method from JFrame is used to display the frame. We can set the size of the frame by using the method setSize from JFrame. This method accepts 2 parameters (width, height), which passed in terms of number of pixels.

An Example // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); } Now we have a button, but no action for the object named listener from the GoodBye class

Creating the GoodBye class // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } Importing the awt.event package from the library that contains the various event listing interfaces. This example use the interface ActionListener to indicate that GoodBye contains the method actionPerformed().

An Example // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } Here we indicate that the class GoodBye contains all methods specified in the interface ActionListener. That is to say that the class implements the interface ActionListener

An Example // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } This method is called when the button is clicked. It is executed through the actionPerformed method, which, as we covered before, for a JButton is a mouseclick. In this example we do not use the ActionEvent parameter passed to the method. We will use it in later examples. Prints Goodbye on the screen (not in the frame) Exits the program

The Files in NotepadThe Files in Command Prompt Compiling and class files The Result

The steps Create a button with new Jbutton (“some label”) Get the Container for the Jframe using getContentPane() Add the button to the content pane of the Jframe with add() Create an ActionEventListener class by –Adding implements ActionEventListener to the lcass declaration and –Defining an actionPerformed method Add the listener object to the list of listeners for the button by calling button.addActionListener(listener), where the button is the name we gave our button, and listener is an instance of the class we created to be executed when the button was clicked (the GoodBye class).