Chapter 4: Laying out the GUI Useful containers –Panels –Frames –Dialogs Layout mechanisms –FlowLayout –BorderLayout –GridLayout –CardLayout Menus.

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

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
TCU CoSc Programming with Java Visual Design (Chapter 5)
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 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.
Corresponds with Chapter 12
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
LAB SESSION 10 *LAYOUT MANAGER *LISTENERS. Laying the components manually by using a null layout is tedious. Each container object has a layout manager.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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 Programming 1 Java Programming II Events, AWT, and Swing.
GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of.
Layout Managers Arranges and lays out the GUI components on a container.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Using AWT Controls, Layout Managers, and Menus Controls are components that allow a user to interact with your application in various ways A layout manager.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.
Basics of GUI Programming Chapter 11 and Chapter 22.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Event and GUI programming 1. Event Handling in Java An object resides in a particular state until it is made to transit to other state. This transition.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Chapter 3: Widgets for a GUI General Component methods Useful widgets classes –Text classes Label TextField TextArea –Active widgets Button Checkbox Choice.
AWT AWT stands for Abstract Windowing Toolkit. It contains all classes to write the program that interface between the user and different windowing toolkits.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
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.
Fundamental of Java Programming Abstract Window Toolkit
Swing JComponents.
Advanced GUIs II CS Lecture
Modern Programming Language Java
TOPICS Labels Using Buttons Applying Check Boxes CheckBox Group
Java Swing.
AWT Components.
Abstract Window ToolKit (AWT)
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT.
AWT Components and Containers
Ch. No Name Marks 01 AWT & SWING EVENT HANDLING NETWORKING
Graphical User Interface
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
Menu Bars and Menus.
GUI building with the AWT
Advanced GUIs II CS Lecture
Advanced GUIs and Graphics
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Chapter 4: Laying out the GUI Useful containers –Panels –Frames –Dialogs Layout mechanisms –FlowLayout –BorderLayout –GridLayout –CardLayout Menus

Controlling the Layout To get look we want use objects control how widgets arranged Generally, divide window into regions and arrange objects in regions Can also bring up new windows Also have mechanisms for creating menus

Containers - mechanisms for controlling appearance Canvas - mechanism for controlling look of graphic objects (pictures, etc.)

Container class abstract class methods: void add(Component c) - add component c void add(Component c, int position) - add c at index void add(String name, Component c) - add c w/ name void remove(Component c) - remove from container void removeAll() - remove all components Component getComponent(int position) - comp at pos int countComponents() - num of comps in container

Container class (cont) methods: Container getParent() - parent cont of container boolean isAncestorOf(Component c) - true if parent, grandparent, etc. of c Dimension minimumSize() - min required size Dimension preferredSize() - preferred size void layout() - force layout to occur (need not call) LayoutManager getLayout() - return the LayoutManager being used (null if none) void setLayout (LayoutManager layout) - set the LayoutManager to use

Panel class generally used, small, simple (Applet is a Panel) constructor: Panel() - empty container methods: inherited from general Container class layout method: initially set to FlowLayout Layouts mechanisms for distributing objects each container has Layout manager

FlowLayout lay things out like word processor, left to right, one column at a time, using minimum space constants: LEFT, CENTER, RIGHT - for alignments constructors: FlowLayout() - centered, standard horz,vert gaps FlowLayout(int align) - set align FlowLayout(int align, int hGap, int vGap) methods (also for BorderLayout, GridLayout): void layoutContainer(Container c) - layout c w/ this Dimension minimumLayoutSize(Container c) Dimension preferredLayoutSize(Container c)

BorderLayout use add method with string as in add(“North”,myButton) which regions depends on string constructors: BorderLayout() - 0 horiz,vert gaps BorderLayout(int hGap, int vGap) methods - same as for FlowLayout notes: BorderLayout resizes objects to fit Layout order: first N,S given needed height, remainder goes to E,C,W, then E,W given needed width, remaining space to C To avoid resizing, add object to panel and add panel to window

GridLayout objects arranged left-right, top-bottom in even sized cells objects resized to fit cells columns are added if needed constructors: GridLayout() - 1 row, infinite columns, default gaps GridLayout(int rows, int cols) - default gaps GridLayout(int r, int c, int hGap, int vGap) - set rows, cols, gaps methods - same as for FlowLayout

public class TestLayout extends Applet { Panel Panel1 = new Panel(); Panel Panel2 = new Panel(); Panel Panel3 = new Panel(); Button BA1 = new Button("A"); Button BA2 = new Button("A"); Button BA3 = new Button("A"); Button BB1 = new Button("B"); Button BB2 = new Button("B"); Button BB3 = new Button("B"); Button BC1 = new Button("C"); Button BC2 = new Button("C"); Button BC3 = new Button("C"); Button BD1 = new Button("D"); Button BD2 = new Button("D"); Button BD3 = new Button("D"); Button BE1 = new Button("E"); Button BE2 = new Button("E"); Button BE3 = new Button("E"); public void init () { Panel2.setLayout(new BorderLayout()); Panel3.setLayout(new GridLayout(2,3)); Panel1.add(BA1); Panel1.add(BB1); Panel1.add(BC1); Panel1.add(BD1); Panel1.add(BE1); Panel2.add("North",BA2); Panel2.add("South",BB2); Panel2.add("East",BC2); Panel2.add("West",BD2); Panel2.add("Center",BE2); Panel3.add(BA3); Panel3.add(BB3); Panel3.add(BC3); Panel3.add(BD3); Panel3.add(BE3); add(Panel1); add(Panel2); add(Panel3); }

CardLayout objects maintained as a stack, only one on top shown best to name components (to find the one you want) constructors: CardLayout() - empty stack CardLayout(int hGap, int vGap) - margins around object group methods: void first(Container par) - first comp in deck (note par) void last(Container par) - last comp in deck void next(Container par) - next comp in deck void previous(Container par) - previous comp in deck void show(Container par, String name) - show named comp see text, pp for example

No Layout can also use no layout: myContainer.setLayout(null); in this case you manually shape,locate objects and add things like borders yourself Canvas often used when you want to glue separate objects together (a picture plus text) in a specific way, without worrying about manager decisions constructor: Canvas()

Window class abstract class pop-up environments, separate from existing container often used for interaction (Dialogs) two types: Frame, Dialog Window methods: void dispose() - eliminate resources used when done void pack() - resize to fit added components void show() - make the window appear

Dialog class generally for simple input/output notes: must have associated frame (ok to use dummy frame) can be modal (takes all input) or modeless must reshape and show to execute uses BorderLayout unless changed constructors: Dialog(Frame parent, boolean isModal) - no title Dialog(Frame parent, String title, boolean isModal) methods: boolean isModal() - true if modal dialog void setModal(boolean isModal) - set modal flag boolean isResizeable() - true if window resizeable void setResizable(boolean canResize)

import java.applet.*; import java.awt.*; public class TestDialog extends Applet { Frame dummy = new Frame(); Dialog myDialog = new Dialog(dummy,"NJ Attitude",true); Panel qpanel = new Panel(); Label question = new Label("You talking to me?"); Checkbox yes = new Checkbox("yes"); Checkbox no = new Checkbox("no"); CheckboxGroup answer = new CheckboxGroup(); public void init () { yes.setState(true); yes.setCheckboxGroup(answer); no.setCheckboxGroup(answer); qpanel.setLayout(new GridLayout(1,3)); qpanel.add(question); qpanel.add(yes); qpanel.add(no); myDialog.add(qpanel); myDialog.reshape(100,100,400,100); myDialog.show(); }

Frame class more general than Dialog notes: needs no associated frame must reshape and show to execute uses BorderLayout unless changed constructors: Frame() - empty with no title Frame(String title) - empty with title methods: boolean isResizeable() - true if window resizeable void setResizable(boolean canResize)

Menus built using set of components: –menu has a MenuBar associated with a Frame –menuBar holds a collection of Menu objects (submenus) –a Menu holds a set of MenuItems (plus separators) –a Menu can be used in place of a MenuItem –MenuItems can be enabled and disabled, also checked

MenuComponent class abstract class useful methods: Font getFont() - return Font associated with comp void setFont(Font f) - set Font String getName() - get name of component void setName(String name) - set name of component TearOff Menus some systems allow menus to “tear off”, I.e. be pulled away from the rest of menu

MenuBar class constructor: MenuBar() - empty menu bar methods: Menu add(Menu m) - add m to menu bar (return) void remove(int index) - remove index item from bar void remove(MenuComponent m) - remove m int countMenus() - return number of menus Menu getMenu(int index) - return index menu Menu getHelpMenu() - return menu of Help menu void setHelpMenu(Menu m) - set m as Help menu

Menu class constructors: Menu(String name) - make named Menu Menu(String name, boolean canTearOff) - named menu with tear off status noted methods: void add(String name) - add named item to Menu MenuItem add(MenuItem m) - add MenuItem to Menu MenuItem addSeparator() - add separator (line) void remove(int index) - remove index item void remove(MenuComponent m) - remove m MenuItem getItem(int index) - index item of Menu int countItems() - number of items in Menu boolean isTearOff() - status of TearOff flag

MenuItem class constructor: MenuItem(String name) - make item with name methods: void disable() - mark item as disabled in menu void enable() - mark item as enabled in menu boolean isEnabled() - check enabled/disabled status String getLabel() - name of item void setLabel(String name) - set name of item

CheckboxMenuItem class type of MenuItem initially unchecked constructor: CheckboxMenuItem(String name) - named item methods: boolean getState() - return checked status of item void setState(boolean state) - sets checked status of item

public class TestMenu extends Applet { Frame myFrame = new Frame("Some choices"); MenuBar mymenuBar = new MenuBar(); Menu fileMenu = new Menu("File"); Menu editMenu = new Menu("Edit"); Menu helpMenu = new Menu("Help!"); Menu openMenu = new Menu("Open"); public void init() { fileMenu.add("New"); openMenu.add("File"); openMenu.add("Project"); fileMenu.add(openMenu); fileMenu.addSeparator(); fileMenu.add("Save"); fileMenu.add("Save As"); fileMenu.add("Quit"); editMenu.add("Cut"); editMenu.add("Copy"); editMenu.add("Paste"); helpMenu.add(new CheckboxMenuItem("Topics")); helpMenu.add("Search"); mymenuBar.add(fileMenu); mymenuBar.add(editMenu); mymenuBar.add(helpMenu); myFrame.setMenuBar(mymenuBar); myFrame.reshape(100,200,400,150); myFrame.show(); }

Running Applications Class for application must have a main() method (see example) Generally do not use Applet Generally build own frame/dialogs

import java.awt.*; public class TestDialogApp extends Frame { public TestDialogApp() {} public static void main(String[] args) { Frame f = new TestDialogApp(); QDialog qd = new QDialog(f,"You talking to me?"); qd.reshape(10,10,400,100); qd.show(); }} class QDialog extends Dialog { public QDialog(Frame parent, String message) { super(parent,false); // Call parents ctor Panel qpanel = new Panel(); Label question = new Label(message); Checkbox yes = new Checkbox("yes"); Checkbox no = new Checkbox("no"); CheckboxGroup answer = new CheckboxGroup(); setBackground(Color.lightGray);setForeground(Color.black); setResizable(false); yes.setState(true); yes.setCheckboxGroup(answer); no.setCheckboxGroup(answer); qpanel.setLayout(new GridLayout(1,3)); qpanel.add(question); qpanel.add(yes); qpanel.add(no); add("Center",qpanel); reshape(100,100,400,100); show(); }}