GUI building with the AWT

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Objectives of This Session
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
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.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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.
A Quick Java Swing Tutorial
GUIs and Events Rick Mercer.
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.
Graphical User Interfaces
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
Java Swing.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT Components and Containers
GUI building with the AWT
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
GUI building with the AWT
Advanced GUIs and Graphics
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

GUI building with the AWT Java GUI building with the AWT

AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Uses the controls defined by your OS therefore it's "least common denominator" Difficult to build an attractive GUI import java.awt.*;

Swing Requires Java 2 or a separate (huge) download More controls, and they are more flexible Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*; // Mac or PC import com.sun.java.swing.*; // UNIX

Swing vs. AWT Swing is bigger and slower Swing is more flexible and better looking Swing and AWT are incompatible--you can use either, but you can’t mix them Learning the AWT is a good start on learning Swing AWT: Button b = new Button (“OK”); Swing: Jbutton b = new Jbutton(“OK”);

GUI Component Menu Menu bar Combo box Text area Button Scroll bar List Label

To build a GUI... Make somewhere to display things--a Frame, a Window, or an Applet Create controls (buttons, text areas, etc.) Add your controls to your display area Arrange, or lay out, your controls Attach Listeners actions to your controls

Buttons import java.awt.*; public class SimpleButton extends java.applet.Applet { Button helloButton = new Button(“Hello World!”); public void init() { add(helloButton); }}

Labels add(eMailLabel); Label eMailLabel = new Label("E-mail address: "); add(eMailLabel);

Text Fields add(txtName); TextField txtName = new TextField(“Donny”, 10); add(txtName);

Text Areas TextArea comments = new TextArea(“This is on line 1\nThis is on line 2”, 3,20); add(comments);

Check Boxes Checkbox goodTutorial = new Checkbox(“Is this a good tutorial?”); add(goodTutorial);

CheckboxGroup age = new CheckboxGroup(); Checkbox chkUnder10 = new Checkbox(“Under 10 years old”, false, age); Checkbox chk10to15 = new Checkbox(“10 to 14 years old”, false, age); Checkbox chk15to20 = new Checkbox(“15 to 19 years old”, true, age); Checkbox chkOver20 = new Checkbox(“Over 20 years old”, false, age); add(chkUnder10); add(chk10to15); add(chk15to20); add(chkOver20);

Drop Down Lists Choice textAlignment = new Choice(); textAlignment.add(“Left”); textAlignment.add(“Center”); textAlignment.add(“Right”); textAlignment.add(“Random”); add(textAlignment);

GUI Class Hierarchy (Swing)

Containers and Components The job of a Container is to hold and display Components. A Container is also a Component Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea Some Container subclasses are Panel (and Applet), Window, and Frame

Container Classes Container classes can contain other GUI components.

GUI Helper Classes The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension.

Swing GUI Components

AWT (Optional)

An Applet is panel is a container java.lang.Object | +----java.awt.Component +----java.awt.Container +----java.awt.Panel +----java.applet.Applet …so you can display things in it

To create an applet class MyApplet extends Applet { … } this is the only way to make an Applet You can add components to the applet It’s best to add components in init( ) You can paint directly on the applet, but… …it’s better to paint on another component Do all painting from paint(Graphics g)

Some types of components

Creating components Label lab = new Label (”Hi, Dave!"); Button but = new Button ("Click me!"); Checkbox toggle = new Checkbox (”toggle"); TextField txt = new TextField (”Initial text.", 20); Scrollbar scrolly = new Scrollbar (Scrollbar.HORIZONTAL, initialValue, bubbleSize, minValue, maxValue);

Adding components to the Applet class MyApplet extends Applet { public void init () { add (lab); // same as this.add(lab) add (but); add (toggle); add (txt); add (scrolly);

Arranging components Every Container has a layout manager The default layout for a Panel is FlowLayout An Applet is a Panel The default layout for a Applet is FlowLayout You could set it explicitly with setLayout (new FlowLayout( )); You could change it to some other layout manager

FlowLayout Use add (component); to add to a component using a FlowLayout Components are added left-to-right If no room, a new row is started Exact layout depends on size of Applet Components are made as small as possible FlowLayout is convenient but often ugly

BorderLayout At most five components can be added If you want more components, add a Panel, then add components to it. setLayout (new BorderLayout()); add (BorderLayout.NORTH, new Button(“NORTH”));

BorderLayout with five Buttons public void init() { setLayout (new BorderLayout ()); add (BorderLayout.NORTH, new Button ("NORTH")); add (BorderLayout.SOUTH, new Button ("SOUTH")); add (BorderLayout.EAST, new Button ("EAST")); add (BorderLayout.WEST, new Button ("WEST")); add (BorderLayout.CENTER, new Button ("CENTER")); }

Using a Panel panel p = new Panel(); add (BorderLayout.SOUTH, p); p.add (new Button (“Button 1”)); p.add (new Button (“Button 2”));

Frame and JFrame import javax.swing.*; class MyFrame extends JFrame { public MyFrame() { setTitle("My Empty Frame"); setSize(300,200); // default size is 0,0 setLocation(10,200); // default is 0,0 (top left corner) } public static void main(String[] args) { JFrame f = new MyFrame(); f.show(); } }

Panels Panel myPanel = new Panel(); myPanel.add(helloButton); myPanel.add(goodbyeButton); add(myPanel);

Making components active Most components already appear to do something--buttons click, text appears To associate an action with a component, attach a listener to it Components send events, listeners listen for events Different components may send different events, and require different listeners

How to Write an Action Listener Action listeners are probably the easiest — and most common — event handlers to implement. You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

How to Write an Action Listener Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example:public class MyClass implements ActionListener { Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }

Listeners Listeners are interfaces, not classes class MyButtonListener implements ActionListener { An interface is a group of methods that must be supplied When you say implements, you are promising to supply those methods

Writing a Listener For a Button, you need an ActionListener b1.addActionListener (new MyButtonListener ( )); An ActionListener must have an actionPerformed( ) method public void actionPerformed(ActionEvent e) {…}

MyButtonListener public void init () { ... b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); }

Listeners for TextFields An ActionListener listens for hitting the return key An ActionListener demands public void actionPerformed (ActionEvent e) use getText( ) to get the text A TextListener listens for any and all keys A TextListener demands public void textValueChanged(TextEvent e)

Summary I: Building a GUI Create an applet by extending Applet Choose a layout manager (FlowLayout is the default) Create more complex layouts by putting Panels in the Applet; each Panel can have its own layout manager Create other components and add them to whichever Panels you like

Summary II: Building a GUI For each active component, look up what kind of Listeners it can have Create (implement) the Listeners, typically one Listener per active component For each Listener you implement, supply the methods that it requires Finally, don't forget to supply the HTML