Graphical Interfaces

Similar documents
Graphical Interfaces

Chapter 1 GUI Applications

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

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

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

Programming Languages and Techniques (CIS120e)

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

1 of :32:42

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

FORMAS DE IMPLEMENTAR LOS OYENTES. A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand.

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

BM214E Object Oriented Programming Lecture 13

The AWT Event Model 9

MIT AITI Swing Event Model Lecture 17

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Packages: Putting Classes Together

Interacción con GUIs

CSE 8B Intro to CS: Java

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Programming Language Concepts: Lecture 8

RAIK 183H Examination 2 Solution. November 10, 2014

Building Graphical User Interfaces. GUI Principles

RAIK 183H Examination 2 Solution. November 11, 2013

(Incomplete) History of GUIs

GUI Programming: Swing and Event Handling

Functors - Objects That Act Like Functions

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI in Java TalentHome Solutions

Building Graphical User Interfaces. Overview

Swing from A to Z Some Simple Components. Preface

Chapter 14. More Swing

The AWT Package, Placing Components in Containers, CardLayout. Preface. Introduction

Graphical User Interfaces (GUIs)

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

An Introduction To Graphical User Interfaces

7. Program Frameworks

!"# $ %&# %####' #&() % # # # #&* # ## +, # -

AWT TEXTFIELD CLASS. Constructs a new empty text field with the specified number of columns.

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

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

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Systems Programming Graphical User Interfaces

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

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

Introduction. Introduction

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

AWT DIALOG CLASS. Dialog control represents a top-level window with a title and a border used to take some form of input from the user.

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Advanced Java Programming (17625) Event Handling. 20 Marks

SINGLE EVENT HANDLING

Java. GUI building with the AWT

Graphical User Interfaces in Java - SWING

Java AWT Windows, Text, & Graphics

AWT CHOICE CLASS. Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu.

SD Module-1 Advanced JAVA

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

The Abstract Window Toolkit

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

SD Module-1 Advanced JAVA. Assignment No. 4

Graphical User Interfaces

Programming Mobile Devices J2SE GUI

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Points Missed on Page page 1 of 8

Window Interfaces Using Swing. Chapter 12

Agenda. Container and Component

G51PRG: Introduction to Programming Second semester Applets and graphics

Laborator 2 Aplicatii Java

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

Unit 7: Event driven programming

Chapter 13 Lab Advanced GUI Applications

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Introduction to the JAVA UI classes Advanced HCI IAT351

CS11 Java. Fall Lecture 4

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

CHAPTER 2. Java Overview

Class 16: The Swing Event Model

CSA 1019 Imperative and OO Programming

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

1005ICT Object Oriented Programming Lecture Notes

An Introduction To Graphical User Interfaces

Graphical User Interfaces. Comp 152

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Tecniche di Progettazione: Design Patterns

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Chapter 11 Handling Exceptions and Events. Chapter Objectives

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Transcription:

Weeks 11&12 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The components of a graphical interface are control elements named widgets activated by the user with the mouse or the keyboard. When activated, a control element will generate a certain answer, according to the program. Most graphical interfaces are based on windows. This means that each active application will have a rectangular area on the screen. All the necessary control elements for the user-application dialog will be placed into this window. In order for an application using the graphical interface to run we need a graphical server to put the screen in the graphical state and to offer the necessary functions to draw and control GUI components. How to create an application window A graphical application will always have a main window. You can create this window using the class java.awt.frame which contains the necessary elements to build a window. class MyWindow extends Frame public MyWindow(String title) super(title); setsize(w/2, h/2); setlocation(w/4, h/4); class FirstWindow public static void main(string[] args) Frame f = new MyWindow("My first window"); f.setvisible(true); When the user acts in some way over an element of a GUI using the mouse or the keyboard, an event will appear, the GUI s element being the source of the event. If you want an answer from an 1

element you should handle the event. This means that someone has to catch the event and to execute a certain function. Handling events in Java When you act over a GUI s element, an object a predefined class (java.awt.event) will be created. If there is a listener object of the created event and this object is known to the source of the event, then a method having as parameter the event object will be called. Of course, the programmer should define first this method. Different elements will generate different events. When you act over a window a java.awt.event.windowevent object will be created and when acting over a button a java.awt.event.actionevent object will be created. The event handler is an object having methods with predefined names from predefined interfaces. There is an interface for every event type. For example, to handle a WindowEvent one has to implement WindowListener interface. The method s name reflects the action executed over an element. The interface WindowListener has the following methods: public void windowactivated(windowevent e); public void windowclosed(windowevent e); public void windowclosing(windowevent e); public void windowdeactivated(windowevent e); public void windowdeiconified(windowevent e); public void windowiconified(windowevent e); public void windowopened(windowevent e); To link an event handler to its source the following syntax is needed: EventSourceObject.addEvenimentListener(EventHandlerObject); import java.awt.event.*; class MyWindowHandler implements WindowListener public void windowclosing(windowevent e) System.exit(0); // all the methods from WindowListener interface class MyWindow extends Frame public MyWindow(String title) 2

super(title); MyWindowHandler r = new MyWindowHandler(); this.addwindowlistener(r); setsize(w/2, h/2); setlocation(w/4, h/4); class FirstWindow public static void main(string[] args) Frame f = new MyWindow("The first window"); f.sevisible(true); How to put elements in a window? You have to create objects of the classes which model the elements. Then, for each element you have to use the method add to place them into the window. You can specify where to put an element in the window using layout managers like FlowLayout or BorderLayout. A button in Java is modeled in the class called Button. A button can generate an ActionEvent, when the user clicks on it. The handler of such an event should implement the interface ActionListener, which contains only a method: actionperformed. To an easy control of the relations from the GUI s elements, the applications should be designed accordingly with the following designing template: a class to describe the window; a class for the handler of the possible events; a class to mediate the dialogs, which creates the GUI s elements, put them into the window and, after an event, updates the elements state. import java.awt.event.*; public class MyWindow extends WindowAdapter implements ActionListener private int counter=0; private Frame f; private Button b; 3

private Label lb; public void assembleframe() f = new Frame("My first window"); f.setsize(w/2, h/2); f.setlocation(w/4, h/4); f.addwindowlistener(this); b = new Button("Push"); b.addactionlistener(this); lb = new Label("0"); f.setlayout(new FlowLayout()); f.add(b); f.add(lb); f.setvisible(true); public void windowclosing(windowevent e) System.exit(0); public void actionperformed(actionevent e) counter++; lb.settext(""+counter); public static void main(string[] args) MyWindow win = new MyWindow (); win.assembleframe(); 4

If there are more than one buttons in a window, you can use the following situations for their handlers: create two different classes for the event handlers of the two buttons and define accordingly the method actionperformed; use the information from the generated ActionEvent object, sent as parameter to the method actionperformed. A possibility is to use the method public Object getsource(); Problem week 11 Change the above example to have two buttons, a label and a checkbox. By default, button 1 is enabled, button 2 is disabled and the checkbox is unchecked. When you check the checkbox, button 2 will be enabled and button 1 will be disabled. When you push button 1, the label s content will be incremented and pushing button 2 the label s content will be decremented. The starting label s value is 0. Reading data from the keyboard To read data from the keyboard, a TextField object is needed. The following example shows you how to read the elements of an array using: o a text field to read the elements from the keyboard, o a button to insert an element into a location in the array, o a label to point the index of the current element in the array. import java.awt.event.*; class Table private int[] tab; public Table(int n) tab=new int[n]; public void setelem(int i, int val) tab[i]=val; public int getdim() return tab.length; 5

public class MyWindow extends WindowAdapter implements ActionListener private int counter=0; private Frame f; private Button b; private Label lb; private TextField tx; Table t; public void assembleframe() t = new Table(10); f = new Frame("My first window"); f.setsize(w/2, h/2); f.setlocation(w/4, h/4); f.addwindowlistener(this); b = new Button("Insert"); b.addactionlistener(this); lb = new Label("Element 0 "); tx = new TextField(15); f.setlayout(new FlowLayout()); f.add(b); f.add(tx); f.add(lb); f.setvisible(true); public void windowclosing(windowevent e) System.exit(0); public void actionperformed(actionevent e) 6

if (counter < t.getdim()) try int val = Integer.parseInt(tx.getText()); t.setelem(counter,val); counter++; lb.settext("element "+counter); tx.settext(""); catch (NumberFormatException exp) lb.settext("wrong format number"); else lb.settext("array full"); public static void main(string[] args) MyWindow l = new MyWindow (); l.assembleframe(); Problem week 12 Create a GUI to contain a text field, text area, a button and a label. If there is a text written in the text field, pressing the button will move the text from the text field to the text area, otherwise a dialog will appear pointing that there is no text in the text field. The label will show how many values are written in the text area. The program will end when inside the text field the key e is written. 7