LayoutManager, J 1 Layout Manager. LayoutManager, J 2 Layout Manager To each UI container there is a layout manager (an object). When you add a component.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

GUI Elements Session 17. Memory Upload Layout Components Button TextField TextArea Label Choice Containers Panels The applet itself.
GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.
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.
Advanced Swing. Advanced Layout Managers GridBagLayout – Example: Web Browser (Grid Bag Layout)Web Browser (Grid Bag Layout) BoxLayout – Example: Web.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Abstract Windowing Toolkit Design Goal: –allow the programmer to build o GUI that looks good on all platforms –provide a well-designed object-oriented.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 L48 Advanced GUI Component (3). 2 OBJECTIVES  To use additional layout managers.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
1 Chapter 22 Containers, Layout Managers, and Borders.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java Software Solutions Lewis and Loftus Chapter 10 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Graphical User Interfaces --
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
© Marty Hall, Larry Brown, Web core programming 1 Layout Managers Arranging Elements in Windows.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Copyright Curt Hill GridBag Layout Manager A flexible but complicated layout.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Csc Basic Graphical User Interface (GUI) Components.
Grid Bag Layout Most Complex and Powerful Components can vary in size and can be added in any order Draw out on piece of paper first Columns and Rows:
Computer Science 209 GUIs Model/View/Controller Layouts.
CPSC 233 Tutorial Xin Apr 6, Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
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 Swing - Lecture 3 Layout Management
Graphical User Interface (GUI)
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
Modern Programming Language Java
University of Central Florida COP 3330 Object Oriented Programming
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Panels & Layout Managers
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
Layout Organization and Management
Graphical User Interface
Presentation transcript:

LayoutManager, J 1 Layout Manager

LayoutManager, J 2 Layout Manager To each UI container there is a layout manager (an object). When you add a component to a container the layout manager automatically decides its place and size. There are different types of layout managers, each have their own rules when it comes to arranging components inside the container. Examples: –Flow –Border –Grid –Card –Grid Bag.

LayoutManager, J 3 BorderLayout public class buttonDir extends Applet { public void init() { setLayout(new BorderLayout()); add("North", new Button("North")); add("South", new Button("South")); add("East", new Button("East")); add("West", new Button("West")); add("Center", new Button("Center")); }

LayoutManager, J 4 The UI of a calculator The following GUI- components are used: –Applet –TextField –Panel –Button

LayoutManager, J 5 GUI code for Calculator import java.awt.*; import java.awt.event.*; import java.applet.*; public class Calculator extends Applet implements ActionListener { private TextField theDisplay; public void init () { //Initialize the applet // // Separate applet into "North", "South", "East", "West", // and "Center", that is "use BorderLayout". setLayout(new BorderLayout()); theDisplay = new TextField(40); add("South",theDisplay); addButtons(); }

LayoutManager, J 6 Adding the buttons private void addButtons () { // Place calculator buttons in a grid at the center of the main frame. Panel topPanel = new Panel(); topPanel.setLayout(new GridLayout(4,4)); Button button; button = new Button("1"); button.addActionListener(this); topPanel.add(button); button = new Button("2"); button.addActionListener(this); topPanel.add(button); // Place calculator buttons in the center of the frame add("Center", topPanel); // Place "do calculation"-button at the right button = new Button("="); button.addActionListener(this); add("East", button); // Place clear-button at the left button = new Button("C"); button.addActionListener(this); add("West", button); }

LayoutManager, J 7 Event-handling code public void actionPerformed (ActionEvent e){ String arg = e.getActionCommand(); handelButtons(arg); } private void handelButtons (String buttonItemText){ if (buttonItemText.equals("C")){ theDisplay.setText(""); } else { String theText = theDisplay.getText(); String newText; int posStart = theDisplay.getSelectionStart(); int posEnd = theDisplay.getSelectionEnd(); if (posStart > 0){ newText = theText.substring(0, posStart) + buttonItemText; } else { newText = buttonItemText; } if (posStart < theText.length()) newText = newText + theText.substring(posEnd, theText.length()); theDisplay.setText(newText); theDisplay.select(posStart+1, posStart+1); } } }

LayoutManager, J 8 Another GUI Example Start Stop Number of points:0

LayoutManager, J 9 How it looks in Windows

LayoutManager, J 10 Making the User Interface - Using GridBagLayout (solutions without GridBagLayout is also possible) public void addUserComponents () { setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Label lblPointsText = new Label("Number of points: ", Label.RIGHT); add(lblPointsText, constraints); constraints.gridx = 1; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Label lblPoints = new Label("0 "); add(lblPoints, constraints); public void addUserComponents () { setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Label lblPointsText = new Label("Number of points: ", Label.RIGHT); add(lblPointsText, constraints); constraints.gridx = 1; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Label lblPoints = new Label("0 "); add(lblPoints, constraints); constraints.gridx = 0; constraints.gridy = 1; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.weightx = 1; constraints.weighty = 1; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); battlefield = new Battlefield(); add(battlefield, constraints); constraints.gridx = 0; constraints.gridy = 2; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Button quitButton = new Button("Start"); add(quitButton, constraints); constraints.gridx = 1; constraints.gridy = 2; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Button startButton = new Button("Stop"); add(startButton, constraints); setBackground(Color.gray); } constraints.gridx = 0; constraints.gridy = 1; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.weightx = 1; constraints.weighty = 1; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); battlefield = new Battlefield(); add(battlefield, constraints); constraints.gridx = 0; constraints.gridy = 2; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Button quitButton = new Button("Start"); add(quitButton, constraints); constraints.gridx = 1; constraints.gridy = 2; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0.5; constraints.weighty = 0; constraints.fill = GridBagConstraints.HORIZONTAL; Button startButton = new Button("Stop"); add(startButton, constraints); setBackground(Color.gray); }

LayoutManager, J 11 Class GridBagConstraints public Insets insets This field specifies the external padding of the component, the minimum amount of space between the component and the edges of its display area. The default value is new Insets(0, 0, 0, 0). public int gridwidth Specifies the number of cells in a row for the component's display area. public int gridheight Specifies the number of cells in a row for the component's display area.

LayoutManager, J 12 Class GridBagConstraints public double weightx Specifies how to distribute extra horizontal space. The grid bag layout manager calculates the weight of a column to be the maximum weighty of all the components in a row. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight. A column that has a weight zero receives no extra space. If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges. The default value of this field is 0. weightx should be a non-negative value. public double weighty Specifies how to distribute extra vertical space. The grid bag layout manager calculates the weight of a row to be the maximum weightx of all the components in a row. If the resulting layout is smaller vertically than the area it needs to fill, the extra space is distributed to each row in proportion to its weight. A row that has a weight of zero receives no extra space. If all the weights are zero, all the extra space appears between the grids of the cell and the top and bottom edges. The default value of this field is 0. weighty should be a non-negative value.

LayoutManager, J 13 Class GridBagConstraints public int fill This field is used when the component's display area is larger than the component's requested size. It determines whether to resize the component, and if so, how. The following values are valid for fill: NONE: Do not resize the component. HORIZONTAL: Make the component wide enough to fill its display area horizontally, but do not change its height. VERTICAL: Make the component tall enough to fill its display area vertically, but do not change its width. BOTH: Make the component fill its display area entirely. The default value is NONE.

LayoutManager, J 14 User Interface - showing the constraints used by the GridBagLayout gridx = 0, gridy = 1, gridwidth = 2, gridheight = 1, weightx = 1, weighty = 1, fill = GridBagConstraints.BOTH, insets = new Insets(5, 5, 5, 5) Start gridx = 0, gridy = 2, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0, fill = GridBagConstraints.HORIZONTAL Start gridx = 0, gridy = 2, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0, fill = GridBagConstraints.HORIZONTAL Stop gridx = 1, gridy = 2, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0, fill = GridBagConstraints.HORIZONTAL Stop gridx = 1, gridy = 2, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0, fill = GridBagConstraints.HORIZONTAL Number of points: gridx = 0, gridy = 0, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0, fill = GridBagConstraints.HORIZONTAL 0 gridx = 1, gridy = 0, gridwidth = 1, gridheight = 1, weightx = 0.5, weighty = 0; fill = GridBagConstraints.HORIZONTAL lblPointsText lblPoints battlefield quitButton startButton

LayoutManager, J 15 CardLayout CardLayout places components (usually panels) on top of each other in a stack like a deck of cards. You see only one at a time, and you can flip through the panels by using another control to select which panel comes to the top.