Introduction to Graphical Interface Programming in Java

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

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 program.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
JFC and the Swing Package Feb 10, Java Foundation Class (JFC) zAWT (Abstract Window Toolkit) has been used since jdk1.0 Archaic and very poorly.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
GUI Programming in Java
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
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.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Session 27 Swing vs. AWT. AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
SWING IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
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.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
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.
Java Swing One of the most important features of Java is its ability to draw graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
A Quick Java Swing Tutorial
Welcome To java
Provision for GUIs in Java
A First Look at GUI Applications
Java Swing.
Provision for GUIs in Java
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
Abstract Window ToolKit (AWT)
תכנות מכוון עצמים בשפת JAVA
Steps to Creating a GUI Interface
Week 8 Swing NetBeans GUI Builder
A Quick Java Swing Tutorial
Advanced Programming in Java
Presentation transcript:

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

GUI versus Graphics Programming Graphical User Interface (GUI) Purpose is to display info and accept user input Built using components "Forms" applications Graphics Programming Manipulate graphical elements on a display points, lines, curves, shapes texture and lighting animation, 3D effect

Simple GUI Application A graphical interface is built using components. Label (read-only) TextField (input) Button

Designing a Simple GUI 1. create a window (a container) 2. create components label = new Label("Username:"); input = new TextField( 12 ); // the width button = new Button("Login"); 3. layout components in a container add( label ); add( input ); add( button ); 4. display the container (frame is the container here): frame.setVisible( true ); 5. wait for user to do something

Software for Graphics Java provides frameworks for building graphical UIs. AWT - Abstract Windowing Toolkit the first Java graphics framework Swing - OS-independent framework. JavaFX - layout is specified in XML (fxml), like Android use SceneBuilder tool can also create layout in code (like Swing) SWT - Standard Widget Toolkit (used in Eclipse, Firefox) Designed for efficiency. An Eclipse project.

Graphics Toolkits Java provides complete "frameworks" for building graphical applications. A framework contains all the components and logic needed to manage a graphical interface. You provide: select components and set their properties write application logic that controls how application responds to user actions you may extend (subclass) existing components to create custom components

AWT Graphics Toolkit - java.awt Java's first GUI toolkit uses graphical UI components of operating system, e.g. Windows, MacOS, X-windows. efficient, low overhead applications look different on each platform difficult to write and test good quality applications different bugs on each OS, hence the slogan . . . "Write once, debug everywhere"

AWT Example import java.awt.*; public class AwtDemo { Frame frame; public AwtDemo( ) { frame = new Frame("Please Login"); // create components Label label = new Label("Username:"); TextField input = new TextField(12); Button button = new Button("Login"); // layout components in the container frame.setLayout(new FlowLayout()); frame.add(label); frame.add(input); frame.add(button); frame.pack(); }

AWT Example (continued) To display the window, call setVisible( true ) public static void main(String [] args) { AwtDemo demo = new AwtDemo( ); demo.frame.setVisible( true ); }

Swing Graphics Framework Improved, OS-independent GUI framework classes in javax.swing and sub-packages Swing implements all components in software applications look & behave the same on all platforms applications may not look like native GUI apps (but you can change that by applying a LookAndFeel) part of the Java Foundation Classes (JFC). JFC also includes a 2D API and drag-and-drop API

Swing Containers JFrame is the top level container for most applications has a title bar, menu bar, and content pane JFrame is a heavy weight component.

Swing Example import javax.swing.*; public class SwingDemo { JFrame frame; public SwingDemo( ) { frame = new JFrame("Please Login"); // create components JTextField label = new JLabel("Username:"); JTextField input = new JTextField(12); JButton button = new JButton("Login"); // layout components in the container ContentPane pane = frame.getContentPane(); pane.setLayout(new FlowLayout()); pane.add(label); pane.add(input); pane.add(button); pane.pack(); }

Swing Example (continued) To display the window use setVisible( true ) public static void main(String [] args) { SwingDemo demo = new SwingDemo( ); demo.frame.setVisible( true ); } This is Bad Programming! We should not directly access the private attributes of an object. We should invoke public behavior instead.

Demo Create the simple "Login" interface.

What is Running My Program? In the main method, setVisible(true) returns immediately. main() exits. But, the application is still running! What is in control? Where is the control of flow? public static void main(String [] args) { SwingDemo demo = new SwingDemo( ); demo.frame.setVisible( true ); System.out.println("UI started. Bye."); }

Swing creates its own Thread Swing runs in a separate thread. Or more than one thread. One thread is special: the Event Dispatcher Thread The Event Dispatcher Thread receives and "dispatches" all events involving the UI. You should perform all UI updates in this thread.

Top Level Containers A window that be displayed on screen: JFrame - title bar, menu, and content pane JWindow - no title bar or menu. JDialog - a dialog window, has title bar JApplet - for Applets (run in web browser). Deprecated now.

What's a Component? Also called "widgets". Label Textbox Button Slider Checkbox ComboBox RadioButton ProgressBar xxx

Visual Guide to Components Know your components You need to know the available components ... and what they can do. Visual Guide to Components Sun's Java Tutorial …/tutorial/ui/features/components.html for Windows: tutorial/ui/features/compWin.html

What Can a Component Do? Common Behavior setIcon( imageIcon ) setLocation( x, y ) setIcon( imageIcon ) setBackground( color ) setForeground( color ) setEnabled( boolean ) setVisible( boolean ) setToolTipText( string )

Important Components to Know Component and JComponent provide most of the behavior that all components have! Object Component AWT Container JComponent Window Panel Frame Applet JPanel JLabel JList Swing JTextBox JSlider JButton JFrame JApplet JComboBox JMenuItem

Hierarchy of Graphics Components Liskov Substitution Principle: Any place where the program is expecting a Component you can use a JButton, JLabel, etc. and the program will work correctly. Component paint( graphics ) resize( ... ) JComponent JButton JLabel MyComponent ... JTextbox

Playing with a JButton import javax.swing.*; import java.awt.*; import javax.swing.*; JButton button = new JButton( "Press Me" ); //TODO: add button to a frame and pack button.setBackground( Color.YELLOW ); button.setForeground( Color.BLUE ); button.setToolTipText( "Make my day." ); button.setFont( new Font( "Arial", Font.BOLD, 24 ) ); button.setEnabled( true );

Components don't have to be boring filename, URL, or InputStream. Can by GIF, JPEG, PNG ImageIcon icon = new ImageIcon("d:/images/fish.png"); button.setIcon( icon );

Containers and Components A GUI has many components in containers. Use add to put component in a container. A container is also a component; so a container may contain other containers. add(component) container component component add(component) add(...) container container component component component component

Lightweight Containers A lightweight container is one that is not a window. You must place it inside another container. Cannot be drawn on screen by itself. JPanel simple rectangular area - most common JTabbedPane - multiple panels with a tab on top JSplitPane JInternalFrame - like a JFrame inside a JFrame

Learning Java Graphics Java Tutorial: Creating a GUI with JFC/Swing The section "Using Swing Components" contains "How To..." examples for many components. Good explanation and examples of Layout Managers. "JDK Demos and Samples" jdk_dir/demo/jfc/* Great demos with jar files (run) and source code. http://www.oracle.com/technetwork/java/javase/dow nloads/index.html Big Java, Chapter 19.

WindowBuilder for Eclipse A graphical editor for creating UI with Swing or SWT. Windowbuilder is included in Eclipse, but you may need to download & install extra components. To find the Window Builder "Update Site" for your version of Eclipse, see: http://www.eclipse.org/windowbuilder/download.php