Graphical User Interfaces. Comp 152

Similar documents
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!

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

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

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

Programming graphics

Graphical User Interfaces

Window Interfaces Using Swing Objects

Introduction to the JAVA UI classes Advanced HCI IAT351

Window Interfaces Using Swing Objects

Object Oriented Programming

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Part I: Learn Common Graphics Components

Chapter 6: Graphical User Interfaces

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

CS111: PROGRAMMING LANGUAGE II

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

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

Systems Programming Graphical User Interfaces

Chapter 17 Creating User Interfaces

Agenda. Container and Component

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

Java Programming Lecture 6

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

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

Chapter 18. Advanced graphics programming

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

2110: GUIS: Graphical User Interfaces

Java IDE Programming-I

Unit 6: Graphical User Interface

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

Java. GUI building with the AWT

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

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

Introduction. Introduction

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

Basicsof. JavaGUI and SWING

Graphical User Interface (GUI)

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

Outline CSE 3461 F10. What is a Widget? Properties of Widgets. A self-contained screen object Also called a control Examples of widgets:

Sri Vidya College of Engineering & Technology

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

EVENTS, EVENT SOURCES AND LISTENERS

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

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

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

Graphical User Interfaces in Java - SWING

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

Dr. Hikmat A. M. AbdelJaber

Chapter 7: A First Look at GUI Applications

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Swing/GUI Cheat Sheet

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphical User Interface (GUI)

Graphical User Interfaces (GUIs)

To gain experience using GUI components and listeners.

Today. cisc3120-fall2012-parsons-lectiii.3 2

CS 251 Intermediate Programming GUIs: Components and Layout

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

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Window Interfaces Using Swing. Chapter 12

BASICS OF GRAPHICAL APPS

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

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

CSC 1214: Object-Oriented Programming

Swing. By Iqtidar Ali

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

MIT AITI Swing Event Model Lecture 17

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

Lecture 9. Lecture

Chapter 12 Advanced GUIs and Graphics

CS 2113 Software Engineering

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

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

GUI in Java TalentHome Solutions

Swing from A to Z Some Simple Components. Preface

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

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

H212 Introduction to Software Systems Honors

GUI Event Handlers (Part II)

Graphical User Interface

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

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

AP CS Unit 11: Graphics and Events

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

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

We are on the GUI fast track path

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Graphical Applications

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

PIC 20A GUI with swing

Transcription:

Graphical User Interfaces Comp 152

Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from start to end. User interaction only at specific points.

Object Oriented programming Somewhat more complex. Create objects Objects interact Methods of one object call methods of another object And vice versa Objects passed around as parameters have methods called on them. All but impossible to determine the exact flow of control But still possible to write code that limits user

Event Driven Programming. Different approach Programmer no longer prescribes flow of control of program. Designs interacting set of classes Which are constantly listening for user input Events User input defines what happens at any one time. Set everything up and let it go. Can't predict user input, so can't predict program control at design time.

Window based programming Window based programming is event-driven By nature. Program waits for user event Responds to event and waits for more. Mouse events Keyboard events Specific kinds of each. Only needs to respond to desired events Ignore others. You the programmer will write the event handlers

Swing Window based programming in Java Library of classes provided by SUN Again not MS-windows, but Window-based Built on earlier AWT toolkit Still some AWT used explicitly. Mostly use swing classes though. Package javax.swing Eg import javax.swing.jframe;

Frame Windows Want to make a general purpose window pop up in java The JFrame class JFrame frame = new JFrame(); frame.setsize(300, 400); frame.settitle("an Empty Frame"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); import javax.swing.*;

What it looks like

Useful JFrame Methods void setsize(int width, int height) void setlocation(int x, int y) Swing coordinates. public void setvisible(boolean b) Show hide window Window there but not visible. Container getcontentpane() Needed to put extra windows into this one. Lots more See API docs from resource page of class page

Inheritance to customize Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components as instance fields Initialize them in the constructor of your subclass If initialization code gets complex, simply add some helper methods

JPanel a class for holding other components put panel into JFrame using JFrame add method JFrame frame = new JFrame(); JPanel holder = new JPanel(); frame.add(holder); now put other controls on holder.

JButtons Basic push button class. is-a component decorated with icon or string push to generate action (that you supply) useful constructors JButton() Creates a button with no set text or icon. JButton(Icon icon) Creates a button with an icon. JButton(String text) Creates a button with text.

JButton Methods A few Important methods public void settext(string text) Sets the button's text. public void seticon(icon defaulticon) Sets the button's default icon. public void addactionlistener(actionlistener l) Adds an ActionListener to the button.

JButton Methods A few Important methods public void settext(string text) Sets the button's text. public void seticon(icon defaulticon) Sets the button's default icon. public void addactionlistener(actionlistener l) Adds an ActionListener to the button.

ActionListener ActionListener interface interface for responding to actionevents Something happened You will implement interface one method in the interface action? void actionperformed(actionevent e) Invoked when an action occurs. button press timer events etc

ActionEvent Objects ActionEvent passed as param to your actionperformed method 4 methods String getactioncommand() Returns the command string associated with this action. this is most interesting text on button for buttons int getmodifiers() Returns the modifier keys held down during this action event. long getwhen() Returns the timestamp of when this event occurred. String paramstring() Returns a parameter string identifying this action event.

JLabel JLabel class for putting up label surprise surprise uneditable text

GUI components. Common/basic components from swing library. JButton JCheckbox JComboBox JList

GUI Components Continued JMenu JRadioButton JSlider JTextField JPasswordField JLable

Basic Layout Managers FlowLayout (panel default) BorderLayout(content pane default) GridLayout GridbagLayout

Layouts Up to now, limited control over layout of components When we use a panel, it arranges the components from the left to the right UI components are arranged by placing them inside containers Each container has a layout manager that directs the arrangement of its components Three useful starter layout managers: border layout flow layout grid layout

Default Panel Layout By default, JPanel places components from left to right and starts a new row when needed Panel layout carried out by FlowLayout layout manager Can set other layout managers panel.setlayout(new BorderLayout());

BorderLayout Border Layout handy layout default for JFrames groups container into five areas: center, north, west, south and east When adding a component, specify the position like this: panel.add( BorderLayout.NORTH, component); Expands each component to fill the entire allotted area If that is not desirable, place each component inside a panel

GridLayout gridlayout Arranges components in grid w/ fixed number of rows/ columns Resizes each component so all have same size Expands each component to fill the entire allotted area Add the components, row by row, left to right: JPanel numberpanel = new JPanel(); numberpanel.setlayout(new GridLayout(4, 3)); numberpanel.add(button7); numberpanel.add(button8); numberpanel.add(button9);

Yes!! another opportunity to excel How do you add two buttons to the north area of a frame? How can you stack three buttons on top of each other?

Check Box Two states: checked and unchecked Use one checkbox for a binary choice Use a group of check boxes when one selection does not exclude another Construct by giving the name in the constructor: JCheckBox italiccheckbox = new JCheckBox("Italic"); Don't place into a button group

Combo Boxes or a large set of choices, use a combo box Uses less space than radio buttons "Combo": combination of a list and a text field The text field displays the name of the current selection if combo box is editable, user can type own selection Use seteditable method

Combo Boxes II Add strings with additem method: JComboBox facenamecombo = new JComboBox(); facenamecombo.additem("serif"); facenamecombo.additem("sansserif");... Get user selection with getselecteditem (return type is Object) String selstring = (String) facenamecombo.getselecteditem(); Select an item with setselecteditem

Radio Buttons small set of mutually exclusive choices, use radio buttons or a combo box only one button can be selected at a time When a button is selected, previously selected button in set is automatically turned off suppose, font sizes are mutually exclusive: (see code on next slide)

Example Radio Buttons JRadioButton smallbutton = new JRadioButton("Small"); JRadioButton mediumbutton = new JRadioButton("Medium"); JRadioButton largebutton = new JRadioButton("Large"); // Add radio buttons into a ButtonGroup so that // only one button in group is on at any time ButtonGroup group = new ButtonGroup(); group.add(smallbutton); group.add(mediumbutton); group.add(largebutton);

Event Handling Radio Buttons, Check Boxes, and Combo Boxes generate an ActionEvent whenever the user selects an item

JSlider Used for selection on a scale

JSlider II two simple constructors public JSlider() Creates a horizontal slider with the range 0 to 100 and an initial value of 50 public JSlider(int min, int max, int value) Creates a horizontal slider using the specified min, max, and value.

Slider Events Slider events handled by ChangeListener method in JSlider public void addchangelistener(changelistener l) ChangeListener interface has a single method: void statechanged(changeevent e) What is a ChangeEvent? one really useful method getsource method from superclass EventObject getsource: tells us which component generated this event

JSlider III to find out where the user moved the slider to public int getvalue() Returns the slider's value.

JTextField JTextField Represents a single line of text. In javax.swing package.

JtextField II Constructors JTextField() Constructs a new TextField. JTextField(int columns) Constructs a new empty TextField with the specified number of columns. JTextField(String text) Constructs a new TextField initialized with the specified text. JTextField(String text, int columns) Constructs a new TextField initialized with the specified text and columns.

JTextField Methods of note void addactionlistener(actionlistener l) Adds the specified action listener to receive action events from this textfield. Each character typed in will call actionperformed void settext(string t) Will put the text from t into the textfield String gettext() Returns the text in the text field at the time of the call

End of GUI That's the end of the GUI components for now. enough to do a fairly interesting project you can use these and learn others from tutorials and api docs as needed. Questions? Read chapter 11