Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

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.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Corresponds with Chapter 12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
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.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
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.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H.
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.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
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 GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 7 A First Look at GUI Applications Layout Managers.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Java GUI.
Tim McKenna Layout Mangers in Java Tim McKenna
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
GUI building with the AWT
Chapter 7-3 (Book Chapter 14)
GUI Layouts By: Leonard & Saif.
GUI building with the AWT
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
Presentation transcript:

Java layout managers

import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() { yellowButton = new JButton("Yellow"); blueButton = new JButton("Blue"); redButton = new JButton("Red"); readButton = new JButton("read this"); text = new JTextArea(1,20); label = new JLabel(); add(readButton); add(text); add(label); yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); readButton.addActionListener(this); }

public void actionPerformed(ActionEvent event) { Object source = event.getSource(); Color color = getBackground(); if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red; else if (source == readButton) { System.out.println("read it"); System.out.println( text.getText() ); label.setText( text.getText() ); } setBackground(color); repaint(); } private JButton yellowButton; private JButton blueButton; private JButton redButton; private JTextArea text; private JButton readButton; private JLabel label; }

Why do we need Layout Managers? zWindow expanded but Components Stay put zcomponents designed for a specific look- and-feel or font size

What are Layout Managers? zA layout manager is a class that encapsulates an algorithm for positioning and sizing GUI components.

What are Layout Managers? zRather than building the layout algorithms into your code and watch for window resizing, the algorithm is kept separate. zThis allows that algorithm to be used for several applications, while simplifying the code.

FlowLayout z The components line up horizontally until there is no more room and then starts a new row of components.

BorderLayout zContainer contentPane = getContentPane(); zcontentPane.setLayout(new BorderLayout()); z zcontentPane.add(new JButton("Button 1 (NORTH)"), z BorderLayout.NORTH); zcontentPane.add(new JButton("2 (CENTER)"), z BorderLayout.CENTER); zcontentPane.add(new JButton("Button 3 (WEST)"), z BorderLayout.WEST); zcontentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), z BorderLayout.SOUTH); zcontentPane.add(new JButton("Button 5 (EAST)"), z BorderLayout.EAST);

BoxLayout  The BoxLayout class puts components in a single row or column. zIt respects the components' requested maximum sizes, and also lets you align components.

GridLayout z Lays out components equal in size and displays them in the requested number of rows and columns

How to Choose a Layout Manager  Scenario: You need to display a component in as much space as it can get. -- use BorderLayout  Scenario: You need to display a few components in a compact row at their natural size. -- Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager FlowLayoutBoxLayout  Scenario: You need to display a few components of the same size in rows and columns -- GridLayout is perfect for this. GridLayout  Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes. -- BoxLayout BoxLayout

Homework zPlace 8 buttons on a container (e.g. JPanel) using FlowLayout, BorderLayout, BoxLayout (either horizontal or vertical) and GridLayout zYou can just re-compile the same Java program 4 times. Each time change the Layout Manager.