Introduction to Graphical User Interfaces (GUIs)

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Java's Graphical User Interface Toolkit
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Basics of GUI Programming Chapter 11 and Chapter 22.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Module 13: Swing API Object Oriented Programming(Java)
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
A Quick Java Swing Tutorial
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Java Swing.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
GUIS: Graphical User Interfaces
GUIS: Graphical User Interfaces
Chapter 7-3 (Book Chapter 14)
Steps to Creating a GUI Interface
Advanced Programming in Java
GUI building with the AWT
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
Presentation transcript:

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

Announcements A3 is up, due Friday, Oct 9 Prelim 1 scheduled for Thursday October 15 We do NOT have any scheduled makeup exam If you have a conflict, let us know now; you can take the exam a little earlier on the same day

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

GUI Motivation Interacting with a program Design...Which to pick? 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?

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

Swing versus SWT Swing builds on AWT SWT is “new” Strives for total portability Secretly seems to have a grudge against Windows Basic architecture is pretty standard SWT is “new” Goal is best performance Great fit with Windows system Basic architecture is pretty standard Lonnie opted for SWT in A3 but could just as easily have used Swing. He feels that use of SWT simplified his code.

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

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

Creating a Window in SWT import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class HelloWorld { public static void main(String[] args) { //create the window Display display = new Display(); Shell shell = new Shell(display); Label label = new Label(shell, SWT.NONE); label.setText("Basic Test!"); label.pack(); shell.pack(); shell.open(); // quit Java after closing the window while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose ();

Creating a Window in Swing 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 }

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

A More Extensive Example import javax.swing.*; import java.awt.*; import java.awt.event.*; 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();

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

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, ... Some windows have hidden components that become visible only when the user takes some action

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();

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!

Containers A container is a component that Heavyweight vs. lightweight 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

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

Layout Managers General syntax 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());

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 top-to- 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

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

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

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());

More Layout Managers CardLayout GridBagLayout Custom Null 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

AWT and Swing AWT Swing Did Swing replaced 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

Code Examples Intro.java Basic1.java Basic2.java Calculator.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