CHAPTER 2. Java Overview

Similar documents
Java. GUI building with the AWT

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Graphic User Interfaces. - GUI concepts - Swing - AWT

Java Applets / Flash

G51PRG: Introduction to Programming Second semester Applets and graphics

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Introduction to Computer Science I

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

Packages: Putting Classes Together

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Java - Applets. public class Buttons extends Applet implements ActionListener

PIC 20A GUI with swing

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

User interfaces and Swing

Introduction. Introduction

GUI in Java TalentHome Solutions

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Systems Programming Graphical User Interfaces

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Building Java Programs Bonus Slides

Window Interfaces Using Swing Objects

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes


China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Window Interfaces Using Swing Objects

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

The AWT Package, An Overview

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java Programming Lecture 6

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Chapter 12 Advanced GUIs and Graphics

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Dr. Hikmat A. M. AbdelJaber

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Agenda. Container and Component

Graphical User Interfaces. Comp 152

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Sri Vidya College of Engineering & Technology

2110: GUIS: Graphical User Interfaces

Part I: Learn Common Graphics Components

encompass a group of features for building Graphical User Interfaces (GUI).

Advanced Java Programming

CompSci 230 S Programming Techniques. Basic GUI Components

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Programming graphics

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Programming Language Concepts: Lecture 8

CSC 551: Web Programming. Fall 2001

Mr. Nilesh Vishwasrao Patil. Advanced Java Programming: (17625)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Part 3: Graphical User Interface (GUI) & Java Applets

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

CS11 Java. Fall Lecture 4

Graphical User Interfaces (GUIs)

The Abstract Window Toolkit

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Lecture 5: Java Graphics

Example: Building a Java GUI

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Example: Building a Java GUI

Graphical User Interfaces in Java - SWING

CS 2113 Software Engineering

AP CS Unit 11: Graphics and Events

CS 251 Intermediate Programming GUIs: Components and Layout

INTRODUCTION TO (GUIS)

CSC 1214: Object-Oriented Programming

Graphical User Interfaces in Java

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interface (GUI)

Object Oriented Design & Patterns. Part 1

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Computer Programming Java AWT Lab 18

Chapter 7: A First Look at GUI Applications

Dr. Hikmat A. M. AbdelJaber

GUI Event Handlers (Part I)

Chapter 1 GUI Applications

Transcription:

Networks and Internet Programming (0907522) CHAPTER 2 Java Overview Instructor: Dr. Khalid A. Darabkh

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 To outline the basic structure of GUIs To show how to add components to containers To understand how to use Layout Managers To understand how to use Event Handling To understand basic graphics processing under the AWT

AWT (Abstract Windowing Toolkit) The AWT is roughly broken into four categories Components and Containers Layout Managers Event Handling Graphics Many AWT components have been replaced by Swing components It is generally not considered a good idea to mix Swing components and AWT components. Choose to use one or the other.

Running Java files There are 3 different ways to run a java executable: As a stand-alone program that can be invoked from the command line. This is termed an application. As a program embedded in a web page to be run when the page is browsed. This is termed an applet. As a program that is invoked on demand on a server system and that runs in the context of a web server. This is termed a servlet. 4

AWT Revisited Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore it's least common denominator Difficult to build an attractive GUI import java.awt.*; import java.awt.event.*; 5

Swing Same concepts as AWT Doesn t work in ancient Java implementations (Java 1.1 and earlier) Many more controls, and they are more flexible Some controls, but not all, are a lot more complicated Gives a choice of look and feel packages Much easier to build an attractive GUI import javax.swing.*; 6

Swing vs. AWT Swing is bigger, slower, and more complicated But not as slow as it used to be Swing is more flexible and better looking Swing and AWT are incompatible--you can use either, but you can t mix them Actually, you can, but it s tricky and not worth doing Learning the AWT is a good start on learning Swing Many of the most common controls are just renamed AWT: Button b = new Button ("OK"); Swing: JButton b = new JButton("OK"); 7

To build a GUI... Make somewhere to display things usually a Frame or Dialog (for an application), or an Applet Create some Components, such as buttons, text areas, panels, etc. Add your Components to your display area Arrange, or lay out, your Components Attach Listeners to your Components Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs, and executes some code to deal with it 8

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

Component Component is the superclass of most of the displayable classes defined within the AWT. Note: it is abstract. MenuComponent is another class which is similar to Component except it is the superclass for all GUI items which can be displayed within a drop-down menu. The Component class defines data and methods which are relevant to all Components paint(graphics g) setbounds setsize setlocation setfont setenabled setvisible setforeground setbackground -- colour -- colour

Container Container is a subclass of Component. (ie. All containers are themselves, Components) Containers contain components For a component to be placed on the screen, it must be placed within a Container The Container class defined all the data and methods necessary for managing groups of Components add remove Validate invalidate setlayout

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 an Applet 12

AWT Class Hierarchy Component Container Button List Checkbox Choice Label Window Panel Frame Applet Note: There are more classes, however, these are what are covered in this chapter TextComponent TextField TextArea

Example: A "Life" applet Container (Applet) Containers (Panels) Component (Canvas) Components (Buttons) Components (TextFields) Components (Labels) 14

Applets An application has a public static void main(string args[ ]) method, but an Applet usually does not An Applet's main method is in the Browser To write an Applet, you extend Applet and override some of its methods The most important methods are init( ), start( ), and paint(graphics g) 15

To create an applet public class MyApplet extends Applet { } this is the only way to make an Applet You can add components to the applet The best place to add components is in init( ) You can paint directly on the applet, but it s better to paint on a contained component Do all painting from paint(graphics g) 16

Some types of components Label Button Checkbox Choice TextField List Scrollbar TextArea Button Checkbox CheckboxGroup 17

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); 18

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);... 19

Creating a Frame When you create an Applet, you get a Panel for free When you write a GUI for an application, you need to create and use a Frame: Frame frame = new Frame(); frame.settitle("my Frame"); frame.setsize(300, 200); // width, height... add components... frame.setvisible(true); Or: class MyClass extends Frame {... settitle("my Frame"); // in some instance method 20

Arranging components Every Container has a layout manager The default layout for a Panel is FlowLayout An Applet is a Panel Therefore, 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 The default layout for a frame is BorderLayout. 21

FlowLayout Use add(component); to add to a component when 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 22

Complete example: FlowLayout import java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet { public void init () { setlayout (new FlowLayout ()); // default add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); } } 23

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 (new Button("NORTH"), BorderLayout.NORTH); 24

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

Complete example: BorderLayout import java.awt.*; import java.applet.*; public class BorderLayoutExample extends Applet { public void init () { setlayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); } } 26

Using a Panel Panel p = new Panel(); add (p, BorderLayout.SOUTH); p.add (new Button ("Button 1")); p.add (new Button ("Button 2")); 27

GridLayout The GridLayout manager divides the container up into a given number of rows and columns: new GridLayout(rows, columns) All sections of the grid are equally sized and as large as possible 28

Complete example: GridLayout import java.awt.*; import java.applet.*; public class GridLayoutExample extends Applet { public void init () { setlayout(new GridLayout(2, 3)); add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } } 29

What if I don t want a LayoutManager? LayoutManagers have proved to be difficult and frustrating to deal with. The LayoutManager can be removed from a Container by invoking its setlayout method with a null parameter. Panel apanel = new Panel(); apanel.setlayout(null); 30

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 31

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 32

Writing a Listener For a Button, you need an ActionListener b1.addactionlistener (new MyButtonListener ( )); An ActionListener must have an actionperformed(actionevent) method public void actionperformed(actionevent e) { } 33

MyButtonListener public void init () {... b1.addactionlistener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionperformed (ActionEvent e) { showstatus ("Ouch!"); } } 34

Listeners for TextFields An ActionListener listens for someone hitting the Enter key An ActionListener requires this method: public void actionperformed (ActionEvent e) You can use gettext( ) to get the text A TextListener listens for any and all keys A TextListener requires this method: public void textvaluechanged(textevent e) 35

Event Sources and Their Listeners Dialog - WindowListener Frame - WindowListener Button - ActionListener Choice - ItemListener Checkbox ItemListener List - ItemListener, ActionListener (when an item is double clicked) Scrollbar - AdjustmentListener TextField - ActionListener, TextListener TextArea - TextListener 36

Simple AWT Example import java.awt.*; import java.awt.event.*; public class SimpleAWT extends java.applet.applet implements ActionListener, ItemListener { private Button button = new Button("Push Me!"); private Checkbox checkbox = new Checkbox("Check Me!"); private Choice choice = new Choice(); private Label label = new Label("Pick something!"); public void init() { button.addactionlistener(this); checkbox.additemlistener(this); choice.additemlistener(this); // An Applet is a Container because it extends Panel. setlayout(new BorderLayout()); choice.additem("red"); choice.additem("green"); choice.additem("blue"); Panel panel = new Panel(); panel.add(button); panel.add(checkbox); panel.add(choice); add(label, "Center"); add(panel, "South"); } 37

Simple AWT Example Cont. public void actionperformed(actionevent e) { if (e.getsource() == button) { label.settext("the Button was pushed."); } } public void itemstatechanged(itemevent e) { if (e.getsource() == checkbox) { label.settext("the Checkbox is now " + checkbox.getstate() + "."); } else if (e.getsource() == choice) { label.settext(choice.getselecteditem() + was selected. ); } } } 38

AWT and Swing AWT Buttons vs. Swing JButtons: A Button is a Component A JButton is an AbstractButton, which is a JComponent, which is a Container, which is a Component Containers: Swing uses AWT Containers AWT Frames vs. Swing JFrames: A Frame is a Window is a Container is a Component A JFrame is a Frame, etc. Layout managers: Swing uses the AWT layout managers, plus a couple of its own Listeners: Swing uses many of the AWT listeners, plus a couple of its own Bottom line: Not only is there a lot of similarity between AWT and Swing, but Swing actually uses much of the AWT 39

Graphics It is possible to draw lines and various shapes within a Panel under the AWT. Each Component contains a Graphics object which defines a Graphics Context which can be obtained by a call to getgraphics(). Common methods used in Graphics include: drawline drawoval drawrect drawroundrect drawarc fillarc filloval fillrect fillroundrect setcolor setfont drawimage drawstring

Java Graphics To start, here s a basic applet that demonstrates Java graphics using AWT: import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); Try to compile this g.fillrect(10, 20, 40, 40); code and run it as } // end paint() an applet. } // end class BasicGraphics What do you see?

import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.fillrect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics The first lines are import statements, to load the AWT and Applet libraries. If you were making a Swing version, you would load Swing libraries.

import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.fillrect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics Our class is called BasicGraphics, and it extends Applet to inherit Applet properties.

import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.fillrect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics Inside the applet, we have just one method: paint() (Remember from Applets that other methods are optional.) It has one parameter, called the abstract Graphics object, and we call it g. Get used to this, you need to tell paint() what to paint on!

import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.fillrect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics paint() has two methods inside of it: setcolor and fillrect g.setcolor(color.red); is the command to color whatever graphic thing we have red. The computer still doesn t know what Graphic thing g is going to be!

import java.awt.*; import java.applet.applet; public class BasicGraphics extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.fillrect(10, 20, 40, 40); } // end paint() } // end class BasicGraphics g.fillrect(10, 20, 40, 40) tells the applet to make g into a fillrect, which is a filled rectangle. 10 is the starting x- position in the applet, 20 is the starting y- position in the applet 40 is the width and the other 40 is the height. Remember what color will fill it? Red!

Summary I: Building a GUI Create a container, such as Frame or Applet Choose a layout manager Create more complex layouts by adding Panels; each Panel can have its own layout manager Create other components and add them to whichever Panels you like 47

Summary II: Building a GUI For each active component, look up what kind of Listeners it can have Create (implement) the Listeners often there is one Listener for each active component Active components can share the same Listener For each Listener you implement, supply the methods that it requires For Applets, write the necessary HTML 48

Vocabulary AWT The Abstract Window Toolkit provides basic graphics tools (tools for putting information on the screen) Swing A much better set of graphics tools Container a graphic element that can hold other graphic elements (and is itself a Component) Component a graphic element (such as a Button or a TextArea) provided by a graphics toolkit listener A piece of code that is activated when a particular kind of event occurs layout manager An object whose job it is to arrange Components in a Container 49