Steps to Creating a GUI Interface

Slides:



Advertisements
Similar presentations
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.
Advertisements

Graphical User Interfaces
Graphic User Interfaces Layout Managers Event Handling.
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.
Object-Oriented Software Engineering Java with added Swing.
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.
Graphical User Interfaces
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 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.
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.
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.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
User Interface Components. Layout Placement of UI components in a window – Size & Position Each component occupies a rectangular region in the window.
Developing GUIs With the Eclipse Visual Editor, Swing/AWT Edition David Gallardo.
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
GUI 1: JFC and Swing Basics OOP tirgul No
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Java Swing.
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Introduction to Graphical Interface Programming in Java
Containers and Components
תכנות מכוון עצמים בשפת JAVA
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

Steps to Creating a GUI Interface The secrets of GUI interface revealed.

Steps to creating a GUI Interface 1. Design it on paper 2. Choose components and containers 3. Create a window or dialog. 4. Add components to the window. 5. Preview the UI. 6. Add behavior - respond to user actions.

Step 1: Design it on paper Know what the interface is supposed to do Decide what information to present to user and what input he should supply. Decide the components and layout on paper Login Name: Login

Step 2: Choose Components & Layout Choose the components and layout on paper JTextField JButton JLabel Username: Login FlowLayout

Step 3: Create a Window (JFrame) import javax.swing.*; public class SwingExample implements Runnable { JFrame frame; public SwingExample( ) { frame = new JFrame(); frame.setTitle("Please Login"); initComponents( ); } private void initComponents( ) { // initialize components here frame.pack(); public void run() { frame.setVisible( true );

Step 3: (alt) Be a JFrame import javax.swing.*; public class SwingExample extends JFrame { public SwingExample( ) { super.setTitle("Please Login"); initComponents( ); } private void initComponents( ) { // initialize components here this.pack(); public void run() { this.setVisible( true );

Step 3.1: Decorate the Frame We can add decoration to the JFrame or components. Add a title: frame.setTitle( "Please Login" );

Step 3.2: Close Application on Exit? Even if you close the window, the GUI thread still running! GUI applications can run forever! Your program must tell the GUI to exit. How to make it quit when you close the window? handle a WindowClosingEvent, or specify Exit-on-Close behavior frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Step 4: Add Components to window Add components to the content pane of a JFrame. As a convenience, you can add directly to JFrame. private void initComponents( ) { JLabel label1 = new JLabel("Username:"); input = new JTextField( 12 ); button = new JButton("Login"); frame.add( label1 ); frame.add( input ); frame.add( button ); // pack components. This sets the window size. frame.pack( ); }

Step 4.1: Choose a Layout Manager Each container uses a Layout Manager to manage the position and size of components. Classic Layout Managers are: BorderLayout (default for JFrame) FlowLayout (default for JPanel) BoxLayout GridLayout GridBagLayout (the most powerful) CardLayout

4.2: Set the Layout Manager Set the container's layout manager private void initComponents( ) { JLabel label1 = new JLabel("Username:"); input = new JTextField( 12 ); button = new JButton("Login"); frame.setLayout( new FlowLayout() ); frame.add( label1 ); frame.add( input ); frame.add( button ); frame.pack( ); }

Adding Components to a Panel Most graphical UI use many "panels" or "panes" to group components. This makes layout and management easier. void initComponents( ) { JLabel label1 = new JLabel("Username:") input = new JTextField( 12 ); button = new JButton("Login"); JPanel panel = new JPanel(); panel.add( label1 ); panel.add( input ); panel.add( button ); frame.getContentPane( ).add( panel ); Put components in a panel Add panel to the frame

Step 5: Preview the Interface To show the window, call setVisible(true) public class SwingExample { .... // create a run() method to display the window public void run() { frame.setVisible( true ); } public class Main { public static void main( String [] args ) { SwingExample gui = new SwingExample( ); gui.run( ); }

Problem: Window is too small If your application shows only a title bar, it means you forgot to set the window size. You must either: pack() or setSize(width, height) Usually you should use pack( ) public class SwingExample { JFrame frame; ... public void run() { frame.pack( ); // set size = best size frame.setVisible(true); }

Step 6: Add Behavior Your application must do something when user presses a button, moves the mouse, etc. Graphics programs are event driven. Events: button press got focus mouse movement text changed slider moved

Why a layout manager? Demo: compare a Java application and Visual C# application when resizing a window. In Java, the layout manager will rearrange or resize components. In Visual C#, the components disappear.

Layout Managers Classic Layout Managers are: BorderLayout (default for JFrame) FlowLayout (default for JPanel) BoxLayout GridLayout GridBagLayout CardLayout SpringLayout

Graphics Class Hierarchy (again) Components inherit most of their behavior (methods) from Component and JComponent. Object Component Container AWT JComponent Window Panel JPanel Frame Applet JFrame JApplet

Exercise: JComponent Look at JavaDoc for JComponent. What properties can you "set" for any component?

Important Containers to Know JPanel, JFrame, and JWindow are used a lot in Swing apps. Panel, Frame, and Window are used a lot in AWT apps. Object Component Container AWT JComponent Window Panel Swing JPanel Frame Applet JFrame JApplet

How to Design a GUI Application Separate the GUI classes from the program logic. Program logic is part of the "model" or domain layer. GUI calls model for information. Try to limit GUI -> Model communication to just one class This reduces coupling between GUI and logic classes. Model does not call methods of GUI objects. Use the Observer Pattern. Model (observable) notifies GUI (observer) when its state changes.

Layers in a GUI Application Graphical Interface (Swing) calls Controller (handle requests from UI) calls Application (application logic)