Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

F27SB2 Programming Languages
TCU CoSc Programming with Java Handling Events.
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.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
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.
Io package as Java’s basic I/O system continue’d.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
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,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
1 Lesson: Applets with User Input and Output with GUI ICS4M.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java Applet Basics (2). The Body Mass Index Calculator.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Csc Basic Graphical User Interface (GUI) Components.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
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,
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
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.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
©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.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
CompSci 230 S Programming Techniques
Aum Amriteshwaryai Namah
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT Components and Containers
Introduction to Event Handling
Chapter 5 Processing Input with Applets
GUI building with the AWT
A picture's worth a thousand words
Presentation transcript:

Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox and CheckboxGroup classes –Choice class –………………..

Some methods of component class comp here is a variable that refers to any component comp.getSize() –returns an object belonging to the class Dimension. It has two instance variable comp.getSize().width and comp.getSize().height comp.getParent() –Returns a value of thpe Container comp.getLocation() –Returns the location of the top-left corner of the comonent. comp.setEnabled(true) & comp.setEnabled(false) –Enable or disable the component. comp.setVisible(true) & comp.setVisible(false) comp.setBackground(color), comp.setForeground(color), comp.setFont(font)

The Button Class An object of class Button is a push button. Some of the Button methods: 1.Constructors: Button bttn = new Button(“test”) 2.Event: When the user clicks on a button, the button generates an event of type ActionEvent. This event is sent to any listener that has been registered with the button. 3.Listeners: An object that wants to handle events generated by buttons must implement the ActionListener interface. This interface defines just one method, "pubic void actionPerformed(ActionEvent evt)", which is called to notify the object of an action event. 4.Registration of Listeners: In order to actually receive notification of an event from a button, an ActionListener must be registered with the button. This is done with the button's addActionListener() method. For example: stopGoButton.addActionListener(buttonHandler)

Button’s methods (continued) 5.Event methods: When actionPerformed(evt) is called by the button, the parameter, evt, contains information about the event. This information can be retrieved by calling methods in the ActionEvent class. In particular, evt.getActionCommand() returns a String giving the command associated with the button. By default, this command is the label that is displayed on the button. 6.Component methods: There are several useful methods in the Button class. For example, stopGoButton.setLabel("Stop") changes the label displayed on the button to "Stop". And stopGoButton.setActionCommand("sgb") changes the action command associated to this button for action events.

The Label Class An object of type Label is just a single line of text. The text cannot be edited by the user The constructor for a Label specifies the text to be displayed: Label message = new Label(“Hello World!”); You can change the text displayed in a label by calling the label’s setText() method: message.setText(“Good World!”); Label is subclass of Component, so you can use Component’s methods such as setFont(), setBackground(), setForeground().

TextField and TextArea Classes TextField and TextArea are boxes where the user can type in and edit text. TextField contains a single line of editable text, while a TextArea displays multiple lines and might include scrollbars. Methods: –getText(); return a copy of the current contents –getSelectedText(); return the selected text –select(int start, int end); change the selected range –setEditable(boolean canBeEdited); specify if or not the text in the component can be edited by the user. The TextField generates an ActionEvent when user presses return while typing in the TextField. The TextField class includes an addActionListener() method. In the actionProformed() method, the evt.getActionCommand() will return a copy of the text from the TextField.

An exmaple for a simple Component events import java.awt.*; // Defines basic classes for GUI programming. import java.awt.event.*; // Defines classes for working with events. import java.applet.*; // Defines the applet class. public class Action04 extends Applet implements ActionListener { Font textFont, buttonFont; // declare the fonts used in this program TextField txt = new TextField("Input field"); //declare a textfield object. Label result = new Label("This is the label where to show results", Label.CENTER); //declare a Label object which will display the string”” Button squareButton = new Button("Square"), rootButton = new Button("Square root"); // Create two new buttons. “Square“ “Square root” are the text // displayed on the buttons.

public void init() { setBackground(Color.lightGray); /* The applet is filled with the background color before the paint method is called. The button and the message in this applet will appear on a light gray background.*/ textFont = new Font("Serif",Font.BOLD,16); // Create a font object representing a big, bold font. buttonFont = new Font("Serif",Font.BOLD,14); squareButton.addActionListener(this); add(squareButton); squareButton.setBackground(Color.blue); squareButton.setForeground(Color.white); squareButton.setFont(buttonFont); /* Set up squareButton to send an "action event" to this applet when the user clicks the button. The parameter, this, is a name for the applet object that we are creating. Add the button to the applet, so that it will appear on the screen.*/

rootButton.addActionListener(this); add(rootButton); rootButton.setBackground(Color.red); rootButton.setForeground(Color.yellow); add(txt); //add the textfield to the applet add(result);//add the label to the applet result.setFont(textFont); } // end init()

public void actionPerformed(ActionEvent evt) { /* This routine is called by the system when an "action" is performed by the user, provided that the applet has been set as a "listener" for such events. Responds to the buttons by giving the different result display in the applet.*/ int number; String command = evt.getActionCommand(); if (command.equals("Square")){ number = Integer.parseInt(txt.getText()); number *= number; result.setText("The square is :"+number); } if (command.equals("Square root")){ number = Integer.parseInt(txt.getText()); result.setText("The square root is :"+Math.sqrt(number)); } } // end class Action