2004-02-03 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.

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
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
1 L48 Advanced GUI Component (3). 2 OBJECTIVES  To use additional layout managers.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
CSE 219 Computer Science III Graphical User Interface.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
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.
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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
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
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
© Marty Hall, Larry Brown, Web core programming 1 Layout Managers Arranging Elements in Windows.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Object-Oriented Software Engineering
3461 Laying Out Components Interior Design for GUIs.
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.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
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.
Java Swing - Lecture 3 Layout Management
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Object-Oriented Software Engineering JFrames with Swing.
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Java GUI.
University of Central Florida COP 3330 Object Oriented Programming
Tim McKenna Layout Mangers in Java Tim McKenna
Chap 7. Building Java Graphical User Interfaces
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.
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.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 Horstmann, Chapter 10 m Core Java (Volume 1), Chapter 9

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 2 Inner classes r Java supports inner classes, i.e., classes defined inside other classes. r Inner classes are often used to define listeners for events. public class ActionPanel3 extends JPanel { int darkness = 128; private class DarkenListener implements ActionListener { public void actionPerformed(ActionEvent e) { darkness = darkness-10; repaint(); } }.... }

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 3 Inner classes (cont.)  The inner class can access the instance variables of the outer class (e.g., darkness ) as well as the methods (e.g., repaint ). r An instance of the inner class can be supplied as the listener for events: public class ActionPanel3 extends JPanel {.... JButton darken = new JButton(“Darken”); public ActionPanel3() { add(darken); darken.addActionListener( new DarkenListener()); }

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 4 Why use inner classes? r We can handle each event separately in a self-contained method, e.g., m DarkenListener.actionPerformed m LightenListener.actionPerformed r Leads to better structure when a class has to implement listeners for a large number of events.

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 5 Keyword: this  Any object can refer to itself using the keyword this. this.x this.setX(2.0) button.addActionListener(this) r Note that a superclass can call a subclass method (due to overriding).

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 6 Keyword: super  The keyword super refers to the superclass instance implicit inside the current object. public class MyPanel extends JPanel() { public paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(x1, y1, xSize, ySize); }..... }

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 7 Subclass constructors r A constructor of a subclass always has an implicit call to the superclass’s constructor at the beginning: public class MyPanel extends JPanel { public MyPanel() { // implicitly super(); setPreferredSize(300, 400); } r However, if there are arguments to the superclass constructor, need to call it explicitly.

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 8 Layout r Java GUI library is designed to be flexible. r Create interfaces for multiple platforms, screen sizes, window sizes, font sizes, international languages etc. r This requires a “layout manager” for each GUI panel. r Comes into picture whenever the panel needs to be laid out. r See Java tutorial for quick intro

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 9 FlowLayout r The default layout manager for a panel. r Places components in top-to-bottom, left-to- right order, like a word processor. r Components are kept at their preferred sizes as far as possible. (No stretching or shrinking). r Often components are centered horizontally. r See ActionPanel2 in code examples.

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 10 BorderLayout r Default layout for JFrames. For other containers, set it: container.setLayout(new BorderLayout()); r A BorderLayout container contains exactly 5 components: north, south, east, west and center. container.add( component, BorderLayout.NORTH); r Of course, any of these components can be a panel that groups together smaller components. r The 5 components stretch to fill all available space.

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 11 BoxLayout r A box is an array of components laid out horizontally or vertically. r Use it via the Box container: Box b = Box.createVerticalBox(); r Components in a box can be aligned: left, right or center (center by default). (Similarly: top, bottom or center for horizontal boxes.) r See ActionPanelCanvas3 in code examples.

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 12 GridBagLayout r The most flexible layout manager in Swing. r Imagine the panel arranged as a grid of invisible “cells.” r A component can occupy any rectangular area of cells. r Within its rectangular area, a component can either stretch or not, aligned or not, padded or not. (Lots of options!)

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 13 GridBagConstraints  To specify all these options, Swing provides another class called GridBagConstraints. panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; // position c.gridwidth = 1; c.gridheight = 3; // size c.weightx = 100; c.weighty = 100; // stretch JList style = new JList(); panel.add(style, c);

MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 14 Programming GridBags r Don’t use the GridBagLayout class directly. r Define subclasses of JPanel with appropriate layout built-in: public class GridBagPanel extends Panel { GridBagConstraints gbc = new GridBagConstraints(); public GridBagPanel() { setLayout(new GridBagLayout());.... set default constraints } public add(Component c, int x1, int y1, int x2, int y2) {.... }