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

Similar documents
GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

H212 Introduction to Software Systems Honors

Programmierpraktikum

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Introduction to the JAVA UI classes Advanced HCI IAT351

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Lecture 9. Lecture

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

Programming Languages and Techniques (CIS120)

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

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

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!

Window Interfaces Using Swing Objects

Graphical User Interfaces 2

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

MIT AITI Swing Event Model Lecture 17

Graphical User Interfaces. Comp 152

Graphical User Interfaces 2

We are on the GUI fast track path

Class 16: The Swing Event Model

Chapter Nine: Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2008 by John Wiley & Sons. All rights reserved.

3/7/2012. Chapter Nine: Interfaces and Polymorphism. Chapter Goals

10/27/2011. Chapter Goals

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Graphical User Interfaces 2

GUI Event Handlers (Part I)

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

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

Final Examination Semester 2 / Year 2010

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Final Examination Semester 2 / Year 2012

AP CS Unit 11: Graphics and Events

Window Interfaces Using Swing Objects

Graphical User Interface (GUI)

Chapter 9 Interfaces and Polymorphism. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Programming graphics

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

Final Examination Semester 2 / Year 2011

Java Swing Introduction

SINGLE EVENT HANDLING

Original GUIs. IntroGUI 1

Programming Languages and Techniques (CIS120e)

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

CSE wi Final Exam 3/12/18 Sample Solution

Jonathan Aldrich Charlie Garrod

2.2 Input and Output. Input and Output. Digital Michelangelo Project. Terminal. Input devices. Output devices.

Graphical User Interfaces

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

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

Object Orientated Programming in Java. Benjamin Kenwright

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

Midterm assessment - MAKEUP Fall 2010

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

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 :) )

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Swing from A to Z Some Simple Components. Preface

CP122 CS I. Abstract Interfaces, Graphical User Interfaces, Inheritance

Graphical User Interface (GUI)

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CS 251 Intermediate Programming GUIs: Components and Layout

Graphical User Interface (GUI)

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java: Graphical User Interfaces (GUI)

Packages: Putting Classes Together

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

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

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

CS415 Human Computer Interaction

COMPSCI 230. Software Design and Construction. Swing

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

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

DM503 Programming B. Peter Schneider-Kamp.

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

CS108, Stanford Handout #22. Thread 3 GUI

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interface

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Event Driven Programming

SampleApp.java. Page 1

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

User interfaces and Swing

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

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

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

CSE 331 Software Design & Implementation

Introduction to concurrency and GUIs

Transcription:

CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical user interface A graphical application shows information inside a frame: a window with a title bar Java s JFrame class allows you to display a frame It is part of the javax.swing package Page 2 The JFrame Class Five steps to displaying a frame: 1) Construct an object of the JFrame class JFrame frame = new JFrame(); 2) Set the size of the frame frame.setsize(300,400); 3) Set the title of the frame frame.settitle( An Empty Frame ); 4) Set the default close operation frame.setdefaultcloseoperation(jframe.exit_on_close); 5) Make it visible frame.setvisible (true); Page 3

EmptyFrameViewer.java Your JVM (Java Virtual Machine does all of the work of displaying the frame on your GUI This application is portable to all supported GUIs! You are using the java Swing library Page 4 Adding Components You cannot draw directly on a JFrame object Instead, construct an object and add it to the frame A few examples objects to add are: JComponent Jpanel JTextComponent JLabel public class RectangleComponent extends JComponent public void paintcomponent(graphics g) // Drawing instructions go here Extend the JComponent Class and override its paintcomponent method Page 5 Adding Panels If you have more than one component, put them into a panel (a container for other user-interface components), and then add the panel to the frame: First Create the components JButton button = new JButton("Click me!"); JLabel label = new JLabel("Hello, World!"); Then Add them to the panel JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); Use a JPanel to group multiple user-interface components together. Add the panel to the frame Page 6

FilledFrameViewer.java Page 7 Using Inheritance to Customize Frames For complex frames: Design a subclass of JFrame Store the components as instance variables Initialize them in the constructor of your subclass. public class FilledFrame extends JFrame private JButton button; Components are instance variables private JLabel label; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 100; public FilledFrame() Initialize and add them in the constructor of your subclass with createcomponents(); a helper method setsize(frame_width, FRAME_HEIGHT); Page 8 Using Inheritance to Customize Frames Then instantiate the customized frame from the main method public class FilledFrameViewer2 public static void main(string[] args) JFrame frame = new FilledFrame(); frame.settitle("a frame with two components"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); Page 9

Special Topic 10.1 Adding the main Method to the Frame Class Some programmers prefer this technique public class FilledFrame extends JFrame Once main has instantiated... the FilledFrame, non-static public static void main(string[] args) instance variables and methods can be used. JFrame frame = new FilledFrame(); frame.settitle("a frame with two components"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); public FilledFrame() createcomponents(); setsize(frame_width, FRAME_HEIGHT);... Page 10 10.2 Events and Event Handling In a modern graphical user interface program, the user controls the program through the mouse and keyboard. The user can enter information into text fields, pull down menus, click buttons, and drag scroll bars in any order. The program must react to the user commands The program can choose to receive and handle events such as mouse move or a button push action event Page 11 Events and Action Listeners Programs must indicate which events it wants to receive It does so by installing event listener objects An event listener object belongs to a class that you declare The methods of your event listener classes contain the instructions that you want to have executed when the events occur To install a listener, you need to know the event source You add an event listener object to selected event sources: Examples: OK Button clicked, Cancel Button clicked, Menu Choice.. Whenever the event occurs, the event source calls the appropriate methods of all attached event listeners Page 12

Example ActionListener The ActionListener interface has one method: public interface ActionListener void actionperformed(actionevent event); ClickListener class implements the ActionListener interface We can ignore the event parameter it has information such as when the event occurred Page 13 Registering ActionListener A ClickListener object must be created, and then registered (added) to a specific event source ActionListener listener = new ClickListener(); button.addactionlistener(listener); Now whenever the button object is clicked, it will call listener.actionperformed, passing it the event as a parameter Page 14 ButtonFrame1.java Creates and adds a JButton to the frame Tells the button to call us back when an event occurs. Page 15

ButtonViewer1.java No changes required to the main to implement an event handler Page 16