241-211 OOP (Java): Layout/141 241-211. OOP Objectives – –describe the basic layout managers for GUIs Semester 2, 2013-2014 14. GUI Layout.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
Corresponds with Chapter 12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
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.
Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers.
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.
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.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
University of Limerick1 Software Architecture Java Layout Managers.
3461 Laying Out Components Interior Design for GUIs.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
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)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Dept. of CSIE, National University of Tainan 10/21/2012 Arranging Components on a User Interface.
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
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
8 April 2008 GUIS —Graphical User Interfaces
University of Central Florida COP 3330 Object Oriented Programming
Tim McKenna Layout Mangers in Java Tim McKenna
Containers and Components
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Panels & Layout Managers
Chapter 7-3 (Book Chapter 14)
Layout Organization and Management
13 April 2010 GUIS: Graphical User Interfaces
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout

OOP (Java): Layout/142 Contents 1.A Reminder on GUI Creation 2.Flow Layout 3.Grid Layout 4.Border Layout 5.Box Layout 6.Combining Layouts 7.Improving the Appearance 8.Other Layout Managers

OOP (Java): Layout/ The 3-step GUI Again A reminder of the three steps in writing GUIs: – –1. Declare the GUI components; – –2. Implement the event handlers for the components; – –3. Position the components on the screen by using layout managers and/or containers.

OOP (Java): Layout/ Emphasis of this Part The examples in this part will concentrate on layout managers and the JPanel container – –step 3 Layout managers examined: – –FlowLayout, GridLayout, BorderLayout, BoxLayout, combining layouts

OOP (Java): Layout/145 Basic Layouts as Pictures flow grid N C S EW border box

OOP (Java): Layout/ Flow Layout Components are added left-to-right – –they are centered in the container (JFrame) – –a new line is started when necessary Resizing the window may cause components to move to a new line.

OOP (Java): Layout/147 FlowDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FlowDemo extends JFrame { public FlowDemo() { super("E-Commerce Application"); Container c = getContentPane(); c.setLayout( new FlowLayout() ); :

OOP (Java): Layout/148 JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck1 ); c.add( jck2 ); c.add( jck3 ); JButton jb1 = new JButton("place order"); c.add( jb1 ); JButton jb2 = new JButton("cancel"); c.add( jb2 ); JLabel jl = new JLabel(new ImageIcon("bmw.jpg")); c.add(jl); :

OOP (Java): Layout/149 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,200); setLocationRelativeTo(null); // center the window setVisible(true); } // end of FlowDemo() public static void main(String[] args) { new FlowDemo(); } } // end of FlowDemo class

OOP (Java): Layout/1410 Initial Appearance

OOP (Java): Layout/1411 After Resizing There is now space for everything on one line.

OOP (Java): Layout/1412 Notes By default, all the components on a line are centered – –the alignment can be altered, e.g. c.setLayout( new FlowLayout( FlowLayout.RIGHT)); – –there is also FlowLayout.LEFT The component sizes are unchanged – –this is not true of some other layout managers

OOP (Java): Layout/ Grid Layout GridLayout places components in a grid, specified in terms of the number of rows and columns – –the spacing between the grid cells can also be specified Some of the components are resized to fit the grid cell they appear inside – –doesn't look nice continued 2x2

OOP (Java): Layout/1414 GridDemo.java contains one major change from FlowDemo.java : c.setLayout( new GridLayout(3,2,10,7); – –3 rows, 2 columns, 10 pixel horizontal spacing, 7 pixel vertical spacing The other change is to increase the vertical size of the frame: setSize(400,400);

OOP (Java): Layout/1415 GridDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GridDemo extends JFrame { public GridDemo() { super("E-Commerce Application"); Container c = getContentPane(); // use GridLayout: 3 rows, 2 columns // 10 pixel horiz. gap, 7 pixel vert. gap c.setLayout( new GridLayout(3, 2, 10, 7) ); :

OOP (Java): Layout/1416 JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck1 ); c.add( jck2 ); c.add( jck3 ); JButton jb1 = new JButton("place order"); c.add( jb1 ); JButton jb2 = new JButton("cancel"); c.add( jb2 ); JLabel jl = new JLabel(new ImageIcon( bmw.jpg")); c.add(jl); :

OOP (Java): Layout/1417 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); setLocationRelativeTo(null); // center the window setVisible(true); } // end of GridDemo() public static void main(String[] args) { new GridDemo(); } } // end of GridDemo class

OOP (Java): Layout/1418 Appearance Note the horizontal and vertical spacing between the components. Components have been resized to equally fill the 400x400 space. check boxes aren’t resized

OOP (Java): Layout/1419 GridDemoP.java Components can be protected from resizing by being placed inside a JPanel – –the panel is resized instead : // use a panel to stop the cancel button growing JPanel p = new JPanel(); JButton jb2 = new JButton("cancel"); p.add(jb2); c.add( p ); :

OOP (Java): Layout/1420 Appearance the 'cancel' button has not been resized

OOP (Java): Layout/ Border Layout BorderLayout allows four components to be placed around the edges of a frame/applet, with a fifth component in the center – –the positions are NORTH, EAST, SOUTH, WEST, and CENTER BorderLayout is the default layout for JFrame NORTH SOUTH CENTER EAST WEST

OOP (Java): Layout/1422 BorderDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BorderDemo extends JFrame { public BorderDemo() { super("E-Commerce Application"); Container c = getContentPane(); // use BorderLayout: // 10 pixel horiz. gap, 7 pixel vert. gap c.setLayout( new BorderLayout(10,7) ); :

OOP (Java): Layout/1423 // JCheckBox jck1 = // not used here //new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck2, BorderLayout.EAST); c.add( jck3, BorderLayout.SOUTH); JButton jb1 = new JButton("place order"); c.add( jb1, BorderLayout.NORTH); JButton jb2 = new JButton("cancel"); c.add( jb2, BorderLayout.WEST); JLabel jl = new JLabel(new ImageIcon( "bmw.jpg")); c.add(jl, BorderLayout.CENTER); :

OOP (Java): Layout/1424 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); // pack(); setLocationRelativeTo(null); setVisible(true); } // end of BorderDemo() public static void main(String[] args) { new BorderDemo(); } } // end of BorderDemo class

OOP (Java): Layout/1425 Appearance Note the vertical and horizontal spacing between the components. Components have been resized to fill the 400x400 space.

OOP (Java): Layout/1426 Component Resizing Components are resized: – –NORTH and SOUTH are stretched to be as wide as the window – –EAST and WEST are stretched to be tall enough to touch the NORTH and SOUTH components – –CENTER is enlarged to be as big as possible Often the look of the GUI can be improved by calling pack().

OOP (Java): Layout/1427 Appearance with pack() The vertical and horizontal spacing between the components is not affected.

OOP (Java): Layout/1428 More than Five? It is possible to have more than five components in a GridLayout – –place them inside a JPanel (which can have its own layout) – –the JPanel container can become one of the components in the top-level frame/applet This use of JPanel is shown later.

OOP (Java): Layout/1429 Less than Five? If the grid does not have a component for a given position, then the other components are resized to fill the space. – –e.g. if NORTH or SOUTH are not used, then EAST, CENTER, and WEST will be made taller to fill the space CENTER EAST WEST

OOP (Java): Layout/ Box Layout This places the components in a horizontal or vertical sequence – –components are not resized BoxDemo.java places its components vertically – –aside from the layout manager, the code is very similar to FlowDemo.java – –pack() can be used to reduce the window size

OOP (Java): Layout/1431 BoxDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BoxDemo extends JFrame { public BoxDemo() { super("E-Commerce Application"); Container c = getContentPane(); // use BoxLayout: align components vertically c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS) ); :

OOP (Java): Layout/1432 JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); c.add( jck1 ); c.add( jck2 ); c.add( jck3 ); JButton jb1 = new JButton("place order"); c.add( jb1 ); JButton jb2 = new JButton("cancel"); c.add( jb2 ); JLabel jl = new JLabel(new ImageIcon("bmw.jpg")); c.add(jl); :

OOP (Java): Layout/1433 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,400); // pack(); setLocationRelativeTo(null); setVisible(true); } // end of BoxDemo() public static void main(String[] args) { new BoxDemo(); } } // end of BoxDemo class

OOP (Java): Layout/1434 Appearance

OOP (Java): Layout/1435 With pack()

OOP (Java): Layout/ Combining Layouts Real GUIs usually require several layout managers for different parts of the display. The basic technique is to create panels (with JPanel ) to hold parts of the display – –each panel will have its own layout – –a panel may have subpanels

OOP (Java): Layout/1437 Layouts to Choose From... flow grid N C S EW border box

OOP (Java): Layout/1438 Example Appearance

OOP (Java): Layout/1439 Component Layout Hierarchy frame (border) West Center East panel p1 (vertical box) image panel p2 (vertical box) check box check box check box button

OOP (Java): Layout/1440 CenterEastWest box border

OOP (Java): Layout/1441 CombinedDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CombinedDemo extends JFrame { public CombinedDemo() { super("E-Commerce Application"); Container c = getContentPane(); // use default BorderLayout for frame :

OOP (Java): Layout/1442 // panel 1: vertical box layout JPanel p1 = new JPanel(); p1.setLayout( new BoxLayout(p1, BoxLayout.Y_AXIS)); JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); p1.add( jck1 ); p1.add( jck2 ); p1.add( jck3 ); :

OOP (Java): Layout/1443 // panel 2: vertical box layout JPanel p2 = new JPanel(); p2.setLayout( new BoxLayout(p2, BoxLayout.Y_AXIS)); JButton jb1= new JButton("place order"); p2.add( jb1 ); JButton jb2 = new JButton("cancel"); p2.add( jb2 ); :

OOP (Java): Layout/1444 JLabel jl = new JLabel( new ImageIcon("bmw.jpg")); // add panels and image to frame c.add(p1, BorderLayout.WEST); c.add(jl, BorderLayout.CENTER); c.add(p2, BorderLayout.EAST); :

OOP (Java): Layout/1445 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setResizable(false); // disable window resizing setLocationRelativeTo(null); setVisible(true); } // end of CombinedDemo() public static void main(String[] args) { new CombinedDemo(); } } // end of CombinedDemo class

OOP (Java): Layout/ Improving the Appearance There are many ways to improve on the basic appearance of the layouts: – –borders – –fixing the sizes of controls to be the same e.g. the size of buttons – –adding space between controls CombinedDemo2.java uses these techniques.

OOP (Java): Layout/1447 CombinedDemo2.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CombinedDemo2 extends JFrame { public CombinedDemo2() { super("E-Commerce Application"); Container c = getContentPane(); // use default GridLayout for frame :

OOP (Java): Layout/1448 // panel 1: vertical box layout JPanel p1 = new JPanel(); // 10 pixel invisible border all around p1.setBorder(BorderFactory.createEmptyBorder( 10, 10, 10, 10)); p1.setLayout( new BoxLayout(p1, BoxLayout.Y_AXIS)); JCheckBox jck1 = new JCheckBox("Downgrade dog to cat"); JCheckBox jck2 = new JCheckBox("Upgrade bike to car"); JCheckBox jck3 = new JCheckBox("Add speed package"); p1.add( jck1 ); p1.add( jck2 ); p1.add( jck3 ); :

OOP (Java): Layout/1449 // panel 2: vertical box layout JPanel p2 = new JPanel(); // 14 pixel invisible border all around p2.setBorder(BorderFactory.createEmptyBorder( 14, 14, 14, 14)); p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS)); JButton jb1 = new JButton("place order"); p2.add( jb1 ); // add 15 pixel vertical space between buttons p2.add(Box.createRigidArea( new Dimension(0, 15))); JButton jb2 = new JButton("cancel"); // set the size of button jb2 to be same as jb1 jb2.setPreferredSize( jb1.getPreferredSize() ); p2.add( jb2 ); :

OOP (Java): Layout/1450 JLabel jl = new JLabel(new ImageIcon("bmw.jpg")); // add panels and image to frame c.add(p1, BorderLayout.WEST); c.add(jl, BorderLayout.CENTER); c.add(p2, BorderLayout.EAST); :

OOP (Java): Layout/1451 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setResizable(false); setLocationRelativeTo(null); setVisible(true); } // end of CombinedDemo() public static void main(String[] args) { new CombinedDemo2(); } } // end of CombinedDemo2 class

OOP (Java): Layout/1452 Appearance 10 pixel invisible border 14 pixel invisible border buttons are the same size 15 pixel space between buttons

OOP (Java): Layout/1453 which a beginner should NOT use 8. Other Layout Managers 1) CardLayout manager – –arranges components into a "deck of cards", where only the top component is visible – –has been replaced by JTabbedPane

OOP (Java): Layout/1454 2) GridBagLayout manager – –a complex (powerful) variation of GridLayout – –components can occupy several adjacent cells – –uses constraints and weights to work out the position of components – –mostly used by GUI builders

OOP (Java): Layout/1455 o o 3) SpringLayout specifies relationships between the edges of its components – –e.g. define that the left edge of one component is a certain distance from the right edge of another component – –designed for use by GUI builders

OOP (Java): Layout/1456 o 4) GroupLayout works with the horizontal and vertical layouts separately o the layout is defined for each dimension independently o o designed for use by GUI builders

OOP (Java): Layout/1457 5) The null layout manager: – –use no layout manager, and position components using: setLocation(int x, int y) – –much more work – –the GUI design may not look correct on different platforms

OOP (Java): Layout/1458 "Third-party" means that the managers are not a standard part of Java "Third-party" means that the managers are not a standard part of Java –must be downloaded separately Two popular ones: Two popular ones: –MiG Layout: –JGoodies FormsLayout Third-party Layout Managers