Introduction. Introduction

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

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

Agenda. Container and Component

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

Window Interfaces Using Swing Objects

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Java. GUI building with the AWT

MIT AITI Swing Event Model Lecture 17

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

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

Window Interfaces Using Swing Objects

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

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

To gain experience using GUI components and listeners.

EVENTS, EVENT SOURCES AND LISTENERS

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Systems Programming Graphical User Interfaces

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

Graphical User Interfaces. Comp 152

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

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

Points Missed on Page page 1 of 8

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

CHAPTER 2. Java Overview

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

SINGLE EVENT HANDLING

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Part I: Learn Common Graphics Components

2110: GUIS: Graphical User Interfaces

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. Chapter 12

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

Chapter 7: A First Look at GUI Applications

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

CSC 1214: Object-Oriented Programming

Java Programming Lecture 6

CSIS 10A Assignment 7 SOLUTIONS

User interfaces and Swing

Graphical User Interfaces

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

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

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

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!

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

Graphical User Interfaces (GUIs)

Swing from A to Z Some Simple Components. Preface

Dr. Hikmat A. M. AbdelJaber

JAVA NOTES GRAPHICAL USER INTERFACES

Top-Level Containers

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Java Applets / Flash

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Swing/GUI Cheat Sheet

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

Class 16: The Swing Event Model

Introduction to the JAVA UI classes Advanced HCI IAT351

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Lab 5 Classy Chat. XMPP Client Implementation --- Part 2 Due Oct. 19 at 11PM

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

CS 180 Fall 2006 Exam II

Using Several Components

DM503 Programming B. Peter Schneider-Kamp.

CS 251 Intermediate Programming GUIs: Components and Layout

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

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

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

Graphical User Interfaces in Java - SWING

CS 106A, Lecture 23 Interactors and GCanvas

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

AP CS Unit 11: Graphics and Events

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

Together, the appearance and how user interacts with the program are known as the program look and feel.

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

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

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

Example: Building a Java GUI

Graphical User Interfaces in Java

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

Original GUIs. IntroGUI 1

Example: Building a Java GUI

Chapter 12 Advanced GUIs and Graphics

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

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

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

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

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

RAIK 183H Examination 2 Solution. November 11, 2013

CS 2113 Software Engineering

INTRODUCTION TO (GUIS)

Java - Applets. public class Buttons extends Applet implements ActionListener

Programming Languages and Techniques (CIS120e)

Advanced Java. Guide 108. Version 1.2

Swing Programming Example Number 2

Transcription:

Introduction Many Java application use a graphical user interface or GUI (pronounced gooey ). A GUI is a graphical window or windows that provide interaction with the user. GUI s accept input from: the keyboard a mouse. A window in a GUI consists of components that: present data to the user allow interaction with the application. 7-1 Introduction Some common GUI components are: buttons, labels, text fields, check boxes, radio buttons, combo boxes, and sliders. 7-2 1

Text Fields provide a line of user-updatable text need to - import javax.swing.*; - declare a variable - construct input = new JTextField( "Enter text here " ); - place in a window - use. For example, String userinput = input.gettext(); import java.awt.*; import javax.swing.*; public class GUIDemo extends JApplet { public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); Container contentpane = getcontentpane(); contentpane.add( input, BorderLayout.NORTH ); contentpane.validate(); 2

JLabel class provided by Swing Single line of read-only text A passive component - no event listener required! Useful for providing labels for sliders, text fields, etc. Labels Using JLabels Two ways to construct Providing text only JLabel speedlabel = new JLabel("Speed is "); Providing text and justification JLabel speedlabel = new JLabel("Speed is ", JLabel.RIGHT); Modifiable with settext method speedlabel.settext("speed is " + speed); 3

import java.awt.*; import javax.swing.*; public class GUIDemo2 extends JApplet { private JLabel label; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); Container contentpane = getcontentpane(); contentpane.add( input, BorderLayout.NORTH ); contentpane.add( label, BorderLayout.SOUTH ); contentpane.validate(); Buttons Let s add a button to our previous program. Want clicks on the button to trigger a program action Requires slightly more programming effort than text fields 4

Checklist for using JButtons Construct the JButton Add it to the content pane of the JApplet and validate So that the JApplet responds to events generated by the JButton Add this as a listener Make sure your JApplet implements ActionListener requires to add an actionperformed method Action Events When a JButton is clicked an ActionEvent is generated actionperformed is executed Just as onmouseclick is executed whenever a user clicks on the canvas actionperformed is provided with a parameter Contains information about the object that triggered the event 5

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo3 extends JApplet implements ActionListener { private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addactionlistener(this); Container contentpane = getcontentpane(); contentpane.add( input, BorderLayout.NORTH ); contentpane.add( label, BorderLayout.SOUTH ); contentpane.add( button, BorderLayout.CENTER); contentpane.validate(); public void actionperformed(actionevent e){ label.settext(input.gettext()); 6

Layout of Components in a Container Many layout options One example: BorderLayout (default JApplet contentpane) BorderLayout.NORTH BorderLayout.EAST BorderLayout.SOUTH BorderLayout.WEST BorderLayout.CENTER Sizes everything to fill space 7

FlowLayout Alternate layout that sequences components left to right, allows to keep natural size import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo4 extends JApplet implements ActionListener { private JLabel label; private JButton button; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("click"); button.addactionlistener(this); Container contentpane = getcontentpane(); contentpane.setlayout( new FlowLayout()); contentpane.add( input); // removed NORTH, SOUTH, etc contentpane.add( label ); contentpane.add( button); contentpane.validate(); public void actionperformed(actionevent e){ label.settext(input.gettext()); 8

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUIDemo5 extends JApplet implements ActionListener { private JLabel label; private JButton button, button2; public void init() { //init is called when the applet starts input = new JTextField( "Enter text " ); label = new JLabel("label"); button = new JButton("update"); button.addactionlistener(this); button2 = new JButton("font"); button2.addactionlistener(this); Container contentpane = getcontentpane(); contentpane.setlayout( new FlowLayout()); contentpane.add( input); contentpane.add( label ); contentpane.add( button); contentpane.add( button2); contentpane.validate(); 9

Processing multiple buttons Use e.getsource() to tell which was clicked public void actionperformed(actionevent e){ if (e.getsource()==button){ label.settext(input.gettext()); if (e.getsource()==button2){ label.setfont( new Font("Arial",Font.PLAIN,20)); 10

Your Assignment Add more buttons, labels, and text fields buttons: copy box1 to box2 copy box2 to box1 exchange boxes (use a temp string to hold one) google swap two string variables make all the fonts grow by 20 percent (*1.2) Review Construct the GUI component Add the component to a container (i.e., a panel or the content pane of a window) If a listener is needed Add this as a listener for the GUI component Add the event-handling methods promised by the listener interface 11