Jan. 20041 AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.

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

Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Graphic User Interfaces Layout Managers Event Handling.
More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check.
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
12-Jul-15 Components. 2 Types of Components Button Canvas Checkbox Choice Label List Scrollbar TextComponent TextArea TextField.
Java Programming Chapter 10 Graphical User Interfaces.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets, AWTS CompSci 230 Software Construction.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Objectives of This Session
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
IMS 3253: Controls 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Check Boxes Radio Buttons Date Time Picker Masked.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
GUIs & Event-Driven Programming Chapter 11 Review.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
JAVA AWT.
AWT Components and Containers
GUI building with the AWT
A picture's worth a thousand words
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg

Jan Out line: AWT Widgets Graphical components - Label -TextComponent -Button -CheckBox -Choice -List -Container -Canvas

Jan AWT Graphics AWT Widgets - components for GUIs

Jan AWT Widgets Widgets are components such as: - buttons, text fields, menus, etc. These widgets are used to make up a Graphical User Interface (GUI) The java.awt package provides a set of classes for implementing a wide variety of widgets. These classes are platform independent, so a menu in a Java program will look like a Windows menu on a PC running Windows, a Mac menu on a Macintosh, and a Motif menu on a Unix machine - all with the same code.

Jan AWT Widgets We construct a GUI by placing components within a container. -normally, there are only two types of containers used: Frame - creates a complete window Panel - a container and a component -Several Panels may be within a Frame, and within each Panel may be several components.

Jan The Component Class The Component class is an “abstract” class. Component CanvasContainerTextComponentButton PanelWindowTextFieldTextArea FrameDialog Applet Label

Jan The Component Class The Component class is an “abstract” class. public abstract class Component { … paint (Graphics g) { } repaint(int x, int, y, int width, int height) { … } update(Graphics g) { … } } The Component Class The Component class is an “abstract” class.

Jan The Component Class void paint (Graphics g) -this method causes something to be painted in the component. -in Component class, this method is empty and does nothing, it is intended to be overridden in a subclass as we have been doing. -Example: … public void paint(Graphics g) { g.setFont (new Font(“Serif”, Font.BOLD, 36)); FontMetrics fm = g.getFontMetrics( ); String str = “” + counter; Dimention d = getSize(); int x = d.width/2 - fm.stringWidth(str)/2; g.drawString(str, x, d.height/2);}}

Jan The Component Class When you want to update the display, you cannot directly call the paint( ) method can. But you can cause it to be called by calling a method called repaint(). void repaint () void repaint (int x, int y, int width, int height) -These two methods are used to tell the system to redraw the Component as soon as possible -They instruct the system to schedule a call to update() -You can’t count on repaint() to immediately update your screen - most of the time it will happen quickly, but if you are running something time sensitive like animation, special techniques must be used.

Jan The Component Class void update( Graphics g) -like the paint() method, you can’t invoke this method directly -The system calls this method when the screen needs to be redrawn. For example, whenever repaint( ) is executed. -If this method is not overridden, this method will clear your component and then call the paint method

Jan import java.applet.*; import java.awt.*; import java.lang.*; /* */ public class DoubleBuffer extends Applet implements Runnable { int x = 5; Thread t; Image buffer = null; Graphics bufferg;

Jan public void init() { //Start thread t = new Thread(this); t.start(); //Create buffer Dimension d = getSize(); buffer = createImage(d.width, d.height); } public void run() { try { while(true) { repaint(); //Request a repaint

Jan //Sleep before update Thread.sleep(100); } catch(Exception e) {} } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { //Get graphics object for buffer if (buffer != null)

Jan { bufferg = buffer.getGraphics(); //Draw to buffer Dimension d = getSize(); bufferg.setColor(Color.white); bufferg.fillRect(0, 0, d.width, d.height); bufferg.setColor(Color.black); bufferg.fillOval(x, d.height/4, 50, 50); //Update screen g.drawImage(buffer, 0, 0, this); //Increment x x +=5; if (x + 50 > d.width) x = 0;}}}

Jan Textual Widgets - Labels A label is a component that will display a line of read-only text on the screen. It has three constructors: -Label() center alignment and no text -Label( String text) a center aligned Label object displaying the text given -Label( String text, int alignment) Label with the given text and alignment specified as either Label. LEFT, Label. CENTER, and Label. RIGHT After you have a Label object, you can use methods defined in the Label class to get and set various attributes.

Jan Label Class -String getText() returns a string containing this label’s text -void setText( String text) sets the text for this label to the value of the argument - int getAlignment() returns an integer corresponding to the alignment of the label 0. is Label. LEFT 1. is Label. CENTER 2. is Label. RIGHT -void setAlignment( int alignment) sets the alignment of the label to the specified argument

Jan Label Class Example: import java. awt.*; public class LabelExample extends java. applet. Applet{ public void init() { setLayout( new GridLayout( 1, 1)); add( new Label(“ left”, Label. LEFT)); add( new Label(“ center”, Label. CENTER)); add( new Label(“ right”, Label. RIGHT)); }//end of init }//end of LabelExample row number column number

Jan init() Method The init() method is somewhat like the paint() method in that it’s not called by any method in our class. The init() method belongs to the Applet class and is called when the applet is first loaded. - This is where we would want to do any onetime initialization -Very similar to the main( ) method for Java application

Jan Text Component Class There are two textual widgets: TextField and TextArea that are very similar to each other, so the data and methods that are common to both were removed and placed into the TextComponent class So TextField and TextArea are both subclasses of Text Component There are quite a few methods that are inherited by TextField and TextArea - String getText() returns a copy of the current contents of the component -void setText( String text) sets the current text to what is specified in by the argument

Jan TextField Class Text fields are a box into which a single line of text may be placed. Text fields are different from labels in that they can be edited.

Jan TextField Class There are four constructors to TextField objects: -TextField() creates an empty text field that is 0 characters wide - TextField( int columns) creates an empty text field wide enough to hold columns characters -TextField (String Text) creates a text field initialized with the given string -TextField( String text, int columns) creates a text field with content and width given by text and columns

Jan TextArea Class Text areas are like text fields except they can handle larger amounts of text. Text areas can be given any width and height and have scroll bars by default.

Jan TextArea Class There are five constructors for a TextArea object: -TextArea() creates an empty TextArea object -TextArea( int rows, int columns) creates an empty text area with the given number of rows and columns( chars) -TextArea( String text) creates a default sized text area containing the argument string -TextArea( String text, int rows, int columns) creates a text area displaying the given string with the size specified

Jan TextArea Class -TextArea( String text, int rows, int columns, int scrollbars) - Acts like the preceding constructor, in addition it has an argument to specify whether or not the text area will have scrollbars. -There are 4 constant arguments that can be used: TextArea. SCROLLBARS_ BOTH TextArea. SCROLLBARS_ NONE TextArea. SCROLLBARS_ HORIZONTAL_ ONLY TextArea. SCROLLBARS_ VERTICAL_ ONLY -The default is for both horizontal and vertical scrollbars to be displayed.

Jan TextArea Class An example: import java. awt.*; public class TextAreaExample extends java. applet. Applet { public void init() { String str = “This is an example for a TextArea that \n”+ “spans multiple lines. \n”+ “This is for course / 3 at the \n”+ “University of Winnipeg!\n”; add( new TextArea( str)); }//end of int }//end of TextAreaExample

Jan Button Class Buttons are a simple UI component that triggers some action There are two constructors for a Button object: -Button() creates an empty button with no label -Button( String label) creates a button with the given string as a label After creating a Button object, you can get the value of the Button’s label by using the getLabel() method which returns a String. Using the setLabel() method, you can set the label of the button by passing in a String value.

Jan Button Class An Example: import java. awt.*; public class ButtonExample extends java. applet. Applet { public void init() { add( new Button(“ Rewind”)); add( new Button(“ Fast Forward”)); add( new Button(“ Play”)); add( new Button(“ Stop”)); add( new Button(“ Pause”)); }//end of init }//end of ButtonExample

Jan Checkbox Class Check boxes are UI components that have two states: on and off (or checked and unchecked, or true and false) unlike buttons, check boxes don’t trigger a direct action, but are used to indicate optional features of some other action. Check boxes can be used in two ways: -Nonexclusive given a series of checkboxes, any of them can be selected; can be checked or unchecked independently of each other -Exclusive given a series of checkboxes, only one can be selected at any one time; also known as radio buttons or checkboxgroups

Jan Checkbox Class There are five constructors: -Checkbox() creates a Checkbox with no label and false -Checkbox( String label) creates a Checkbox with the given label and initialized to false -Checkbox( String label, boolean state) creates a Checkbox with the given label and state -Checkbox( String label, boolean state, CheckboxGroup group) -Checkbox( String label, CheckboxGroup group, boolean state) The last two create a Checkbox with the label and state specified as well as specifying a group that this Checkbox will belong to. There are a number of methods in this class that allow you to get and set the label, state and CheckboxGroup.

Jan Checkbox Class An Example: import java. awt.*; public class CheckboxExample extends java. applet. Applet { public void init() { setLayout( new FlowLayout( FlowLayout. LEFT)); add( new Checkbox(“ Red”)); add( new Checkbox(“ Orange”)); add( new Checkbox(“ Yellow”)); add( new Checkbox(“ Green”)); add( new Checkbox(“ Blue”)); add( new Checkbox(“ Indigo”)); add( new Checkbox(“ Violet”)); }//end of init }//end of CheckboxExample

Jan CheckBoxGroups A CheckboxGroup is a collection of Checkboxes in which only one of them can be selected at one time; - also known as radio buttons To create a series of radio buttons, first create an instance of CheckboxGroup -ChecboxGroup cbg = new CheckboxGroup(); Then create and add the radio buttons into the group - use either of the following two methods: Checkbox( String label, boolean state, CheckboxGroup group) Checkbox( String label, CheckboxGroup group, boolean state)

Jan CheckBoxGroups An Example: import java. awt.*; public class CheckboxGroupExample extends java. applet. Applet { public void init() { setLayout( new FlowLayout( FlowLayout. LEFT)); CheckboxGroup cbg = new CheckboxGroup(); add( new Checkbox(“ One”, false, cbg)); add( new Checkbox(“ Two”, false, cbg)); add( new Checkbox(“ Three”, true, cbg)); add( new Checkbox(“ Four”, false, cbg)); }//end of init }//end of CheckboxGroupExample

Jan CheckBoxGroups Since CheckboxGroups only allow one button to be selected at one time, the last Checkbox to be added to the group with the state being true will be the one selected by default. So in the previous example, radio button with the label three would be selected.

Jan Choice Class Choice menus are more complex than labels, buttons, or checkboxes Choice menus are pop-up or pull-down lists in which an item can be selected. To create a Choice menu, we first need an instance of the Choice class and then add items to it. The items added are enumerated from top to bottom starting with the index 0. One of the methods in this class to make note of is: -String getSelectedItem() returns the text of the current selection

Jan Choice Class An Example: import java. awt.*; public class ChoiceExample extends java. applet. Applet { public void init() { Choice c = new Choice(); c. add(“ Red”); c. add(“ Orange”); c. add(“ Yellow”); c. add(“ Green”); c. add(“ Blue”); c. add(“ Indigo”); c. add(“ Violet”); add( c); }//end of init }//end of ChoiceExample

Jan List Class A List is functionally similar to a Choice menu in that it lets you choose from a list, but Lists do not pop-up, they are permanently displayed on the screen. You can choose more than one item on the List if that capability is enabled. List items are enumerated from top to bottom starting at index 0 just like a Choice menu.

Jan List Class There are three constructors for a List object: -List() creates a default sized List object in single selection mode -List( int rows) creates a List object in single selection mode with the number of rows given -List( int rows, boolean multipleselection) creates a List object with the specified number of rows. The List is in multiple selection mode if the boolean argument is true and single selection otherwise.

Jan List Class An Example: import java. awt.*; public class ListExample extends java. applet. Applet { public void init() { List mylist = new List( 7, true); mylist. add(“ Red”); mylist. add(“ Orange”); mylist. add(“ Yellow”); mylist. add(“ Green”); mylist. add(“ Blue”); mylist. add(“ Indigo”); mylist. add(“ Violet”); add( mylist); }//end of init }//end of ListExample

Jan List Class There are many methods in this class, we will look at a few of them: -String getItem( int index) returns the item string at the given index -int getItemCount() returns the number of items in the list -int getSelectedIndex() returns the index of the current selection returns -1 if no item is selected or more than 1 item is selected -int[] getSelectedIndexes() returns an array of index positions (for multiple selection lists)

Jan List Class -String getSelectedItem() returns the selected item as a string -String[] getSelectedItems() returns an array of strings containing all selected items -void select( int index) selects the item at the specified index