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

Similar documents
INTRODUCTION TO (GUIS)

2110: GUIS: Graphical User Interfaces

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphic User Interfaces. - GUI concepts - Swing - AWT

Top-Level Containers

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

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

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

Dr. Hikmat A. M. AbdelJaber

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

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!

CS 251 Intermediate Programming GUIs: Components and Layout

Java. GUI building with the AWT

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

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

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

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

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

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

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

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

Lecture 5: Java Graphics

Chapter 6: Graphical User Interfaces

Introduction. Introduction

DM503 Programming B. Peter Schneider-Kamp.

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

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

Java: Graphical User Interfaces (GUI)

Packages: Putting Classes Together

H212 Introduction to Software Systems Honors

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

Graphical User Interfaces (GUIs)

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

CHAPTER 2. Java Overview

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

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

Swing. By Iqtidar Ali

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

Window Interfaces Using Swing Objects

FirstSwingFrame.java Page 1 of 1

COURSE 7 PROGRAMMING III OOP. JAVA LANGUAGE

Part I: Learn Common Graphics Components

Systems Programming Graphical User Interfaces

Graphical User Interfaces. Comp 152

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

Java Programming Lecture 6

CS11 Java. Fall Lecture 4

Window Interfaces Using Swing Objects

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

Lecture 3: Java Graphics & Events

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

User interfaces and Swing

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

MIT AITI Swing Event Model Lecture 17

Graphical interfaces & event-driven programming

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

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

Containers and Components

Building Java Programs Bonus Slides

1.00 Lecture 14. Lecture Preview

BASICS OF GRAPHICAL APPS

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

CS 2113 Software Engineering

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

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Class 14: Introduction to the Swing Toolkit

Basicsof. JavaGUI and SWING

To gain experience using GUI components and listeners.

Graphical User Interface (GUI)

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

AP CS Unit 11: Graphics and Events

Lecture 18 Java Graphics and GUIs

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

Agenda. Container and Component

Unit 6: Graphical User Interface

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

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

(Incomplete) History of GUIs

Computer Science 210: Data Structures. Intro to Java Graphics

Graphical User Interfaces 2

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

Window Interfaces Using Swing. Chapter 12

CS 1316 Exam 3 Fall 2009


Using Several Components

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

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

Sri Vidya College of Engineering & Technology

CSE 331 Software Design & Implementation

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

Lecture 9. Lecture

Laying Out Components. What is Widget Layout?

Chapter 12 GUI Basics

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

Transcription:

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

Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2

Interactive Programs Classic view of computer programs: transform inputs to outputs, stop input output Event-driven programs: interactive, long-running Servers interact with clients Applications interact with user(s) input events user output events program user 3

GUI Motivation Interacting with a program Program-Driven = Proactive Statements execute in sequential, predetermined order Typically use keyboard or file I/O, but program determines when that happens Usually single-threaded Event-Driven = Reactive Program waits for user input to activate certain statements Typically uses a GUI (Graphical User Interface) Often multi-threaded Design...Which to pick? Program called by another program? Program used at command line? Program interacts often with user? Program used in window environment? How does Java do GUIs? 4

Java Support for Building GUIs Java Foundation Classes Classes for building GUIs Major components awt and swing Pluggable look-and-feel support Accessibility API Java 2D API Drag-and-drop Support Internationalization Our main focus: Swing Building blocks of GUIs Windows & components User interactions Built upon the AWT (Abstract Window Toolkit) Java event model 5

Java Foundation Classes Pluggable Look-and-Feel Support Controls look-and-feel for particular windowing environment E.g., Java, Windows, Mac Accessibility API Supports assistive technologies such as screen readers and Braille Java 2D Drawing Includes rectangles, lines, circles, images,... Drag-and-drop Support for drag and drop between Java application and a native application Internationalization Support for other languages 6

GUI Statics and GUI Dynamics Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain other components frames, panels, dialog boxes,... Layout managers: control placement and sizing of components Dynamics: user interactions Events button-press, mouse-click, keypress,... Listeners: an object that responds to an event Helper classes Graphics, Color, Font, FontMetrics, Dimension,... 7

Creating a Window import javax.swing.*; public class Basic1 { public static void main(string[] args) { //create the window JFrame f = new JFrame("Basic Test!"); //quit Java after closing the window f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(200, 200); //set size in pixels f.setvisible(true); //show the window 8

Creating a Window import javax.swing.*; public class Basic1 { public static void main(string[] args) { //create the window JFrame f = new JFrame("Basic Test!"); //quit Java after closing the window f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(200, 200); //set size in pixels f.setvisible(true); //show the window 8

Creating a Window Using a Constructor import javax.swing.*; public class Basic2 extends JFrame { public static void main(string[] args) { new Basic2(); public Basic2() { settitle("basic Test2!"); //set the title //quit Java after closing the window setdefaultcloseoperation(exit_on_close); setsize(200, 200); //set size in pixels setvisible(true); //show the window 9

import javax.swing.*; import java.awt.*; import java.awt.event.*; A More Extensive Example public class Intro extends JFrame { private int count = 0; private JButton mybutton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setdefaultcloseoperation(exit_on_close); setlayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(mybutton); //add components add(label); mybutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { count++; label.settext("count: " + count); ); pack(); setvisible(true); public static void main(string[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception exc) { new Intro(); 10

import javax.swing.*; import java.awt.*; import java.awt.event.*; A More Extensive Example public class Intro extends JFrame { private int count = 0; private JButton mybutton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setdefaultcloseoperation(exit_on_close); setlayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(mybutton); //add components add(label); mybutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { count++; label.settext("count: " + count); ); pack(); setvisible(true); public static void main(string[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception exc) { new Intro(); 10

import javax.swing.*; import java.awt.*; import java.awt.event.*; A More Extensive Example public class Intro extends JFrame { private int count = 0; private JButton mybutton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setdefaultcloseoperation(exit_on_close); setlayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(mybutton); //add components add(label); mybutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { count++; label.settext("count: " + count); ); pack(); setvisible(true); public static void main(string[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception exc) { new Intro(); 10

GUI Statics Determine which components you want Choose a top-level container in which to put the components (JFrame is often a good choice) Choose a layout manager to determine how components are arranged Place the components 11

Components = What You See Visual part of an interface Represents something with position and size Can be painted on screen and can receive events Buttons, labels, lists, sliders, menus,... 12

Component Examples import javax.swing.*; import java.awt.*; public class ComponentExamples extends JFrame { public ComponentExamples() { setlayout(new FlowLayout(FlowLayout.LEFT)); add(new JButton("Button")); add(new JLabel("Label")); add(new JComboBox(new String[] { "A", "B", "C" )); add(new JCheckBox("JCheckBox")); add(new JSlider(0, 100)); add(new JColorChooser()); setdefaultcloseoperation(exit_on_close); pack(); setvisible(true); public static void main(string[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception exc) { new ComponentExamples(); 13

Component Examples import javax.swing.*; import java.awt.*; public class ComponentExamples extends JFrame { public ComponentExamples() { setlayout(new FlowLayout(FlowLayout.LEFT)); add(new JButton("Button")); add(new JLabel("Label")); add(new JComboBox(new String[] { "A", "B", "C" )); add(new JCheckBox("JCheckBox")); add(new JSlider(0, 100)); add(new JColorChooser()); setdefaultcloseoperation(exit_on_close); pack(); setvisible(true); public static void main(string[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); catch (Exception exc) { new ComponentExamples(); 13

More Components JFileChooser: allows choosing a file JLabel: a simple text label JTextArea: editable text JTextField: editable text (one line) JScrollBar: a scrollbar JPopupMenu: a pop-up menu JProgressBar: a progress bar Lots more! 14

Containers A container is a component that Can hold other components Has a layout manager Heavyweight vs. lightweight A heavyweight component interacts directly with the host system JWindow, JFrame, and JDialog are heavyweight Except for these top-level containers, Swing components are almost all lightweight JPanel is lightweight There are three basic top-level containers JWindow: top-level window with no border JFrame: top-level window with border and (optional) menu bar JDialog: used for dialog windows Another important container JPanel: used mostly to organize objects within other containers 15

A Component Tree JFrame JPanel JPanel JPanel JPanel JPanel JPanel JPanel JPanel JPanel JComboBox (km) JComboBox (mi) JTextField (3226) JSlider JTextField (2000) JSlider 16

Layout Managers A layout manager controls placement and sizing of components in a container If you do not specify a layout manager, the container will use a default: JPanel default = FlowLayout JFrame default = BorderLayout Five common layout managers: BorderLayout, BoxLayout, FlowLayout, GridBagLayout, GridLayout General syntax container.setlayout(new LayoutMan()); Examples: JPanel p1 = new JPanel(new BorderLayout()); JPanel p2 = new JPanel(); p2.setlayout(new BorderLayout()); 17

Some Example Layout Managers FlowLayout Components placed from left to right in order added When a row is filled, a new row is started Lines can be centered, left-justified or right-justified (see FlowLayout constructor) See also BoxLayout GridLayout Components are placed in grid pattern number of rows & columns specified in constructor Grid is filled left-to-right, then topto-bottom BorderLayout Divides window into five areas: North, South, East, West, Center Adding components FlowLayout and GridLayout use container.add(component) BorderLayout uses container.add(component, index) where index is one of BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST BorderLayout.CENTER 18

FlowLayout Example import javax.swing.*; import java.awt.*; public class Statics1 { public static void main(string[] args) { new S1GUI(); class S1GUI { private JFrame f; public S1GUI() { f = new JFrame("Statics1"); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(500, 200); f.setlayout(new FlowLayout(FlowLayout.LEFT)); for (int b = 1; b < 9; b++) f.add(new JButton("Button " + b)); f.setvisible(true); 19

FlowLayout Example import javax.swing.*; import java.awt.*; public class Statics1 { public static void main(string[] args) { new S1GUI(); class S1GUI { private JFrame f; public S1GUI() { f = new JFrame("Statics1"); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(500, 200); f.setlayout(new FlowLayout(FlowLayout.LEFT)); for (int b = 1; b < 9; b++) f.add(new JButton("Button " + b)); f.setvisible(true); 19

BorderLayout Example import javax.swing.*; import java.awt.*; public class Statics2 { public static void main(string[] args) { new S2GUI(); class ColoredJPanel extends JPanel { Color color; ColoredJPanel(Color color) { this.color = color; public void paintcomponent(graphics g) { g.setcolor(color); g.fillrect(0, 0, 400, 400); class S2GUI extends JFrame { public S2GUI() { settitle("statics2"); setdefaultcloseoperation(jframe.exit_on_close); setsize(400, 400); add(new ColoredJPanel(Color.RED), BorderLayout.NORTH); add(new ColoredJPanel(Color.GREEN), BorderLayout.SOUTH); add(new ColoredJPanel(Color.BLUE), BorderLayout.WEST); add(new ColoredJPanel(Color.YELLOW), BorderLayout.EAST); add(new ColoredJPanel(Color.BLACK), BorderLayout.CENTER); setvisible(true); 20

BorderLayout Example import javax.swing.*; import java.awt.*; public class Statics2 { public static void main(string[] args) { new S2GUI(); class ColoredJPanel extends JPanel { Color color; ColoredJPanel(Color color) { this.color = color; public void paintcomponent(graphics g) { g.setcolor(color); g.fillrect(0, 0, 400, 400); class S2GUI extends JFrame { public S2GUI() { settitle("statics2"); setdefaultcloseoperation(jframe.exit_on_close); setsize(400, 400); add(new ColoredJPanel(Color.RED), BorderLayout.NORTH); add(new ColoredJPanel(Color.GREEN), BorderLayout.SOUTH); add(new ColoredJPanel(Color.BLUE), BorderLayout.WEST); add(new ColoredJPanel(Color.YELLOW), BorderLayout.EAST); add(new ColoredJPanel(Color.BLACK), BorderLayout.CENTER); setvisible(true); 20

GridLayout Example import javax.swing.*; import java.awt.*; public class Statics3 { public static void main(string[] args) { new S3GUI(); class S3GUI extends JFrame { static final int DIM = 25; static final int SIZE = 12; static final int GAP = 1; public S3GUI() { settitle("statics3"); setdefaultcloseoperation(exit_on_close); setlayout(new GridLayout(DIM, DIM, GAP, GAP)); for (int i = 0; i < DIM * DIM; i++) add(new MyPanel()); pack(); setvisible(true); class MyPanel extends JPanel { MyPanel() { setpreferredsize(new Dimension(SIZE, SIZE)); public void paintcomponent(graphics g) { float gradient = 1f - ((float)math.abs(getx() - gety()))/(float)((size + GAP) * DIM); g.setcolor(new Color(0f, 0f, gradient)); g.fillrect(0, 0, getwidth(), getheight()); 21

GridLayout Example import javax.swing.*; import java.awt.*; public class Statics3 { public static void main(string[] args) { new S3GUI(); class S3GUI extends JFrame { static final int DIM = 25; static final int SIZE = 12; static final int GAP = 1; public S3GUI() { settitle("statics3"); setdefaultcloseoperation(exit_on_close); setlayout(new GridLayout(DIM, DIM, GAP, GAP)); for (int i = 0; i < DIM * DIM; i++) add(new MyPanel()); pack(); setvisible(true); class MyPanel extends JPanel { MyPanel() { setpreferredsize(new Dimension(SIZE, SIZE)); public void paintcomponent(graphics g) { float gradient = 1f - ((float)math.abs(getx() - gety()))/(float)((size + GAP) * DIM); g.setcolor(new Color(0f, 0f, gradient)); g.fillrect(0, 0, getwidth(), getheight()); 21

More Layout Managers CardLayout Tabbed index card look from Windows GridBagLayout Most versatile, but complicated Custom Can define your own layout manager But best to try Java's layout managers first... Null No layout manager Programmer must specify absolute locations Provides great control, but can be dangerous because of platform dependency 22

AWT and Swing AWT Initial GUI toolkit for Java Provided a Java look and feel Basic API: java.awt.* Swing More recent (since Java 1.2) GUI toolkit Added functionality (new components) Supports look and feel for various platforms (Windows, Mac) Basic API: javax.swing.* Did Swing replaced AWT? Not quite: both use the AWT event model 23

Code Examples Intro.java Button & counter Basic1.java Create a window Basic2.java Create a window using a constructor Calculator.java Shows use of JOptionPane to produce standard dialogs ComponentExamples.java Sample components Statics1.java FlowLayout example Statics2.java BorderLayout example Statics3.java GridLayout example LayoutDemo.java Multiple layouts 24