Graphical User Interfaces

Similar documents
Graphical User Interfaces. Comp 152

Graphical User Interfaces 2

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

Graphical User Interfaces 2

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

Graphical User Interfaces 2

Graphic User Interfaces. - GUI concepts - Swing - AWT

Part I: Learn Common Graphics Components

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

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!

MIT AITI Swing Event Model Lecture 17

H212 Introduction to Software Systems Honors

Introduction. Introduction

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

Graphical User Interface (GUI)

Java Programming Lecture 6

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

Window Interfaces Using Swing Objects

Agenda. Container and Component

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

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

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

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

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

Graphical User Interface

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

Dr. Hikmat A. M. AbdelJaber

Window Interfaces Using Swing Objects

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

2110: GUIS: Graphical User Interfaces

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

Building Java Programs Bonus Slides

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

We are on the GUI fast track path

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

Graphical User Interface (GUI)

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

Graphical User Interfaces (GUIs)

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

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

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

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

Chapter 12 Advanced GUIs and Graphics

Java IDE Programming-I

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

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

CS 2113 Software Engineering

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Systems Programming Graphical User Interfaces

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

Basicsof. JavaGUI and SWING

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

Unit 6: Graphical User Interface

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

CSIS 10A Assignment 7 SOLUTIONS

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

CS 251 Intermediate Programming GUIs: Components and Layout

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Charlie Garrod Michael Hilton

To gain experience using GUI components and listeners.

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

Chapter 6: Graphical User Interfaces

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chris9an Kästner Charlie Garrod

Lecture 9. Lecture

Java. GUI building with the AWT

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

JAVA NOTES GRAPHICAL USER INTERFACES

CSC 1214: Object-Oriented Programming

User interfaces and Swing

Class 16: The Swing Event Model

Lecture 18 Java Graphics and GUIs

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

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

JFrame In Swing, a JFrame is similar to a window in your operating system

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Programming graphics

Sketchpad. Plan for Today. Class 22: Graphical User Interfaces IBM 705 (1954) Computer as Clerk : Augmenting Human Intellect

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.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

COMPSCI 230. Software Design and Construction. Swing

Jonathan Aldrich Charlie Garrod

Laying Out Components. What is Widget Layout?

Containers and Components

SINGLE EVENT HANDLING

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Graphical User Interface (GUI)

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

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

SD Module-1 Advanced JAVA

BASICS OF GRAPHICAL APPS

Final Examination Semester 2 / Year 2011

Chapter 17 Creating User Interfaces

SD Module-1 Advanced JAVA. Assignment No. 4

Transcription:

Graphical User Interfaces CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011

Overview Command line versus GUI apps Java GUI applica;ons JFrame Layout managers Popular widgets Events Ac;on listeners Inner listener classes Drawing things (without StdDraw) JPanel 2

User interfaces Command Line Interface (CLI) Good for experts Automa;ng thing via scripts (e.g. DOS batch files) Easy to use remotely (e.g. over SSH) Low resource consump;on Graphical User Interface (GUI) Good for novices Difficult to automate (e.g. macros in Office) Good for presen;ng mul;ple views, graphical data Remote access difficult (e.g. VNC, Remote Desktop) High resource consump;on 3

Thus far: StdDraw Good for drawing But no GUI widgets: BuVons Combo boxes Text fields Dialog boxes Java GUIs Today: doing it ourselves Use the Java API to create a GUI Crea;ng: common widgets, area to draw on In prac;ce, owen done using a GUI builder 4

JFrame Top- level container Container that holds all the GUI elements Created by main() method 5

JFrame example GUI with a single giant buvon BuVon doesn't do anything (yet) import javax.swing.*; public class SimpleButton public static void main(string [] args) JFrame frame = new JFrame(); JButton button = new JButton("click me!"); frame.getcontentpane().add(button); Create the frame and a buvon. Add the buvon to the content por;on of the frame. frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(300, 300); frame.setvisible(true); Make a reasonable size and actually display. Makes the X box close everything. SimpleButton.java 6

Layout manager Adding mul;ple things Handles arranging mul;ple things in the frame BorderLayout 5 regions: NORTH, SOUTH, EAST, WEST, CENTER Default type for a frame MultiButtons.java MultiButtonsFlow.java FlowLayout LeW to right Creates mul;ple rows if needed Default type for a panel (more on panels in a bit) 7

Adding mul;ple things BoxLayout Ver;cal stack of components BoxLayout.Y_AXIS Horizontal row BoxLayout.X_AXIS Won't rearrange if you resize window MultiButtonsBox.java 8

Handy JFrame methods Some JFrame methods: Method JFrame() JFrame(String title) setlayout(layoutmanager mgr) setdefaultcloseoperation(int op) setsize(int width, int height) setvisible(boolean b) getcontentpane() settitle(string s) Descrip.on Default constructor, a window with no name Constructor, give the window a ;tle Change the layout manager for the frame What to do when the frame is closed Sets to the given width and height Show or hide depending on value of b Returns the content pane where we add things Change the ;tle of the window 9

Handy JBuVon methods Some JBuVon methods: Method JButton(String s) settext(string s) setenabled(boolean b) setpreferredsize(dimension d) Descrip.on Constructor a new buvon with the given label Change the buvon s label Enables or disables the buvon Tell layout manager how big you would like the buvon to be 10

Widget'o'rama JButton JLabel JTextField JCheckBox JTextArea JSlider JRadioButton ButtonGroup JSeparator Widgets.java JComboBox 11

Events Something triggers Usually the user Examples: User clicks buvon Moves the mouse Changing selec;on in a combo box list GUI events 12

Event listener Event listeners and sources Code called when an event occurs Done by implemen;ng a Java interface Which one depends on what you care about Register listener with the object Event source The object that generated the event e.g. the JBuVon object Normally you'll be handling events Not genera;ng them 13

BuVon counter example Single buvon window Every ;me buvon is pushed, increment counter Display counter as label of the buvon 14

import javax.swing.*; import java.awt.event.*; public class ButtonCount implements ActionListener private int count = 0; private JButton button; public void actionperformed(actionevent event) count++; button.settext("count = " + count); public void go() JFrame frame = new JFrame(); button = new JButton("count = " + count); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(button); frame.setsize(300,300); frame.setvisible(true); button.addactionlistener(this); public static void main(string [] args) ButtonCount gui = new ButtonCount(); gui.go(); ButtonCount.java 15

Mul;ple listeners Listening for mul;ple buvons Op;on 1: Single actionperformed()method Use the passed in event object Test which buvon object triggered the method Op;on 2: Mul;ple inner classes Each classes implements actionlistener Each has its own actionperformed() method NOTE: can use private instance vars of parent 16

Op;on 1: Single listener approach public class FarmListener implements ActionListener private JButton buttoncow; private JButton buttondog; private JButton buttoncat; public void actionperformed(actionevent event) if (event.getsource() == buttoncow) StdAudio.play("cow.wav"); else if (event.getsource() == buttondog) StdAudio.play("dog.wav"); else if (event.getsource() == buttoncat) StdAudio.play("cat.wav"); public void go()... buttoncow.addactionlistener(this); buttondog.addactionlistener(this); buttoncat.addactionlistener(this);... FarmListener.java 17

Op;on 2: Inner listener approach public class FarmInner private JButton buttoncow; private JButton buttondog; private JButton buttoncat; class CowListener implements ActionListener public void actionperformed(actionevent event) StdAudio.play("cow.wav"); class DogListener implements ActionListener public void actionperformed(actionevent event) StdAudio.play("dog.wav"); class CatListener implements ActionListener public void actionperformed(actionevent event) StdAudio.play("cat.wav"); public void go()... buttoncow.addactionlistener(new CowListener()); buttondog.addactionlistener(new DogListener()); buttoncat.addactionlistener(new CatListener());... FarmInner.java 18

Building Java GUIs JFrame basis for GUI Summary Choice of layout manager We looked at just three: BorderLayout, FlowLayout, BoxLayout Handles where widgets such as buvons appear Variety of widgets Event handling Widgets trigger events No;fy registered listeners Single listener that handles mul;ple widget Separate inner class for each widget 19