Layout Managers Arranges and lays out the GUI components on a container.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
TCU CoSc Programming with Java Handling Events.
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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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 GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS Lecture 01 Frames and Components and events Lynda Thomas
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Object-Oriented Software Engineering
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
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.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Session 10. Java Simplified / Session 10 / 2 of 36  An Applet is a Java program that can be executed with the help of a Java enabled browser.  Every.
GUIs & Event-Driven Programming Chapter 11 Review.
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.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Graphical User Interfaces -- GUIs
Swing JComponents.
“Form Ever Follows Function” Louis Henri Sullivan
INFSY 547: WEB-Based Technologies
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
GUI building with the AWT
Border Layout, using Panels, Introduction to PinBallGame
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Layout Managers Arranges and lays out the GUI components on a container

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2 Layout Managers Layout Managers controls the size and position of components in a container. Every container has a default Layout Manager: Panels – FlowLayout Window (e.g. Applets, etc.) – BorderLayout Usage: myContainer.setLayout( new LayoutManger() );

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 3 Layout Managers Basic Layouts FlowLayout BorderLayout GridLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 4 FlowLayout FlowLayout( int align ) FlowLayout() GUI components are placed on a container from left to right in the order in which they are added to the container. Alignments: Left ( FlowLayout.LEFT ) Center ( FlowLayout.CENTER ) Right ( FlowLayout.RIGHT )

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 5 Layout Managers Basic Layouts FlowLayout BorderLayout Panels GridLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 6 BorderLayout GUI components are arranged into 5 regions North – top South – bottom Center – middle East – right West – left Max of 5 Components can be added directly. Usage: c.setLayout(new BorderLayout()); c.add("North", new Button("North"));

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 7 Panels A component that can contain other components. Containers that allow grouping of Components to create complex GUI’s. May have components added to them (including other panels) Default Layout: FlowLayout Each Panel can have a different layout.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 8 Layout Managers Basic Layouts FlowLayout BorderLayout GridLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 9 GridLayout GridLayout(int rows, int cols) Container is divided into a grid where components are placed in rows and columns. Every component has the same width and height.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 10 GridLayout Components are added left to right until row is full, starting from the top-left cell of the grid. The process continues left to right on the next row of the grid. Usage: c.setLayout(new GridLayout(1, 1)); c.add(new Button("Button 1"));

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 11 Exercises Create an applet that looks like a phone’s dialing pad. Use buttons and a Grid Layout. Create an applet that looks like a chat window. Create an applet that looks like Windows’ Standard calculator. Use buttons, labels, text fields and several panels to do this.

Advanced Layouts (optional)

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 13 Layout Managers Advanced Layouts CardLayout GridBagLayout BoxLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 14 CardLayout Components are arranged or stacked like a deck of cards. Only the component at the “top” of the deck is visible. Each card is usually a container (i.e. Panel) Each card can use any layout manager.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 15 Layout Managers Advanced Layouts CardLayout GridBagLayout BoxLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 16 GridBagLayout Similar to GridLayout Arranges components into a grid But more Flexible Each component size can vary. Components can be added in any order.

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 17 Layout Managers Advanced Layouts GridBagLayout CardLayout BoxLayout

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 18 BoxLayout GUI components are arranged left-to-right or top-to-bottom in a container.

Event Handling

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 20 AWT 1.1 Idea: Listeners Idea: each visual object has one or more “listeners” that perform the action for that object Listeners implement an interface and perform some action e.g., ActionListener, MouseListener, etc. You add listeners to each object b.addActionListener( myListener ) When button is pressed, listener’s method is called Efficient – only the added listeners receive the events

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 21 The Applet as the Listener What needs to be done import java.awt.event.*; init() method: create the Button as before goButton = new Button( “Go” ); goButton must be a field of the applet add the applet (“this”) as listener for each button goButton.addActionListener( this ) applet implements ActionListener interface define actionPerformed( ActionEvent e ) method use e.getSource() to get the Button object that was clicked use if statements to determine which button it is if ( e.getSource() == goButton )

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 22 The Applet as the Listener MyApplet (implements ActionListener) b1 = new Button( “1” ); b1.addActionListener( this ); Button b Button actionListeners b1 … inside init() … b2 = new Button( “2” ); b2.addActionListener( this ); Button b Button actionListeners b2 … public void actionPerformed(ActionEvent e) { if ( e.getSource() == b1 ) { … } else if ( e.getSource() == b2 ) { … } create an ActionEvent object for each ActionListener, call actionPerformed(…) when b2 is pressed … ActionEvent source actionPerformed(…)

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 23 Events Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HelloWorldApplet2 extends JApplet implements ActionListener { private JButton button; private JTextField textField; private JLabel label; public void init() { Container c = this.getContentPane(); c.setLayout( new FlowLayout() ); label = new JLabel( "Hello, World!" ); c.add( label ); textField = new JTextField( "Enter your name here", 15 ); c.add( textField ); button = new JButton( "Click Me" ); button.addActionListener( this ); c.add( button ); } public void actionPerformed( ActionEvent ae ) { if ( ae.getSource() == button ) { String string = textField.getText(); label.setText( "Hello, " + string + "!" ); }

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 24 Events Example 2 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class HideMessage extends JApplet implements ActionListener { JButton hideButton; JTextField message; public void init() { Container c = this.getContentPane(); hideButton = new JButton( “Hide” ); c.add( hideButton ); hideButton.addActionListener( this ); message = new JTextField( “Hello” ); c.add( message ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == hideButton ) { message.setVisible( false ); }

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 25 Exercises Hide and Show Applet add another button TextCopy applet use a TextField, a Label, and a Button When you press the button, the text is copied from the TextField to the Label

Mouse Event Listeners

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 27 Mouse listener interfaces MouseListener void mouseClicked( MouseEvent me ) void mouseEntered( MouseEvent me ) void mouseExited( MouseEvent me ) void mousePressed( MouseEvent me ) void mouseReleased( MouseEvent me ) MouseMotionListener void mouseMoved( MouseEvent me ) void mouseDragged( MouseEvent me )

9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 28 Mouse Event Represents the state of the mouse pointer when the event occurs Most useful methods: getX() and getY() returns the coordinates of the mouse