Multiple buttons and action calls

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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
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.
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,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Interfaces & Polymorphism part 2:
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 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,
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.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
Lesson 33: Layout management and drawing – Java GUI.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
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.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Chapter 4 Interface Types and Polymorphism Part 2.
Lecture 15 Basic GUI 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.
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
Java Applet What is a Java Applet? How is applet compiled?
Web Design & Development Lecture 11
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Chapter 4 Interface Types and Polymorphism Part 2
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Introduction to Event Handling
Steps to Creating a GUI Interface
The calculator – Java GUI
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Multiple buttons and action calls Lesson 29: Multiple buttons and action calls

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. RECAP

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. RECAP

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). RECAP

public static void main (String[] args){ RECAP // 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 Result The Files in Notepad The Files in Command Prompt RECAP The Files in Notepad The Files in Command Prompt Compiling and class files

The Result RECAP

Many Buttons // ManyButtons.java import java.awt.*; import javax.swing.*; class ManyButtons{ public static void main (String[] args){ JFrame frame = new JFrame("myCalculator"); Container pane = frame.getContentPane(); JButton exit = new JButton("Exit"); GoodBye listener = new GoodBye(); exit.addActionListener(listener); JButton myAddition = new JButton("2+2"); Addition listener1 = new Addition(); myAddition.addActionListener(listener1); JButton mySubtraction = new JButton("123-12"); Subtraction listener2 = new Subtraction(); mySubtraction.addActionListener(listener2); pane.add(exit); pane.add(myAddition); pane.add(mySubtraction); frame.pack(); frame.show(); } Many Buttons

Many Buttons // Addition.java import java.awt.event.*; class Addition implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("The answer is 4"); System.exit(0); } // Subtraction.java class Subtraction implements ActionListener{ System.out.println("The answer is 111"); // GoodBye.java class GoodBye implements ActionListener{ System.out.println("Goodbye!"); Many Buttons

Text input // TextInput.java import java.awt.*; import javax.swing.*; class TextInput { public static void main (String[] args){ JFrame frame = new JFrame("textInput"); Container pane = frame.getContentPane(); JTextField input = new JTextField("enter text and press return"); Echo listner=new Echo(); input.addActionListener(listener); pane.add(input); frame.pack(); frame.show(); }

Text input // Echo.java import java.awt.event.*; class Echo implements ActionListener{ public void actionPerformed(ActionEvent e) { JTextField source =(JTextField)e.getSource(); String text = source.getText(); System.out.println("text"); System.exit(0); }